update qr scan and navigation
This commit is contained in:
		
							parent
							
								
									639719453f
								
							
						
					
					
						commit
						87ad1617a4
					
				| @ -1,5 +1,5 @@ | ||||
| package ru.myitschool.work.core | ||||
| // БЕРИТЕ И ИЗМЕНЯЙТЕ ХОСТ ТОЛЬКО ЗДЕСЬ И НЕ БЕРИТЕ ИЗ ДРУГИХ МЕСТ. ФАЙЛ ПЕРЕМЕЩАТЬ НЕЛЬЗЯ | ||||
| object Constants { | ||||
|     const val SERVER_ADDRESS = "http://10.0.2.2:8080" | ||||
|     const val SERVER_ADDRESS = "http://10.6.66.79:8080" | ||||
| } | ||||
| @ -3,12 +3,14 @@ package ru.myitschool.work.ui | ||||
| import android.annotation.SuppressLint | ||||
| import android.os.Bundle | ||||
| import android.util.Log | ||||
| import android.view.View | ||||
| import androidx.activity.OnBackPressedCallback | ||||
| import androidx.appcompat.app.AppCompatActivity | ||||
| import androidx.navigation.createGraph | ||||
| import androidx.navigation.findNavController | ||||
| import androidx.navigation.fragment.NavHostFragment | ||||
| import androidx.navigation.fragment.fragment | ||||
| import androidx.navigation.ui.setupWithNavController | ||||
| import com.google.android.material.bottomnavigation.BottomNavigationView | ||||
| import dagger.hilt.android.AndroidEntryPoint | ||||
| import ru.myitschool.work.R | ||||
| @ -33,16 +35,17 @@ class RootActivity : AppCompatActivity() { | ||||
| 
 | ||||
|         val navController = navHostFragment?.navController ?: throw IllegalStateException("NavHostFragment not found") | ||||
|         val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation) | ||||
|         bottomNavigationView.setupWithNavController(navController) | ||||
| 
 | ||||
|         //navController.setGraph(R.navigation.main_nav_graph) | ||||
|         //navController.navigate(R.id.fragment_profile) | ||||
|         Log.d("role", "$userRole") | ||||
|         if (userRole == "ROLE_ADMIN") { | ||||
|             navController.setGraph(R.navigation.main_nav_graph) | ||||
|             navController.setGraph(R.navigation.main_admin_nav_graph) | ||||
|             navController.navigate(R.id.fragment_profile) | ||||
|         } else { | ||||
|             navController.setGraph(R.navigation.main_nav_graph) | ||||
|             bottomNavigationView.menu.clear() | ||||
|             bottomNavigationView.visibility = View.GONE | ||||
|             navController.navigate(R.id.fragment_profile) | ||||
| 
 | ||||
|         } | ||||
|  | ||||
| @ -0,0 +1,50 @@ | ||||
| package ru.myitschool.work.ui.admin | ||||
| 
 | ||||
| import android.os.Bundle | ||||
| import android.view.View | ||||
| import androidx.fragment.app.Fragment | ||||
| import androidx.fragment.app.viewModels | ||||
| import androidx.lifecycle.ViewModel | ||||
| import androidx.navigation.fragment.findNavController | ||||
| import com.squareup.picasso.Picasso | ||||
| import ru.myitschool.work.R | ||||
| import ru.myitschool.work.databinding.FragmentAdminBinding | ||||
| import ru.myitschool.work.databinding.FragmentProfileBinding | ||||
| import ru.myitschool.work.ui.profile.ProfileViewModel | ||||
| import ru.myitschool.work.utils.collectWithLifecycle | ||||
| 
 | ||||
| class AdminFragment : Fragment(R.layout.fragment_admin) { | ||||
|     private var _viewBinding: FragmentAdminBinding? = null | ||||
|     private val viewBinding: FragmentAdminBinding get() = _viewBinding!! | ||||
| 
 | ||||
|     private val viewModel by viewModels<AdminViewModel> { AdminViewModel.Factory } | ||||
| 
 | ||||
| 
 | ||||
|     override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||
|         _viewBinding = FragmentAdminBinding.bind(view) | ||||
|         super.onViewCreated(view, savedInstanceState) | ||||
| 
 | ||||
| 
 | ||||
|     /*viewModel.state.collectWithLifecycle(this) { state -> | ||||
| 
 | ||||
|         viewBinding.error.visibility = if (state is AdminViewModel.State.Error) View.VISIBLE else View.GONE | ||||
| 
 | ||||
| 
 | ||||
|         when(state) { | ||||
|             is AdminViewModel.State.Loading -> Unit | ||||
|             is AdminViewModel.State.GoToInfo -> { | ||||
|                 findNavController().navigate() | ||||
|             } | ||||
|             is ProfileViewModel.State.Error -> { | ||||
|                 viewBinding.errorText.text = state.text | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|     }*/ | ||||
| } | ||||
| 
 | ||||
| override fun onDestroyView() { | ||||
|     _viewBinding = null | ||||
|     super.onDestroyView() | ||||
| } | ||||
| } | ||||
| @ -0,0 +1,87 @@ | ||||
| package ru.myitschool.work.ui.admin | ||||
| 
 | ||||
| import android.util.Log | ||||
| import androidx.lifecycle.ViewModel | ||||
| import androidx.lifecycle.ViewModelProvider | ||||
| import androidx.lifecycle.viewModelScope | ||||
| import kotlinx.coroutines.flow.MutableStateFlow | ||||
| import kotlinx.coroutines.flow.asStateFlow | ||||
| import kotlinx.coroutines.launch | ||||
| import ru.myitschool.work.data.auth.AuthStorageDataSource | ||||
| import ru.myitschool.work.data.user.UserNetworkDataSource | ||||
| import ru.myitschool.work.data.user.UserRepoImpl | ||||
| import ru.myitschool.work.domain.user.EntranceEntity | ||||
| import ru.myitschool.work.domain.user.GetUserUseCase | ||||
| import ru.myitschool.work.domain.user.UserEntity | ||||
| 
 | ||||
| class AdminViewModel( | ||||
|     private val getUserUseCase: GetUserUseCase | ||||
| ) : ViewModel() { | ||||
|     private val _state = MutableStateFlow<State>(State.Loading) | ||||
|     val state = _state.asStateFlow() | ||||
| 
 | ||||
|     init { | ||||
|         updateStateGet() | ||||
|     } | ||||
| 
 | ||||
|     fun clickRefresh() { | ||||
|         updateStateGet() | ||||
|     } | ||||
| 
 | ||||
|     fun updateStateGet() { | ||||
|         viewModelScope.launch { | ||||
|             _state.emit(State.Loading) | ||||
|             val entranceList : List<EntranceEntity> = getUserUseCase.getEntrancesList().fold( | ||||
|                 onSuccess = { list -> | ||||
|                     list | ||||
|                 }, | ||||
|                 onFailure = { | ||||
|                     emptyList() | ||||
|                 } | ||||
|             ) | ||||
|             _state.emit( | ||||
|                 getUserUseCase.invoke().fold( | ||||
|                     onSuccess = { data -> | ||||
|                         Log.d("uraa", "успех успех ${data.toString()}") | ||||
| 
 | ||||
|                         State.GoToInfo(data, entranceList) | ||||
|                     }, | ||||
|                     onFailure = { error -> | ||||
|                         Log.d("kaput", error.message.toString()) | ||||
|                         State.Error(error.message.toString()) | ||||
|                     } | ||||
|                 ) | ||||
|             ) | ||||
|             //_state.emit(State.Error("о нет ошибка ошибка помогите")) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     sealed interface State { | ||||
|         data object Loading: State | ||||
|         data class GoToInfo( | ||||
|             val profileInfo: UserEntity, | ||||
|             val entrancesList : List<EntranceEntity> | ||||
|         ) : State | ||||
|         data class Error( | ||||
|             val text: String | ||||
|         ) : State | ||||
|     } | ||||
|     companion object { | ||||
|         val Factory : ViewModelProvider.Factory = object : ViewModelProvider.Factory { | ||||
|             @Suppress("UNCHECKED_CAST") | ||||
|             override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||||
|                 return AdminViewModel( | ||||
|                     getUserUseCase = GetUserUseCase( | ||||
|                         repo = UserRepoImpl( | ||||
|                             userNetworkDataSource = UserNetworkDataSource() | ||||
|                         ), | ||||
|                         authStorageDataSource = AuthStorageDataSource | ||||
|                     ) | ||||
|                 ) as T | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -56,7 +56,7 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) { | ||||
|                 is ProfileViewModel.State.Show -> { | ||||
|                     viewBinding.noData.visibility = View.GONE | ||||
|                     viewBinding.name.text = state.profileInfo.name | ||||
|                     viewBinding.position.text = "Должность: ${state.profileInfo.name}" | ||||
|                     viewBinding.position.text = "Должность: ${state.profileInfo.position}" | ||||
|                     if (state.profileInfo.lastEntry == null) viewBinding.lastEntry.text = "Время последнего входа: Нет данных" | ||||
|                     else viewBinding.lastEntry.text = "Время последнего входа: ${state.profileInfo.lastEntry}" | ||||
|                     Picasso.get().load(state.profileInfo.avatarUrl).resize(100, 100).centerCrop().into(viewBinding.imageView) | ||||
|  | ||||
| @ -26,8 +26,7 @@ class QrResultFragment : Fragment(R.layout.fragment_qr_result) { | ||||
|         super.onViewCreated(view, savedInstanceState) | ||||
|         parentFragmentManager.setFragmentResultListener( | ||||
|             QrScanDestination.REQUEST_KEY, viewLifecycleOwner | ||||
|         ) { | ||||
|             key, bundle -> | ||||
|         ) { key, bundle -> | ||||
|             if (key == QrScanDestination.REQUEST_KEY) { | ||||
|                 val code = QrScanDestination.getDataIfExist(bundle) | ||||
|                 viewModel.sendResult(code) | ||||
|  | ||||
| @ -23,6 +23,7 @@ data object QrScanDestination { | ||||
|     } | ||||
| 
 | ||||
|     internal fun packToBundle(data: String): Bundle { | ||||
| 
 | ||||
|         return bundleOf( | ||||
|             KEY_QR_DATA to data | ||||
|         ) | ||||
|  | ||||
| @ -1,28 +1,37 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_height="match_parent" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent"> | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto"> | ||||
| 
 | ||||
|     <androidx.fragment.app.FragmentContainerView | ||||
|         android:id="@+id/nav_host_fragment" | ||||
|         android:name="androidx.navigation.fragment.NavHostFragment" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         app:defaultNavHost="true" /> | ||||
|         android:layout_height="0dp" | ||||
|         app:defaultNavHost="true" | ||||
|         app:layout_constraintBottom_toTopOf="@+id/bottom_navigation" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         /> | ||||
| 
 | ||||
|     <com.google.android.material.bottomnavigation.BottomNavigationView | ||||
|         android:id="@+id/bottom_navigation" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_gravity="bottom" | ||||
|         android:layout_marginBottom="4dp" | ||||
| 
 | ||||
|         android:background="@color/white" | ||||
|         app:itemActiveIndicatorStyle="@color/orange" | ||||
|         app:itemIconTint="@color/grey" | ||||
|         app:itemRippleColor="@color/orange" | ||||
|         app:labelVisibilityMode="unlabeled" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintHorizontal_bias="0.0" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:menu="@menu/bottom_menu_admin" | ||||
|         android:background="@color/white" | ||||
|         app:itemIconTint="@color/grey" | ||||
|         app:labelVisibilityMode="unlabeled" | ||||
|         app:itemRippleColor="@color/orange" | ||||
|         app:itemActiveIndicatorStyle="@color/orange" /> | ||||
| </FrameLayout> | ||||
|         app:menu="@menu/bottom_menu_admin" /> | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
							
								
								
									
										94
									
								
								app/src/main/res/layout/fragment_admin.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								app/src/main/res/layout/fragment_admin.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,94 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:tools="http://schemas.android.com/tools"> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:text="@string/found_user" | ||||
|         android:textColor="@color/black" | ||||
|         android:textSize="20sp" | ||||
|         app:layout_constraintBottom_toTopOf="@+id/cardView" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         app:layout_constraintVertical_bias="1.0" /> | ||||
| 
 | ||||
|     <androidx.cardview.widget.CardView | ||||
|         android:id="@+id/cardView" | ||||
|         android:layout_width="270dp" | ||||
|         android:layout_height="72dp" | ||||
|         android:layout_marginTop="68dp" | ||||
|         android:outlineSpotShadowColor="@android:color/transparent" | ||||
|         app:cardBackgroundColor="@color/grey_light" | ||||
|         app:cardCornerRadius="10dp" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintHorizontal_bias="0.262" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent"> | ||||
| 
 | ||||
|         <androidx.cardview.widget.CardView | ||||
|             android:layout_width="240dp" | ||||
|             android:layout_height="35dp" | ||||
|             android:layout_marginStart="15dp" | ||||
|             android:layout_marginTop="18dp" | ||||
|             android:layout_marginEnd="28dp" | ||||
|             android:outlineSpotShadowColor="@android:color/transparent" | ||||
|             app:cardBackgroundColor="@color/white" | ||||
|             app:cardCornerRadius="10dp"> | ||||
| 
 | ||||
|             <EditText | ||||
|                 android:id="@+id/userLogin" | ||||
|                 android:layout_width="205dp" | ||||
|                 android:layout_height="33dp" | ||||
|                 android:layout_marginLeft="13dp" | ||||
|                 android:background="@android:color/transparent" | ||||
|                 android:gravity="center_vertical" | ||||
|                 android:hint="@string/login" | ||||
|                 android:inputType="textEmailAddress" | ||||
|                 android:maxLength="30" | ||||
|                 android:maxLines="1" | ||||
|                 android:textColorHint="@color/grey" | ||||
|                 android:textSize="12sp"> | ||||
| 
 | ||||
|             </EditText> | ||||
| 
 | ||||
|         </androidx.cardview.widget.CardView> | ||||
| 
 | ||||
| 
 | ||||
|     </androidx.cardview.widget.CardView> | ||||
| 
 | ||||
|     <com.google.android.material.button.MaterialButton | ||||
|         android:id="@+id/find_button" | ||||
|         android:layout_width="100dp" | ||||
|         android:layout_height="42dp" | ||||
|         android:layout_marginTop="16dp" | ||||
|         android:backgroundTint="@color/orange" | ||||
|         android:textSize="16sp" | ||||
|         app:circularflow_defaultRadius="10dp" | ||||
|         app:circularflow_radiusInDP="1dp" | ||||
|         app:cornerRadius="10dp" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintHorizontal_bias="0.498" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toBottomOf="@+id/cardView" /> | ||||
| 
 | ||||
|     <TextView | ||||
|         android:elevation="10dp" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:gravity="center_vertical" | ||||
|         android:text="@string/find" | ||||
|         android:textColor="@color/white" | ||||
|         android:textSize="16sp" | ||||
|         app:layout_constraintBottom_toBottomOf="@+id/find_button" | ||||
|         app:layout_constraintEnd_toEndOf="@+id/find_button" | ||||
|         app:layout_constraintStart_toStartOf="@+id/find_button" | ||||
|         app:layout_constraintTop_toTopOf="@+id/find_button"> | ||||
| 
 | ||||
|     </TextView> | ||||
| 
 | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
| @ -31,8 +31,8 @@ | ||||
|         app:layout_constraintTop_toTopOf="parent"> | ||||
| 
 | ||||
|         <androidx.cardview.widget.CardView | ||||
|             android:layout_width="244dp" | ||||
|             android:layout_height="50dp" | ||||
|             android:layout_width="249dp" | ||||
|             android:layout_height="45dp" | ||||
|             android:layout_marginStart="28dp" | ||||
|             android:layout_marginTop="20dp" | ||||
|             android:layout_marginEnd="28dp" | ||||
|  | ||||
| @ -2,7 +2,8 @@ | ||||
| <navigation 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:id="@+id/admin_main_nav_graph"> | ||||
|     android:id="@+id/admin_main_nav_graph" | ||||
|     app:startDestination="@id/fragment_profile"> | ||||
| 
 | ||||
|     <fragment | ||||
|         android:id="@+id/fragment_profile" | ||||
| @ -35,5 +36,12 @@ | ||||
|             android:id="@+id/action_fragment_qr_result_to_fragment_profile" | ||||
|             app:destination="@id/fragment_profile" /> | ||||
|     </fragment> | ||||
|     <fragment | ||||
|         android:id="@+id/fragment_admin" | ||||
|         android:name="ru.myitschool.work.ui.admin.AdminFragment" | ||||
|         android:label="Admin" | ||||
|         tools:layout="@layout/fragment_admin"> | ||||
| 
 | ||||
|     </fragment> | ||||
| 
 | ||||
| </navigation> | ||||
| @ -18,4 +18,6 @@ | ||||
|     <string name="qr_null_result">Вход был отменён</string> | ||||
|     <string name="profile">Профиль</string> | ||||
|     <string name="user_view">Просмотр пользователя</string> | ||||
|     <string name="found_user">Найти информацию о сотруднике</string> | ||||
|     <string name="find">Найти</string> | ||||
| </resources> | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user