#18 Working update username
This commit is contained in:
		
							parent
							
								
									8ad82dda43
								
							
						
					
					
						commit
						f263b34c12
					
				
					 5 changed files with 34 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -8,6 +8,7 @@ import androidx.compose.ui.res.stringResource
 | 
			
		|||
import androidx.compose.ui.unit.sp
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
 | 
			
		||||
fun BasicTextButton(@StringRes text: Int, modifier: Modifier, action: () -> Unit) {
 | 
			
		||||
    TextButton(onClick = action, modifier = modifier) { Text(text = stringResource(text)) }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
package be.ugent.sel.studeez.common.composable
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.StringRes
 | 
			
		||||
import androidx.compose.foundation.text.KeyboardActions
 | 
			
		||||
import androidx.compose.foundation.text.KeyboardOptions
 | 
			
		||||
import androidx.compose.material.Icon
 | 
			
		||||
import androidx.compose.material.IconButton
 | 
			
		||||
| 
						 | 
				
			
			@ -14,8 +15,10 @@ import androidx.compose.runtime.*
 | 
			
		|||
import be.ugent.sel.studeez.R.string as AppText
 | 
			
		||||
import be.ugent.sel.studeez.R.drawable as AppIcon
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.input.key.Key
 | 
			
		||||
import androidx.compose.ui.res.painterResource
 | 
			
		||||
import androidx.compose.ui.res.stringResource
 | 
			
		||||
import androidx.compose.ui.text.input.ImeAction
 | 
			
		||||
import androidx.compose.ui.text.input.KeyboardType
 | 
			
		||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
 | 
			
		||||
import androidx.compose.ui.text.input.VisualTransformation
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,16 +1,12 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.profile
 | 
			
		||||
 | 
			
		||||
import androidx.compose.foundation.layout.Column
 | 
			
		||||
import androidx.compose.material.Button
 | 
			
		||||
import androidx.compose.material.Text
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.runtime.getValue
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.tooling.preview.Preview
 | 
			
		||||
import androidx.hilt.navigation.compose.hiltViewModel
 | 
			
		||||
import androidx.lifecycle.viewmodel.compose.viewModel
 | 
			
		||||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.BasicButton
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.BasicTextButton
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.LabelledInputField
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate
 | 
			
		||||
| 
						 | 
				
			
			@ -36,8 +32,12 @@ fun EditProfileScreen(
 | 
			
		|||
                label = R.string.username
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            BasicTextButton(text = R.string.save, Modifier.textButton()) {} // TODO
 | 
			
		||||
            BasicTextButton(text = R.string.delete_profile, Modifier.textButton()) {} // TODO
 | 
			
		||||
            BasicTextButton(text = R.string.save, Modifier.textButton()) {
 | 
			
		||||
                viewModel.onSaveClick()
 | 
			
		||||
            }
 | 
			
		||||
            BasicTextButton(text = R.string.delete_profile, Modifier.textButton()) {
 | 
			
		||||
                viewModel.onDeleteClick()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,31 +1,45 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.profile
 | 
			
		||||
 | 
			
		||||
import androidx.compose.runtime.mutableStateOf
 | 
			
		||||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
 | 
			
		||||
import be.ugent.sel.studeez.domain.AccountDAO
 | 
			
		||||
import be.ugent.sel.studeez.domain.LogService
 | 
			
		||||
import be.ugent.sel.studeez.domain.UserDAO
 | 
			
		||||
import be.ugent.sel.studeez.screens.StudeezViewModel
 | 
			
		||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
			
		||||
import javax.inject.Inject
 | 
			
		||||
 | 
			
		||||
@HiltViewModel
 | 
			
		||||
class ProfileEditViewModel @Inject constructor(
 | 
			
		||||
    private val accountDAO: AccountDAO,
 | 
			
		||||
    private val userDAO: UserDAO,
 | 
			
		||||
    logService: LogService
 | 
			
		||||
) : StudeezViewModel(logService) {
 | 
			
		||||
 | 
			
		||||
    var uiState = mutableStateOf(ProfileEditUiState())
 | 
			
		||||
        private set
 | 
			
		||||
 | 
			
		||||
    private val username
 | 
			
		||||
        get() = uiState.value.username
 | 
			
		||||
    init {
 | 
			
		||||
        launchCatching {
 | 
			
		||||
            uiState.value = uiState.value.copy(username = userDAO.getUsername()!!)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onUsernameChange(newValue: String) {
 | 
			
		||||
        uiState.value = uiState.value.copy(username = newValue)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onSaveClick() {
 | 
			
		||||
        // TODO
 | 
			
		||||
        launchCatching {
 | 
			
		||||
            userDAO.save(uiState.value.username)
 | 
			
		||||
            SnackbarManager.showMessage(R.string.save_success)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onDeleteClick() {
 | 
			
		||||
        // TODO
 | 
			
		||||
        launchCatching {
 | 
			
		||||
            accountDAO.deleteAccount()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue