Детекция админа на вьюхе

This commit is contained in:
v228a 2025-02-19 14:21:07 +03:00
parent eb690b6908
commit 5dca912277
5 changed files with 71 additions and 10 deletions

View File

@ -1,6 +1,6 @@
package ru.myitschool.work.core
object Constants {
// "http://localhost:8080"
const val SERVER_ADDRESS = "http://10.0.2.2:8080"
// const val SERVER_ADDRESS = "https://nto.den4iksop.org"
}

View File

@ -6,17 +6,22 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import ru.myitschool.work.R
import ru.myitschool.work.databinding.FragmentEntryListBinding
import ru.myitschool.work.databinding.FragmentLoginBinding
import ru.myitschool.work.ui.login.LoginDestination
import ru.myitschool.work.ui.login.LoginViewModel
import ru.myitschool.work.ui.profile.ProfileDestination
import ru.myitschool.work.ui.profile.ProfileViewModel
import ru.myitschool.work.utils.collectWhenStarted
class EntryListFragment : Fragment(R.layout.fragment_entry_list) {
private var _binding: FragmentEntryListBinding? = null
private val binding: FragmentEntryListBinding get() = _binding!!
private val viewModel: LoginViewModel by viewModels()
private val viewModel: EntryListViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -26,8 +31,20 @@ class EntryListFragment : Fragment(R.layout.fragment_entry_list) {
}
private fun initCallback (){
binding.floatingActionButton2.setOnClickListener { viewModel.closeEntryList() }
}
private fun subscribe(){
viewModel.action.collectWhenStarted(this){ action ->
when(action) {
is EntryListViewModel.Action.OpenProfile -> {
findNavController().navigate(LoginDestination) {
popUpTo<ProfileDestination> { inclusive = true }
}
}
}
}
private fun subscribe(){}
}
}

View File

@ -1,7 +1,46 @@
package ru.myitschool.work.ui.entrylist
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import ru.myitschool.work.ui.profile.ProfileViewModel
import ru.myitschool.work.ui.profile.ProfileViewModel.Action
import ru.myitschool.work.ui.profile.ProfileViewModel.State
import ru.myitschool.work.utils.MutablePublishFlow
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject
class EntryListViewModel : ViewModel() {
// TODO: Implement the ViewModel
@HiltViewModel
class EntryListViewModel @Inject constructor(
@ApplicationContext private val context: Context,
) : ViewModel() {
private val _action = MutablePublishFlow<Action>()
val action = _action.asSharedFlow()
private val _state = MutableStateFlow<State>(initialState)
val state = _state.asStateFlow()
fun closeEntryList(){
viewModelScope.launch {
_action.emit(Action.OpenProfile)
}
}
sealed interface Action {
data object OpenProfile: Action
}
companion object {
val initialState = State.Loading
}
}

View File

@ -57,9 +57,13 @@ class ProfileFragment : Fragment(R.layout.fragment_profile) {
}
is ProfileViewModel.State.Show -> {
swipeRefreshLayout.isRefreshing = false
binding.fullname.text = state.fullname
binding.position.text = state.position
binding.lastEntry.text = state.lastEntry
if (state.admin){
binding.fullname.text = state.fullname + " admin"
}else{
binding.fullname.text = state.fullname
}
Picasso.get()
.load(state.imageUrl)
.error(R.drawable.ic_no_img)

View File

@ -66,7 +66,8 @@ class ProfileViewModel @Inject constructor(
fullname = value.fullname,
imageUrl = value.imageUrl,
position = value.position,
lastEntry = simpleDateFormat.format(Date(value.lastEntryMillis))
lastEntry = simpleDateFormat.format(Date(value.lastEntryMillis)),
admin = false
)
}
},
@ -84,7 +85,6 @@ class ProfileViewModel @Inject constructor(
sealed interface State {
data object Loading : State
data class Error(
val errorText: String,
) : State
@ -94,6 +94,7 @@ class ProfileViewModel @Inject constructor(
val imageUrl: String,
val position: String,
val lastEntry: String,
val admin: Boolean,
) : State
}
@ -103,7 +104,7 @@ class ProfileViewModel @Inject constructor(
data object OpenLog : Action
}
private companion object {
companion object {
private val simpleDateFormat = SimpleDateFormat(
"yyyy-MM-DD HH:mm",
Locale.US