add editfragment

This commit is contained in:
adm 2024-11-07 18:36:23 +03:00
parent f3417b5e55
commit 14b0959bc2
15 changed files with 354 additions and 84 deletions

View File

@ -4,7 +4,7 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-10-30T14:28:50.613358900Z">
<DropdownSelection timestamp="2024-11-06T14:04:58.126302300Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\adm\.android\avd\Medium_Phone_API_35.avd" />

View File

@ -10,12 +10,13 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.autorization.R
import com.example.autorization.ui.fragments.signup.User
class UserAdapter(private val users:List<User>): Adapter<UserAdapter.UserViewHolder>() {
class UserAdapter(): Adapter<UserAdapter.UserViewHolder>() {
private val _users:MutableList<User> = mutableListOf<User>()
var OnClick:((index:Int, view:View) -> Unit)? = null
private val _users = mutableListOf<User>()
init {
_users.addAll(users)
fun setList(user: List<User>){
_users.addAll(user.toMutableList());
}
inner class UserViewHolder(view: View):ViewHolder(view) {
@ -35,7 +36,10 @@ class UserAdapter(private val users:List<User>): Adapter<UserAdapter.UserViewHol
return UserViewHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.user_item, parent, false))
}
fun addUser(user:User){
_users.add(0, user)
notifyItemInserted(0)
}
fun deleteUserById(index: Int){
_users.removeAt(index)
notifyItemRemoved(index)

View File

@ -0,0 +1,55 @@
package com.example.autorization.ui.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.example.autorization.R
import com.example.autorization.ui.fragments.signup.User
class UserAdapterDiffUtils: ListAdapter<User, UserAdapterDiffUtils.UserViewHolder>(UserDiffCallback) {
var OnLongClick: ((User) -> Unit)? = null
inner class UserViewHolder(view:View): RecyclerView.ViewHolder(view) {
val userNameTextView = view.findViewById<TextView>(R.id.UserNameTextView)
val userEmailTextView = view.findViewById<TextView>(R.id.UserEmailTextView)
val userPasswordTextView = view.findViewById<TextView>(R.id.UserPasswordTextView)
val userCardView = view.findViewById<CardView>(R.id.UserCardView)
fun bind(user: User){
userEmailTextView.text = user.email
userNameTextView.text = user.name
userPasswordTextView.text = user.password
userCardView.setOnClickListener {
OnLongClick?.invoke(user)
}
}
}
object UserDiffCallback: DiffUtil.ItemCallback<User>(){
override fun areItemsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem.email == newItem.email
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
return UserViewHolder(LayoutInflater
.from(parent.context)
.inflate(R.layout.user_item, parent, false)
)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
val user = getItem(position)
holder.bind(user)
}
}

View File

@ -1,61 +0,0 @@
package com.example.autorization.ui.fragments
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import android.widget.PopupMenu.OnMenuItemClickListener
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.autorization.R
import com.example.autorization.ui.adapters.UserAdapter
import com.example.autorization.ui.fragments.signup.User
class UsersFragment : Fragment(R.layout.fragment_users) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<RecyclerView>(R.id.UserRecyclerView)
val users: List<User> = mutableListOf(
User(email = "test@mail.ru", name = "Alex", password = "123"),
User(email = "test@mail.ru", name = "Bob", password = "321"),
User(email = "test@mail.ru", name = "Alex", password = "123"),
User(email = "test@mail.ru", name = "Bob", password = "321"),
User(email = "test@mail.ru", name = "Alex", password = "123"),
User(email = "test@mail.ru", name = "Alex", password = "123"),
User(email = "test@mail.ru", name = "Bob", password = "321"),
User(email = "test@mail.ru", name = "Alex", password = "123"),
)
recyclerView.layoutManager = LinearLayoutManager(requireContext())
val userAdapter = UserAdapter(users)
recyclerView.adapter = userAdapter
userAdapter.OnClick = { index, view ->
showMenu(view, userAdapter, index)
}
}
private fun showMenu(
view:View,
userAdapter: UserAdapter,
index:Int){
PopupMenu(requireContext(), view).apply {
setOnMenuItemClickListener { item ->
when (item?.itemId) {
R.id.DeleteAction -> {
userAdapter.deleteUserById(index)
true
}
else -> true
}
}
inflate(R.menu.user_action_menu)
show()
}
}
}

View File

@ -0,0 +1,6 @@
package com.example.autorization.ui.fragments.edituser
data class EditUser(val email: String,
val password: String,
val confirmedPassword: String? = null,
val name:String, )

View File

@ -0,0 +1,30 @@
package com.example.autorization.ui.fragments.edituser
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.autorization.R
import com.example.autorization.ui.fragments.signup.User
class EditUserFragment(private val user: User): Fragment(R.layout.fragment_edtit_user_screen) {
val editUserViewModel by viewModels<EditUserViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val passwordEditText = view.findViewById<EditText>(R.id.PasswordEditText)
val confirmedPasswordEditText = view.findViewById<EditText>(R.id.ConfirmPasswordEditText)
val loginEditText = view.findViewById<EditText>(R.id.EmailPhoneNumberEditText)
val nameEditText = view.findViewById<EditText>(R.id.NameEditText)
val createButton = view.findViewById<Button>(R.id.CreateAccountButton)
editUserViewModel.setUser(user)
editUserViewModel.user.observe(viewLifecycleOwner){
if (it != null){
loginEditText.setText(it.email)
nameEditText.setText(it.name)
}
}
}
}

View File

@ -0,0 +1,40 @@
package com.example.autorization.ui.fragments.edituser
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.autorization.ui.fragments.signup.User
class EditUserViewModel:ViewModel() {
private val _editUser = MutableLiveData<EditUser?>(null)
val editUser : LiveData<EditUser?> = _editUser
private val _user = MutableLiveData<User?>(null)
val user = _user
fun setUser(user: User){
_user.postValue(user)
}
fun editUserName(newName:String){
_editUser.postValue(_editUser.value?.copy(name = newName))
}
fun editUserPassword(password:String){
_editUser.postValue(_editUser.value?.copy(password = password))
}
fun editUserEmail(email:String){
_editUser.postValue(_editUser.value?.copy(email = email))
}
fun editUserConfirmedPassword(confirmedPassword: String){
_editUser.postValue(_editUser.value?.copy(confirmedPassword = confirmedPassword))
}
fun editUser(): User?{
val editUser = _editUser.value
if(editUser?.password == editUser?.confirmedPassword) return _user.value
return null
}
}

View File

@ -11,14 +11,24 @@ import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.fragment.app.viewModels
import com.example.autorization.R
import com.example.autorization.ui.fragments.users.UserViewModel
import com.example.autorization.ui.fragments.users.UsersFragment
class SignUpScreen : Fragment(R.layout.fragment_sign_up_screen) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewModel by viewModels<SignUpViewModel> ()
val viewModelUser by activityViewModels<UserViewModel> ()
val passwordEditText = view.findViewById<EditText>(R.id.PasswordEditText)
val loginEditText = view.findViewById<EditText>(R.id.EmailPhoneNumberEditText)
val nameEditText = view.findViewById<EditText>(R.id.NameEditText)
@ -38,7 +48,13 @@ class SignUpScreen : Fragment(R.layout.fragment_sign_up_screen) {
}
createButton.setOnClickListener {
viewModel.registrationUser()
val user = viewModel.registrationUser()
if(user != null) {
viewModelUser.addUser(user)
parentFragmentManager.commit {
replace<UsersFragment>(R.id.fragment_container_view)
}
}
}
viewModel.signUpState.observeForever{

View File

@ -10,41 +10,41 @@ class SignUpViewModel:ViewModel() {
private val users : MutableList<User> = mutableListOf()
fun registrationUser(){
if (_signUpState.value == null) return
fun registrationUser():User?{
if (_signUpState.value == null) return null
if(_signUpState.value!!.name.isBlank())
{
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустое имя пользователя")
)
return
return null
}
if(_signUpState.value!!.login.isBlank()){
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустой логин")
)
return
return null
}
if(_signUpState.value!!.password.isBlank()) {
_signUpState.postValue(_signUpState.value!!.copy(
errorMessage = "Пустой пароль")
)
return
return null
}
if(_signUpState.value!!.login.contains('@')) {
users.add(User(
val user = User(
email = _signUpState.value!!.login,
name = _signUpState.value!!.name,
password = _signUpState.value!!.password
))
return
)
return user
}
users.add(
User(
val user =User(
number = _signUpState.value!!.login,
name = _signUpState.value!!.name,
password = _signUpState.value!!.password
))
)
return user
}
fun setError(error:String) {

View File

@ -0,0 +1,8 @@
package com.example.autorization.ui.fragments.users
import com.example.autorization.ui.fragments.signup.User
sealed class UserState(){
data class AddUserAction(val user:User): UserState()
data class RemoveUserAction(val index:Int) : UserState()
}

View File

@ -0,0 +1,77 @@
package com.example.autorization.ui.fragments.users
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.autorization.R
import com.example.autorization.ui.adapters.UserAdapter
import com.example.autorization.ui.adapters.UserAdapterDiffUtils
import com.example.autorization.ui.fragments.edituser.EditUserFragment
import com.example.autorization.ui.fragments.signup.SignUpScreen
import com.example.autorization.ui.fragments.signup.SignUpState
import com.example.autorization.ui.fragments.signup.User
class UsersFragment : Fragment(R.layout.fragment_users) {
val viewModel by activityViewModels<UserViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<RecyclerView>(R.id.UserRecyclerView)
val createUserButton = view.findViewById<Button>(R.id.CreateAccountButton)
createUserButton.setOnClickListener {
viewModel.removeUserByPosition(0)
}
recyclerView.layoutManager = LinearLayoutManager(requireContext())
val userAdapterDiffUtils = UserAdapterDiffUtils()
recyclerView.adapter = userAdapterDiffUtils
viewModel.users.observe(viewLifecycleOwner){
userAdapterDiffUtils.submitList(it as MutableList<User>)
}
userAdapterDiffUtils.OnLongClick = { user ->
val editUserFragment = EditUserFragment(user)
parentFragmentManager.commit {
replace(R.id.fragment_container_view, editUserFragment)
}
}
}
private fun showMenu(
view:View,
index:Int){
PopupMenu(requireContext(), view).apply {
setOnMenuItemClickListener { item ->
when (item?.itemId) {
R.id.DeleteAction -> {
viewModel.removeUserByPosition(index)
true
}
else -> true
}
}
inflate(R.menu.user_action_menu)
show()
}
}
}

View File

@ -9,7 +9,7 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="com.example.autorization.ui.fragments.UsersFragment"
android:name="com.example.autorization.ui.fragments.users.UsersFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="24dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".ui.fragments.signup.SignUpScreen">
<TextView
android:layout_marginBottom="16dp"
android:layout_gravity="center"
android:textAppearance="@style/Header2"
android:textColor="@color/primary600"
android:text="@string/edituser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textAppearance="@style/Regular400"
android:textColor="@color/gray700"
android:text="@string/lorem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_marginTop="24dp"
android:id="@+id/NameEditText"
android:hint="@string/name"
style="@style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/EmailPhoneNumberEditText"
android:hint="@string/EmailOrPhone"
style="@style/BaseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/PasswordEditText"
android:hint="@string/password"
android:drawableEnd="@drawable/visibility_off"
style="@style/BaseEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/ConfirmPasswordEditText"
android:hint="@string/confirm_password"
android:drawableEnd="@drawable/visibility_off"
style="@style/BaseEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<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"
android:text="@string/edit"
android:textColor="@color/white"
android:enabled="false"
android:textAppearance="@style/Large400"
android:background="@drawable/create_button_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"/>
</LinearLayout>

View File

@ -1,15 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.UsersFragment">
android:orientation="vertical"
android:padding="10dp"
tools:context=".ui.fragments.users.UsersFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/UserRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/CreateAccountButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="10"
tools:listitem="@layout/user_item" />
</FrameLayout>
<Button
android:id="@+id/CreateAccountButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/create_button_color"
android:minHeight="60dp"
android:text="@string/create_account"
android:textAppearance="@style/Large400"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -12,4 +12,7 @@
<string name="create_account">Create Account</string>
<string name="do_you_have_account_sign_in">Do you have account? Sign In</string>
<string name="remove_user">Удалить пользователя</string>
<string name="edituser">EditUser</string>
<string name="confirm_password">Confirm Password</string>
<string name="edit">Edit</string>
</resources>