git add viewmodel

This commit is contained in:
adm 2024-10-25 17:35:02 +03:00
parent fbe57b265f
commit 5cfb98ecfb
7 changed files with 84 additions and 6 deletions

View File

@ -4,7 +4,7 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-10-23T13:44:51.378268100Z">
<DropdownSelection timestamp="2024-10-25T13:51:03.991973200Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\adm\.android\avd\Medium_Phone_API_35.avd" />

View File

@ -35,8 +35,8 @@ android {
}
}
dependencies {
dependencies {
implementation (libs.androidx.fragment.ktx)
implementation(libs.androidx.core.ktx)

View File

@ -8,6 +8,7 @@ import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.viewModels
@ -19,16 +20,33 @@ class SignUpScreen : Fragment(R.layout.fragment_sign_up_screen) {
super.onViewCreated(view, savedInstanceState)
val viewModel by viewModels<SignUpViewModel> ()
val passwordEditText = view.findViewById<EditText>(R.id.PasswordEditText)
val loginEditText = view.findViewById<EditText>(R.id.EmailPhoneNumberEditText)
val nameEditText = view.findViewById<EditText>(R.id.NameEditText)
val termsCheckBox = view.findViewById<CheckBox>(R.id.TarmsAndPolicyCheckBox)
val createButton = view.findViewById<Button>(R.id.CreateAccountButton)
val errorTextView = view.findViewById<TextView>(R.id.ErrorLabel)
var passwordIsVisible = false
passwordEditText.doOnTextChanged { text, start, before, count ->
passwordEditText.doOnTextChanged { text, _, _, _ ->
viewModel.setPassword(text.toString())
}
loginEditText.doOnTextChanged { text, _, _, _ ->
viewModel.setLogin(text.toString())
}
nameEditText.doOnTextChanged { text, _, _, _ ->
viewModel.setName(text.toString())
}
viewModel.signUpState.observeForever{ it ->
createButton.setOnClickListener {
viewModel.registrationUser()
}
viewModel.signUpState.observeForever{
createButton.isEnabled = it.acceptTerms
if(it.errorMessage.isNotBlank()) {
errorTextView.text = it.errorMessage
errorTextView.visibility = View.VISIBLE
}
}
termsCheckBox.setOnCheckedChangeListener { buttonView, isChecked ->

View File

@ -5,5 +5,6 @@ data class SignUpState(
var password: String ="",
var name: String = "",
var acceptTerms: Boolean = false,
var errorMessage: String = "",
var buttonEnable: Boolean = acceptTerms
)

View File

@ -3,13 +3,60 @@ package com.example.autorization.ui.fragments.signup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import java.util.stream.Stream
import kotlin.math.log
class SignUpViewModel:ViewModel() {
private var _signUpState: MutableLiveData<SignUpState> = MutableLiveData(SignUpState())
val signUpState : LiveData<SignUpState> = _signUpState
private val users : MutableList<User> = mutableListOf()
fun registrationUser(){
if (_signUpState.value == null) return
if(_signUpState.value!!.name.isBlank())
{
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустое имя пользователя")
)
return
}
if(_signUpState.value!!.login.isBlank()){
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустой логин")
)
return
}
if(_signUpState.value!!.password.isBlank()) {
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустой пароль")
)
return
}
if(_signUpState.value!!.login.contains('@')) {
users.add(User(
email = _signUpState.value!!.login,
name = _signUpState.value!!.name,
password = _signUpState.value!!.password
))
return
}
users.add(
User(
number = _signUpState.value!!.login,
name = _signUpState.value!!.name,
password = _signUpState.value!!.password
))
}
fun setError(error:String) {
_signUpState.postValue(_signUpState.value?.copy(errorMessage = error))
}
fun setPassword(password:String) {
_signUpState.postValue(_signUpState.value?.copy(password = password))
}
fun setName(name:String) {
_signUpState.postValue(_signUpState.value?.copy(name = name))
}
fun setLogin(login:String) {
_signUpState.postValue(_signUpState.value?.copy(login = login))

View File

@ -0,0 +1,6 @@
package com.example.autorization.ui.fragments.signup
data class User(var email:String? = null,
var number:String? = null,
val name:String,
val password:String)

View File

@ -72,6 +72,12 @@
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/ErrorLabel"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/CreateAccountButton"
android:layout_marginTop="36dp"