ban nemnogo ploho
This commit is contained in:
parent
0c722d75cf
commit
e87cefea93
@ -6,6 +6,7 @@ import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.PATCH
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Path
|
||||
import retrofit2.http.Query
|
||||
|
||||
@ -22,6 +23,17 @@ interface LoginApi {
|
||||
@Query("login") login: String
|
||||
): Response<ResponseBody>
|
||||
|
||||
@PUT("ban")
|
||||
suspend fun ban(
|
||||
@Query("login") login: String
|
||||
): Response<ResponseBody>
|
||||
|
||||
@PUT("unban")
|
||||
suspend fun unban(
|
||||
@Query("login") login: String
|
||||
): Response<ResponseBody>
|
||||
|
||||
|
||||
@PATCH("{login}/open")
|
||||
suspend fun open(@Path("login") login: String, @Body request: OpenWithCodeRequest): Response<ResponseBody>
|
||||
}
|
||||
|
@ -42,7 +42,6 @@ class AdminFragment : Fragment(R.layout.fragment_admin) {
|
||||
binding.loginadmin.addTextChangedListener(TextChangedListener {
|
||||
viewModel.onUsernameChanged(it)
|
||||
})
|
||||
subscribe()
|
||||
binding.adminName.text = user
|
||||
binding.find.setOnClickListener {
|
||||
login(user, password, binding.loginadmin.text.toString())
|
||||
@ -63,41 +62,36 @@ class AdminFragment : Fragment(R.layout.fragment_admin) {
|
||||
if (state.photo.isNotEmpty()) {
|
||||
picasso.load(state.photo).into(binding.photoAdmin)
|
||||
}
|
||||
if (state.isBanned){
|
||||
binding.ban.text = "разблокировать"
|
||||
}else{
|
||||
binding.ban.text = "заблокировать"
|
||||
}
|
||||
binding.ban.setOnClickListener {
|
||||
viewModel.banPerson(username, state.isBanned, loginPerson){
|
||||
if (state.isBanned){
|
||||
binding.ban.text = "разблокировать"
|
||||
}else{
|
||||
binding.ban.text = "заблокировать"
|
||||
}
|
||||
}
|
||||
}
|
||||
binding.fullnameAdmin.text = state.fullName
|
||||
binding.positionAdmin.text = state.position
|
||||
binding.lastEntryAdmin.text = state.lastVisit
|
||||
binding.errorAdmin.text = state.error
|
||||
setError(state.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun subscribe() {
|
||||
// viewModel.state.collectWhenStarted(this) { state ->
|
||||
// binding.login.isEnabled = state.isLoginEnabled
|
||||
// if (state.error != null) {
|
||||
// binding.error.visibility = View.VISIBLE
|
||||
// binding.error.text = state.error
|
||||
// } else {
|
||||
// binding.error.visibility = View.GONE
|
||||
// }
|
||||
// }
|
||||
// viewModel.savedUsername.collectWhenStarted(this) { username ->
|
||||
// viewModel.savedPassword.collectWhenStarted(this) { password ->
|
||||
// if (!username.isNullOrBlank() && !password.isNullOrBlank()) {
|
||||
// login(username, password)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
println("akjshdfg")
|
||||
}
|
||||
|
||||
|
||||
private fun setError(error: String?) {
|
||||
val showError = error != null
|
||||
val views = listOf(binding.fullnameAdmin, binding.positionAdmin, binding.lastEntryAdmin, binding.photoAdmin)
|
||||
val views = listOf(binding.fullnameAdmin, binding.ban, binding.positionAdmin, binding.lastEntryAdmin, binding.photoAdmin)
|
||||
views.forEach { it.visibility = if (showError) View.GONE else View.VISIBLE}
|
||||
binding.errorAdmin.visibility = if (showError) View.VISIBLE else View.GONE
|
||||
binding.block.visibility = if (showError) View.GONE else View.VISIBLE
|
||||
binding.errorAdmin.text = error ?: ""
|
||||
}
|
||||
|
||||
|
@ -9,5 +9,7 @@ data class AdminMainState(
|
||||
val photo: String = "",
|
||||
val position: String = "",
|
||||
val lastVisit: String = "",
|
||||
val role: String = "",
|
||||
val isBanned: Boolean = false,
|
||||
val error: String? = null
|
||||
)
|
||||
|
@ -56,6 +56,8 @@ class AdminViewModel @Inject constructor(
|
||||
photo = personInfo.photo,
|
||||
position = personInfo.position,
|
||||
lastVisit = dfo.format(dfi.parse(personInfo.lastVisit)!!),
|
||||
role = personInfo.role,
|
||||
isBanned = personInfo.isBanned,
|
||||
error = null
|
||||
)
|
||||
}
|
||||
@ -81,6 +83,34 @@ class AdminViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun banPerson(username: String, ban : Boolean, loginPerson: String, onSuccess: () -> Unit) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
when(ban){
|
||||
false -> loginApi.ban(loginPerson)
|
||||
true -> loginApi.unban(loginPerson)
|
||||
}
|
||||
onSuccess()
|
||||
} catch (httpException: HttpException) {
|
||||
try {
|
||||
httpException.response()?.errorBody()?.string()?.let { errorString ->
|
||||
val gson = GsonBuilder().create()
|
||||
val errorDto = gson.fromJson(errorString, ErrorDto::class.java)
|
||||
_state.update {
|
||||
AdminMainState(
|
||||
error = errorDto.error
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_state.update { AdminMainState(error = httpException.message()) }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_state.update { AdminMainState(error = "Unknown error: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onUsernameChanged(username: String) =
|
||||
_stateL.update { it.copy(isLoginEnabled = isUsernameValid(username), error = null) }
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
app:layout_constraintVertical_bias="0.089" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/block"
|
||||
android:id="@+id/ban"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
@ -28,7 +28,8 @@
|
||||
app:layout_constraintEnd_toEndOf="@+id/loginadmin"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
app:layout_constraintStart_toStartOf="@+id/loginadmin"
|
||||
app:layout_constraintTop_toBottomOf="@+id/loginadmin" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/loginadmin"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/errorAdmin"
|
||||
@ -40,7 +41,8 @@
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/block" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/ban"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/lastEntryAdmin"
|
||||
@ -53,7 +55,8 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.501"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/positionAdmin" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/positionAdmin"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/fullnameAdmin"
|
||||
@ -66,7 +69,8 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/errorAdmin"
|
||||
app:layout_constraintVertical_bias="0.033" />
|
||||
app:layout_constraintVertical_bias="0.033"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/positionAdmin"
|
||||
@ -79,7 +83,21 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.501"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/photoAdmin" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/photoAdmin"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/photoAdmin"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.501"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/fullnameAdmin"
|
||||
tools:srcCompat="@tools:sample/avatars"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="409dp"
|
||||
@ -94,18 +112,6 @@
|
||||
android:orientation="vertical" />
|
||||
</ScrollView>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/photoAdmin"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.501"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/fullnameAdmin"
|
||||
tools:srcCompat="@tools:sample/avatars" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/loginadmin"
|
||||
android:layout_width="164dp"
|
||||
|
Loading…
x
Reference in New Issue
Block a user