fix profile
This commit is contained in:
parent
8097e200c7
commit
212287a89a
@ -16,7 +16,7 @@ import ru.myitschool.work.data.auth.Network.client
|
|||||||
|
|
||||||
class UserNetworkDataSource {
|
class UserNetworkDataSource {
|
||||||
suspend fun getUser(login : String): Result<UserDto> = withContext(Dispatchers.IO) {
|
suspend fun getUser(login : String): Result<UserDto> = withContext(Dispatchers.IO) {
|
||||||
//runCatching {
|
runCatching {
|
||||||
|
|
||||||
val result = client.get("$SERVER_ADDRESS/api/${login}/info") {
|
val result = client.get("$SERVER_ADDRESS/api/${login}/info") {
|
||||||
header(HttpHeaders.Authorization, token)
|
header(HttpHeaders.Authorization, token)
|
||||||
@ -31,7 +31,7 @@ class UserNetworkDataSource {
|
|||||||
}
|
}
|
||||||
Log.d("result", result.bodyAsText())
|
Log.d("result", result.bodyAsText())
|
||||||
result.body()
|
result.body()
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getEntrancesList(login : String) : Result<List<EntranceDto>> = withContext(Dispatchers.IO){
|
suspend fun getEntrancesList(login : String) : Result<List<EntranceDto>> = withContext(Dispatchers.IO){
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
package ru.myitschool.work.domain.auth
|
|
||||||
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
|
|
||||||
/*class IsUserExistUseCase (
|
|
||||||
private val authRepo : AuthRepo
|
|
||||||
) {
|
|
||||||
suspend operator fun invoke(email : String) : Result<Boolean?> {
|
|
||||||
return authRepo.isUserExist(email)
|
|
||||||
}
|
|
||||||
}*/
|
|
@ -49,17 +49,17 @@ class RootActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*onBackPressedDispatcher.addCallback(
|
onBackPressedDispatcher.addCallback(
|
||||||
this,
|
this,
|
||||||
object : OnBackPressedCallback(true) {
|
object : OnBackPressedCallback(true) {
|
||||||
override fun handleOnBackPressed() {
|
override fun handleOnBackPressed() {
|
||||||
onSupportNavigateUp()
|
onSupportNavigateUp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)*/
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*override fun onSupportNavigateUp(): Boolean {
|
override fun onSupportNavigateUp(): Boolean {
|
||||||
val navController = findNavController(R.id.nav_host_fragment)
|
val navController = findNavController(R.id.nav_host_fragment)
|
||||||
val popBackResult = if (navController.previousBackStackEntry != null) {
|
val popBackResult = if (navController.previousBackStackEntry != null) {
|
||||||
navController.popBackStack()
|
navController.popBackStack()
|
||||||
@ -67,6 +67,6 @@ class RootActivity : AppCompatActivity() {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
return popBackResult || super.onSupportNavigateUp()
|
return popBackResult || super.onSupportNavigateUp()
|
||||||
}*/
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -48,6 +48,8 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
|
|||||||
viewBinding.loading.visibility = if (state is ProfileViewModel.State.Loading) View.VISIBLE else View.GONE
|
viewBinding.loading.visibility = if (state is ProfileViewModel.State.Loading) View.VISIBLE else View.GONE
|
||||||
viewBinding.profile.visibility = if (state is ProfileViewModel.State.Show) View.VISIBLE else View.GONE
|
viewBinding.profile.visibility = if (state is ProfileViewModel.State.Show) View.VISIBLE else View.GONE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
when(state) {
|
when(state) {
|
||||||
is ProfileViewModel.State.Loading -> Unit
|
is ProfileViewModel.State.Loading -> Unit
|
||||||
is ProfileViewModel.State.Show -> {
|
is ProfileViewModel.State.Show -> {
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
package ru.myitschool.work.ui.qr.result
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.viewModels
|
||||||
|
import androidx.navigation.fragment.findNavController
|
||||||
|
import com.squareup.picasso.Picasso
|
||||||
|
import ru.myitschool.work.R
|
||||||
|
import ru.myitschool.work.databinding.FragmentProfileBinding
|
||||||
|
import ru.myitschool.work.databinding.FragmentQrResultBinding
|
||||||
|
import ru.myitschool.work.ui.profile.ProfileViewModel
|
||||||
|
import ru.myitschool.work.ui.qr.scan.QrScanDestination
|
||||||
|
import ru.myitschool.work.utils.collectWithLifecycle
|
||||||
|
|
||||||
|
class QrResultFragment : Fragment(R.layout.fragment_qr_result) {
|
||||||
|
private var _viewBinding: FragmentQrResultBinding? = null
|
||||||
|
private val viewBinding: FragmentQrResultBinding get() = _viewBinding!!
|
||||||
|
|
||||||
|
private val viewModel by viewModels<QrResultViewModel> { QrResultViewModel.Factory }
|
||||||
|
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
_viewBinding = FragmentQrResultBinding.bind(view)
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
|
parentFragmentManager.setFragmentResultListener(
|
||||||
|
QrScanDestination.REQUEST_KEY, viewLifecycleOwner
|
||||||
|
) {
|
||||||
|
key, bundle ->
|
||||||
|
if (key == QrScanDestination.REQUEST_KEY) {
|
||||||
|
val code = QrScanDestination.getDataIfExist(bundle)
|
||||||
|
if (code != null) {
|
||||||
|
viewModel.sendResult(code.toLong())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
viewBinding.close.setOnClickListener {
|
||||||
|
findNavController().navigate(R.id.action_fragment_qr_result_to_fragment_profile)
|
||||||
|
}
|
||||||
|
|
||||||
|
viewModel.state.collectWithLifecycle(this) { state ->
|
||||||
|
|
||||||
|
viewBinding.error.visibility = if (state is QrResultViewModel.State.Error) View.VISIBLE else View.GONE
|
||||||
|
viewBinding.loading.visibility = if (state is QrResultViewModel.State.Loading) View.VISIBLE else View.GONE
|
||||||
|
viewBinding.result.visibility = if (state is QrResultViewModel.State.Show) View.VISIBLE else View.GONE
|
||||||
|
|
||||||
|
|
||||||
|
when(state) {
|
||||||
|
is QrResultViewModel.State.Loading -> Unit
|
||||||
|
is QrResultViewModel.State.Show -> {
|
||||||
|
viewBinding.result.text = state.result
|
||||||
|
|
||||||
|
}
|
||||||
|
is QrResultViewModel.State.Error -> {
|
||||||
|
viewBinding.errorText.text = state.text
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
_viewBinding = null
|
||||||
|
super.onDestroyView()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
|||||||
|
package ru.myitschool.work.ui.qr.result
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import androidx.lifecycle.viewmodel.CreationExtras
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import ru.myitschool.work.R
|
||||||
|
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.EnterUseCase
|
||||||
|
import ru.myitschool.work.domain.user.EntranceEntity
|
||||||
|
import ru.myitschool.work.domain.user.GetUserUseCase
|
||||||
|
import ru.myitschool.work.domain.user.UserEntity
|
||||||
|
|
||||||
|
class QrResultViewModel(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val enterUseCase: EnterUseCase
|
||||||
|
) : ViewModel() {
|
||||||
|
private val _state = MutableStateFlow<State>(State.Loading)
|
||||||
|
val state = _state.asStateFlow()
|
||||||
|
|
||||||
|
fun updateState(code: Long?) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_state.emit(State.Loading)
|
||||||
|
if (code == null) {
|
||||||
|
_state.emit(State.Error(context.getString(R.string.qr_null_result)))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_state.emit(
|
||||||
|
enterUseCase.invoke(code).fold(
|
||||||
|
onSuccess = { data ->
|
||||||
|
Log.d("uraa", "успех успех ${data.toString()}")
|
||||||
|
when (data) {
|
||||||
|
true -> State.Show(context.getString(R.string.qr_success_result))
|
||||||
|
false -> State.Show(context.getString(R.string.qr_wrong_result))
|
||||||
|
null -> State.Show(context.getString(R.string.qr_wrong_result))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onFailure = { error ->
|
||||||
|
Log.d("kaput", error.message.toString())
|
||||||
|
State.Error(error.message.toString())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
//_state.emit(State.Error("о нет ошибка ошибка помогите"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun sendResult(code : Long) {
|
||||||
|
updateState(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
sealed interface State {
|
||||||
|
data object Loading: State
|
||||||
|
data class Show(
|
||||||
|
val result : String
|
||||||
|
) : 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>, extras: CreationExtras): T {
|
||||||
|
val application = extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]!!
|
||||||
|
return QrResultViewModel(
|
||||||
|
context = application,
|
||||||
|
enterUseCase = EnterUseCase(
|
||||||
|
repo = UserRepoImpl(
|
||||||
|
userNetworkDataSource = UserNetworkDataSource()
|
||||||
|
),
|
||||||
|
authStorageDataSource = AuthStorageDataSource
|
||||||
|
)
|
||||||
|
) as T
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
70
app/src/main/res/layout/fragment_qr_result.xml
Normal file
70
app/src/main/res/layout/fragment_qr_result.xml
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/result"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="20sp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
tools:text="Вход был отменён" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/close"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:src="@drawable/ic_close"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
android:clickable="true"
|
||||||
|
android:focusable="true"
|
||||||
|
android:background="@color/orange"/>
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/loading"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/error"
|
||||||
|
android:layout_width="160dp"
|
||||||
|
android:layout_height="160dp"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/errorText"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="15dp"
|
||||||
|
android:gravity="center"
|
||||||
|
tools:text="Ошибка помогите" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/refresh"
|
||||||
|
android:layout_width="45dp"
|
||||||
|
android:layout_height="45dp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:background="@drawable/ic_refresh"
|
||||||
|
android:gravity="center" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -9,6 +9,34 @@
|
|||||||
android:id="@+id/fragment_profile"
|
android:id="@+id/fragment_profile"
|
||||||
android:name="ru.myitschool.work.ui.profile.ProfileFragment"
|
android:name="ru.myitschool.work.ui.profile.ProfileFragment"
|
||||||
android:label="Profile"
|
android:label="Profile"
|
||||||
tools:layout="@layout/fragment_profile" />
|
tools:layout="@layout/fragment_profile">
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_fragment_profile_to_fragment_qr"
|
||||||
|
app:destination="@id/fragment_qr" />
|
||||||
|
</fragment>
|
||||||
|
|
||||||
|
<fragment
|
||||||
|
android:id="@+id/fragment_qr"
|
||||||
|
android:name="ru.myitschool.work.ui.qr.scan.QrScanFragment"
|
||||||
|
android:label="QrScan"
|
||||||
|
tools:layout="@layout/fragment_qr_scan" >
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_fragment_qr_to_fragment_qr_result"
|
||||||
|
app:destination="@id/fragment_qr_result" />
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_fragment_qr_to_fragment_profile"
|
||||||
|
app:destination="@id/fragment_profile" />
|
||||||
|
</fragment>
|
||||||
|
<fragment
|
||||||
|
android:id="@+id/fragment_qr_result"
|
||||||
|
android:name="ru.myitschool.work.ui.qr.result.QrResultFragment"
|
||||||
|
android:label="QrResult"
|
||||||
|
tools:layout="@layout/fragment_qr_result" >
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_fragment_qr_result_to_fragment_profile"
|
||||||
|
app:destination="@id/fragment_profile" />
|
||||||
|
</fragment>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</navigation>
|
</navigation>
|
@ -13,4 +13,7 @@
|
|||||||
<string name="entry">Вход</string>
|
<string name="entry">Вход</string>
|
||||||
<string name="type_of_entrance">Тип прохода</string>
|
<string name="type_of_entrance">Тип прохода</string>
|
||||||
<string name="no_data">Нет данных\n</string>
|
<string name="no_data">Нет данных\n</string>
|
||||||
|
<string name="qr_success_result">Успешно</string>
|
||||||
|
<string name="qr_wrong_result">Что-то пошло не так</string>
|
||||||
|
<string name="qr_null_result">Вход был отменён</string>
|
||||||
</resources>
|
</resources>
|
Loading…
x
Reference in New Issue
Block a user