nav
This commit is contained in:
parent
a39fc9ab15
commit
a45d21216b
@ -0,0 +1,12 @@
|
||||
package ru.myitschool.work.data.dto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class RoomBookingsDto(
|
||||
@SerialName("name")
|
||||
val name: String,
|
||||
@SerialName("data")
|
||||
val data: Map<String, String?>
|
||||
)
|
||||
@ -8,10 +8,10 @@ import ru.myitschool.work.domain.room.entities.RoomEntity
|
||||
class RoomBookingRepository(
|
||||
private val authRepository: AuthRepository
|
||||
) {
|
||||
suspend fun getRoomBookings(): Result<RoomEntity> {
|
||||
suspend fun getRoomBookings(roomId: Int): Result<RoomEntity> {
|
||||
val token = authRepository.getToken() ?: return getNoAuthResult()
|
||||
return NetworkDataSource.getRoomBookings(token).mapCatching { dto ->
|
||||
RoomEntity(dto)
|
||||
return NetworkDataSource.getRoomBookings(token, roomId = roomId).mapCatching { dto ->
|
||||
RoomEntity(name=dto.name, data=dto.data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,8 +23,10 @@ import ru.myitschool.work.data.dto.AuthResponseDto
|
||||
import ru.myitschool.work.data.dto.PlaceDto
|
||||
import ru.myitschool.work.data.dto.BookRequestDto
|
||||
import ru.myitschool.work.data.dto.RoomBookingRequestDto
|
||||
import ru.myitschool.work.data.dto.RoomBookingsDto
|
||||
import ru.myitschool.work.data.dto.UserDto
|
||||
import ru.myitschool.work.data.repo.AuthRepository
|
||||
import ru.myitschool.work.domain.room.entities.RoomEntity
|
||||
|
||||
object NetworkDataSource {
|
||||
private val client by lazy {
|
||||
@ -116,15 +118,15 @@ object NetworkDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getRoomBookings(token: String): Result<Map<String, String?>> = withContext(Dispatchers.IO) {
|
||||
suspend fun getRoomBookings(token: String, roomId: Int): Result<RoomBookingsDto> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
val response = client.get(getUrl(Constants.ROOM_BOOKING_URL)) {
|
||||
val response = client.get(getUrl("$Constants.ROOM_BOOKING_URL/$roomId")) {
|
||||
headers {
|
||||
append("Authorization", "Bearer $token")
|
||||
}
|
||||
}
|
||||
if (response.status == HttpStatusCode.OK) {
|
||||
response.body<Map<String, String?>>()
|
||||
response.body<RoomBookingsDto>()
|
||||
} else {
|
||||
if (response.status == HttpStatusCode.Unauthorized) {
|
||||
AuthRepository.logout()
|
||||
|
||||
@ -6,7 +6,7 @@ import ru.myitschool.work.domain.room.entities.RoomEntity
|
||||
class GetRoomBookingsUseCase(
|
||||
private val repository: RoomBookingRepository
|
||||
) {
|
||||
suspend operator fun invoke(): Result<RoomEntity> {
|
||||
return repository.getRoomBookings()
|
||||
suspend operator fun invoke(roomId: Int): Result<RoomEntity> {
|
||||
return repository.getRoomBookings(roomId = roomId)
|
||||
}
|
||||
}
|
||||
@ -2,5 +2,6 @@ package ru.myitschool.work.domain.room.entities
|
||||
|
||||
|
||||
class RoomEntity (
|
||||
val name: String,
|
||||
val data: Map<String, String?>
|
||||
)
|
||||
@ -3,4 +3,4 @@ package ru.myitschool.work.ui.nav
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data object RoomScreenDestination: AppDestination
|
||||
data class RoomScreenDestination(val roomId: Int): AppDestination
|
||||
@ -10,9 +10,12 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import androidx.navigation.toRoute
|
||||
import ru.myitschool.work.data.repo.AuthRepository
|
||||
import ru.myitschool.work.domain.auth.GetTokenUseCase
|
||||
import ru.myitschool.work.domain.room.GetRoleUseCase
|
||||
@ -35,13 +38,13 @@ fun AppNavHost(
|
||||
LaunchedEffect(Unit) {
|
||||
val code = GetTokenUseCase(AuthRepository).invoke()
|
||||
val role = GetRoleUseCase(AuthRepository).invoke()
|
||||
destination = if (code == null) {
|
||||
destination = (if (code == null) {
|
||||
AuthScreenDestination
|
||||
} else if (role == "room") {
|
||||
} else if (role == "ROOM") {
|
||||
RoomScreenDestination
|
||||
} else {
|
||||
MainScreenDestination
|
||||
}
|
||||
}) as AppDestination?
|
||||
}
|
||||
if (destination != null) {
|
||||
NavHost(
|
||||
@ -60,8 +63,12 @@ fun AppNavHost(
|
||||
composable<BookScreenDestination> {
|
||||
BookScreen(navController = navController)
|
||||
}
|
||||
composable<RoomScreenDestination> {
|
||||
RoomScreen(navController = navController)
|
||||
composable<RoomScreenDestination> { backStackEntry ->
|
||||
val room: RoomScreenDestination = backStackEntry.toRoute()
|
||||
RoomScreen(
|
||||
roomId = room.roomId,
|
||||
navController = navController
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package ru.myitschool.work.ui.screen.room
|
||||
|
||||
sealed interface RoomIntent {
|
||||
data object Refresh: RoomIntent
|
||||
data class Refresh(val placeId: Int): RoomIntent
|
||||
data class Booking(val placeId: Int, val date: String): RoomIntent
|
||||
data class UnBook(val placeId: Int, val date: String): RoomIntent
|
||||
}
|
||||
@ -7,6 +7,7 @@ import ru.myitschool.work.ui.screen.auth.AuthViewModel
|
||||
|
||||
@Composable
|
||||
fun RoomScreen(
|
||||
roomId: Int,
|
||||
viewModel: AuthViewModel = viewModel(),
|
||||
navController: NavController
|
||||
) {
|
||||
|
||||
@ -9,6 +9,7 @@ sealed interface RoomState {
|
||||
val error: String
|
||||
): RoomState
|
||||
data class Data(
|
||||
val data: RoomEntity
|
||||
val data: Map<String, String?>,
|
||||
val name: String
|
||||
): RoomState
|
||||
}
|
||||
@ -1,7 +1,10 @@
|
||||
package ru.myitschool.work.ui.screen.room
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.navigation.toRoute
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@ -12,8 +15,14 @@ import ru.myitschool.work.data.repo.RoomBookingRepository
|
||||
import ru.myitschool.work.domain.room.DeleteRoomBookingsUseCase
|
||||
import ru.myitschool.work.domain.room.GetRoomBookingsUseCase
|
||||
import ru.myitschool.work.domain.room.SendRoomBookingRequestUseCase
|
||||
import ru.myitschool.work.ui.nav.RoomScreenDestination
|
||||
|
||||
class RoomViewModel : ViewModel() {
|
||||
|
||||
class RoomViewModel(
|
||||
savedStateHandle: SavedStateHandle,
|
||||
): ViewModel() {
|
||||
|
||||
private val roomScreen = savedStateHandle.toRoute<RoomScreenDestination>()
|
||||
|
||||
private val roomBookingRepository = RoomBookingRepository(
|
||||
AuthRepository
|
||||
@ -40,13 +49,18 @@ class RoomViewModel : ViewModel() {
|
||||
val uiState: StateFlow<RoomState> = _uiState.asStateFlow()
|
||||
|
||||
init {
|
||||
refresh()
|
||||
refresh(roomScreen.roomId)
|
||||
// Timer().schedule(object : TimerTask() {
|
||||
// override fun run() {
|
||||
// Log.d("mytimer", "A Kiss every 5 seconds")
|
||||
// }
|
||||
// }, 0, 5000)
|
||||
}
|
||||
|
||||
fun onIntent(intent: RoomIntent) {
|
||||
when (intent) {
|
||||
is RoomIntent.Refresh -> {
|
||||
refresh()
|
||||
refresh(intent.placeId)
|
||||
}
|
||||
|
||||
is RoomIntent.Booking -> {
|
||||
@ -56,11 +70,12 @@ class RoomViewModel : ViewModel() {
|
||||
intent.date
|
||||
).fold(
|
||||
onSuccess = {
|
||||
// _actionFlow.emit(BookAction.BackWithSuccess)
|
||||
refresh()
|
||||
refresh(intent.placeId)
|
||||
},
|
||||
onFailure = { error ->
|
||||
error.printStackTrace()
|
||||
RoomState.Error(
|
||||
error = error.message.orEmpty()
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -73,11 +88,12 @@ class RoomViewModel : ViewModel() {
|
||||
intent.date
|
||||
).fold(
|
||||
onSuccess = {
|
||||
// _actionFlow.emit(BookAction.BackWithSuccess)
|
||||
refresh()
|
||||
refresh(intent.placeId)
|
||||
},
|
||||
onFailure = { error ->
|
||||
error.printStackTrace()
|
||||
RoomState.Error(
|
||||
error = error.message.orEmpty()
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -85,14 +101,15 @@ class RoomViewModel : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
private fun refresh(roomId: Int) {
|
||||
viewModelScope.launch {
|
||||
_uiState.update { RoomState.Loading }
|
||||
_uiState.update {
|
||||
getRoomBookingsDataUseCase.invoke().fold(
|
||||
getRoomBookingsDataUseCase.invoke(roomId = roomId).fold(
|
||||
onSuccess = { data ->
|
||||
RoomState.Data(
|
||||
data = data
|
||||
data = data.data,
|
||||
name = data.name
|
||||
)
|
||||
},
|
||||
onFailure = { error ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user