Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ae2cd0f4f | ||
|
|
30ecbb6008 | ||
|
|
2ddf6ec534 | ||
|
|
3e97a07e76 | ||
|
|
dd25a33b22 | ||
|
|
309436d3d9 | ||
|
|
be79413efa | ||
|
|
3f51458863 | ||
|
|
b70cdbff56 | ||
|
|
b1a0bfab92 | ||
|
|
fd8309521d | ||
|
|
0c5c97d2cd |
@@ -0,0 +1,38 @@
|
|||||||
|
package ru.myitschool.work.auth.data
|
||||||
|
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.core.NetworkDataSource
|
||||||
|
import ru.myitschool.work.util.DataStoreManager
|
||||||
|
|
||||||
|
object AuthRepository {
|
||||||
|
|
||||||
|
suspend fun checkAndSaveCode(code: String): MyResult<Unit> {
|
||||||
|
return when (val result = NetworkDataSource.checkAuth(code)) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
if (result.data) {
|
||||||
|
DataStoreManager.saveAuthCode(code)
|
||||||
|
MyResult.Success(Unit)
|
||||||
|
} else {
|
||||||
|
MyResult.Error("Неверный код")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is MyResult.Error -> result
|
||||||
|
}
|
||||||
|
/*DataStoreManager.saveAuthCode(code)
|
||||||
|
return MyResult.Success(Unit)*/
|
||||||
|
}
|
||||||
|
suspend fun logOut(): MyResult<Unit> {
|
||||||
|
return try {
|
||||||
|
DataStoreManager.clearAuthCode()
|
||||||
|
MyResult.Success(Unit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
MyResult.Error("Ошибка при выходе")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun checkCode(code: String): MyResult<Boolean> =
|
||||||
|
NetworkDataSource.checkAuth(code)
|
||||||
|
|
||||||
|
suspend fun getSavedCode(): String? =
|
||||||
|
DataStoreManager.getAuthCode()
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.myitschool.work.auth.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.auth.data.AuthRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
|
||||||
|
class CheckAndSaveAuthCodeUseCase(
|
||||||
|
private val repository: AuthRepository,
|
||||||
|
) {
|
||||||
|
suspend operator fun invoke(code: String): MyResult<Unit> {
|
||||||
|
return repository.checkAndSaveCode(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.myitschool.work.auth.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.auth.data.AuthRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
|
||||||
|
class LogOutUseCase(private val repository: AuthRepository) {
|
||||||
|
suspend operator fun invoke(): MyResult<Unit>{
|
||||||
|
return repository.logOut()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package ru.myitschool.work.auth.presentation
|
||||||
|
|
||||||
|
sealed interface AuthIntent {
|
||||||
|
data object Send: AuthIntent
|
||||||
|
data class TextInput(val text: String): AuthIntent
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package ru.myitschool.work.auth.presentation
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material3.Button
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextField
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.testTag
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import ru.myitschool.work.R
|
||||||
|
import ru.myitschool.work.core.TestIds
|
||||||
|
import ru.myitschool.work.ui.Screen
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AuthScreen(
|
||||||
|
viewModel: AuthViewModel = viewModel(),
|
||||||
|
navController: NavController
|
||||||
|
) {
|
||||||
|
val state by viewModel.uiState.collectAsState()
|
||||||
|
|
||||||
|
LaunchedEffect(state.isAuthenticated) {
|
||||||
|
if (state.isAuthenticated) {
|
||||||
|
navController.navigate(Screen.Main) {
|
||||||
|
popUpTo(navController.graph.startDestinationId) { inclusive = true }
|
||||||
|
launchSingleTop = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(24.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
) {
|
||||||
|
if (state.isLoading){
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier.size(64.dp)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.auth_title),
|
||||||
|
style = MaterialTheme.typography.headlineSmall,
|
||||||
|
textAlign = TextAlign.Center
|
||||||
|
)
|
||||||
|
|
||||||
|
Spacer(Modifier.height(15.dp))
|
||||||
|
|
||||||
|
TextField(
|
||||||
|
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
|
||||||
|
value = state.code,
|
||||||
|
onValueChange = {
|
||||||
|
viewModel.onIntent(AuthIntent.TextInput(it))
|
||||||
|
},
|
||||||
|
label = { Text(stringResource(R.string.auth_label)) }
|
||||||
|
)
|
||||||
|
|
||||||
|
Text(
|
||||||
|
text = state.error?: "",
|
||||||
|
modifier = Modifier.testTag(TestIds.Auth.ERROR),
|
||||||
|
color = MaterialTheme.colorScheme.error)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Button(
|
||||||
|
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
|
||||||
|
onClick = {
|
||||||
|
viewModel.onIntent(AuthIntent.Send)
|
||||||
|
},
|
||||||
|
enabled = state.isButtonEnabled
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.auth_sign_in))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.myitschool.work.auth.presentation
|
||||||
|
|
||||||
|
data class AuthState(
|
||||||
|
val code: String = "",
|
||||||
|
val error: String? = null,
|
||||||
|
val isLoading: Boolean = false,
|
||||||
|
val isAuthenticated: Boolean = false,
|
||||||
|
val isButtonEnabled: Boolean = false
|
||||||
|
)
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package ru.myitschool.work.auth.presentation
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import ru.myitschool.work.auth.data.AuthRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.auth.domain.CheckAndSaveAuthCodeUseCase
|
||||||
|
|
||||||
|
class AuthViewModel : ViewModel() {
|
||||||
|
private val checkAndSaveAuthCodeUseCase by lazy { CheckAndSaveAuthCodeUseCase(AuthRepository) }
|
||||||
|
private val _uiState = MutableStateFlow<AuthState>(AuthState())
|
||||||
|
val uiState: StateFlow<AuthState> = _uiState.asStateFlow()
|
||||||
|
|
||||||
|
fun onIntent(intent: AuthIntent) {
|
||||||
|
when (intent) {
|
||||||
|
AuthIntent.Send -> sendCode()
|
||||||
|
is AuthIntent.TextInput -> updateCode(intent.text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun updateCode(newCode: String) {
|
||||||
|
val trimmed = newCode.trim()
|
||||||
|
val isValid = trimmed.length == 4 && trimmed.matches(Regex("^[a-zA-Z0-9]{4}$"))
|
||||||
|
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(
|
||||||
|
code = trimmed,
|
||||||
|
isButtonEnabled = isValid,
|
||||||
|
error = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sendCode() {
|
||||||
|
val currentCode = _uiState.value.code
|
||||||
|
if (currentCode.length != 4 || !currentCode.matches(Regex("^[a-zA-Z0-9]{4}$"))) return
|
||||||
|
|
||||||
|
viewModelScope.launch {
|
||||||
|
_uiState.update { it.copy(isLoading = true, error = null) }
|
||||||
|
|
||||||
|
when (val result = checkAndSaveAuthCodeUseCase(currentCode)){
|
||||||
|
is MyResult.Success -> {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(isLoading = false, isAuthenticated = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is MyResult.Error -> {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(
|
||||||
|
isLoading = false,
|
||||||
|
error = result.error,
|
||||||
|
isAuthenticated = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package ru.myitschool.work.booking.data
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
typealias AvailableBookingResponse = Map<String, List<AvailablePlace>>
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AvailablePlace(
|
||||||
|
val id: Int,
|
||||||
|
val place: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.myitschool.work.booking.data
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class BookRequest(
|
||||||
|
val date: String,
|
||||||
|
val placeID: Int
|
||||||
|
)
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package ru.myitschool.work.booking.data
|
||||||
|
|
||||||
|
import ru.myitschool.work.booking.domain.BookingData
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.core.NetworkDataSource
|
||||||
|
|
||||||
|
object BookingRepository {
|
||||||
|
suspend fun getAvailableForBooking(code: String): MyResult<BookingData> {
|
||||||
|
return when (val result = NetworkDataSource.getAvailableForBooking(code)) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
val bookingData = BookingData(
|
||||||
|
dateToPlaces = result.data
|
||||||
|
)
|
||||||
|
MyResult.Success(bookingData)
|
||||||
|
}
|
||||||
|
is MyResult.Error -> {
|
||||||
|
MyResult.Error(result.error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun createBooking(code: String, date: String, placeId: Int): MyResult<Unit> {
|
||||||
|
return NetworkDataSource.createBooking(code, date, placeId)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.myitschool.work.booking.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.booking.data.AvailablePlace
|
||||||
|
|
||||||
|
data class BookingData(
|
||||||
|
val dateToPlaces: Map<String, List<AvailablePlace>>
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.myitschool.work.booking.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.booking.data.BookingRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
|
||||||
|
class CreateBookingUseCase(
|
||||||
|
private val repository: BookingRepository
|
||||||
|
) {
|
||||||
|
suspend operator fun invoke(code: String, date: String, placeId: Int): MyResult<Unit> {
|
||||||
|
return repository.createBooking(code, date, placeId)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.myitschool.work.booking.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.booking.data.BookingRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
|
||||||
|
class GetAvailableUseCase(
|
||||||
|
private val repository: BookingRepository
|
||||||
|
) {
|
||||||
|
suspend operator fun invoke(code: String): MyResult<BookingData> {
|
||||||
|
return repository.getAvailableForBooking(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.myitschool.work.booking.presentation
|
||||||
|
|
||||||
|
sealed interface BookingIntent {
|
||||||
|
object LoadData: BookingIntent
|
||||||
|
data class SelectDate(val date: String): BookingIntent
|
||||||
|
data class SelectPlace(val placeId: Int): BookingIntent
|
||||||
|
object Book: BookingIntent
|
||||||
|
object Refresh: BookingIntent
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
package ru.myitschool.work.booking.presentation
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.*
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.selection.selectable
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material3.*
|
||||||
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.testTag
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import ru.myitschool.work.core.TestIds
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
fun BookingScreen(
|
||||||
|
onNavigateBack: () -> Unit,
|
||||||
|
onBookingSuccess: () -> Unit,
|
||||||
|
viewModel: BookingViewModel = viewModel()
|
||||||
|
) {
|
||||||
|
val state by viewModel.state.collectAsState()
|
||||||
|
|
||||||
|
LaunchedEffect(state.bookingSuccess) {
|
||||||
|
if (state.bookingSuccess) {
|
||||||
|
onBookingSuccess()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Scaffold(
|
||||||
|
topBar = {
|
||||||
|
TopAppBar(
|
||||||
|
title = { Text("Бронирование") },
|
||||||
|
navigationIcon = {
|
||||||
|
IconButton(
|
||||||
|
onClick = onNavigateBack,
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.BACK_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("<")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
) { paddingValues ->
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(paddingValues)
|
||||||
|
) {
|
||||||
|
when {
|
||||||
|
state.isLoading -> {
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier.align(Alignment.Center)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
state.error != null -> {
|
||||||
|
ErrorContent(
|
||||||
|
error = state.error!!,
|
||||||
|
onRefresh = { viewModel.onIntent(BookingIntent.Refresh) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
state.dates.isEmpty() -> {
|
||||||
|
EmptyContent(onNavigateBack = onNavigateBack)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
BookingContent(
|
||||||
|
state = state,
|
||||||
|
onIntent = { viewModel.onIntent(it) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun ErrorContent(
|
||||||
|
error: String,
|
||||||
|
onRefresh: () -> Unit
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = error,
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.ERROR)
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
Button(
|
||||||
|
onClick = onRefresh,
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.REFRESH_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Обновить")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun EmptyContent(onNavigateBack: () -> Unit) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = "Всё забронировано",
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.EMPTY)
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
Button(
|
||||||
|
onClick = onNavigateBack,
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.BACK_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Назад")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun BookingContent(
|
||||||
|
state: BookingState,
|
||||||
|
onIntent: (BookingIntent) -> Unit
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
) {
|
||||||
|
TabRow(
|
||||||
|
selectedTabIndex = state.dates.indexOf(state.selectedDate),
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
) {
|
||||||
|
state.dates.forEachIndexed { index, date ->
|
||||||
|
Tab(
|
||||||
|
selected = date == state.selectedDate,
|
||||||
|
onClick = { onIntent(BookingIntent.SelectDate(date)) },
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.getIdDateItemByPosition(index))
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = formatDateForDisplay(date),
|
||||||
|
modifier = Modifier
|
||||||
|
.testTag(TestIds.Book.ITEM_DATE)
|
||||||
|
.padding(16.dp)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(16.dp)
|
||||||
|
) {
|
||||||
|
state.availablePlaces.forEachIndexed { index, place ->
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.testTag(TestIds.Book.getIdPlaceItemByPosition(index))
|
||||||
|
.selectable(
|
||||||
|
selected = place.id == state.selectedPlaceId,
|
||||||
|
onClick = { onIntent(BookingIntent.SelectPlace(place.id)) }
|
||||||
|
)
|
||||||
|
.padding(vertical = 8.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
RadioButton(
|
||||||
|
selected = place.id == state.selectedPlaceId,
|
||||||
|
onClick = { onIntent(BookingIntent.SelectPlace(place.id)) },
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.ITEM_PLACE_SELECTOR)
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.width(8.dp))
|
||||||
|
Text(
|
||||||
|
text = place.place,
|
||||||
|
modifier = Modifier.testTag(TestIds.Book.ITEM_PLACE_TEXT)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Button(
|
||||||
|
onClick = { onIntent(BookingIntent.Book) },
|
||||||
|
enabled = state.selectedPlaceId != null && !state.isBookingInProgress,
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(16.dp)
|
||||||
|
.testTag(TestIds.Book.BOOK_BUTTON)
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = if (state.isBookingInProgress) "Бронирование..." else "Забронировать"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun formatDateForDisplay(isoDate: String): String {
|
||||||
|
return try {
|
||||||
|
val inputFormat = SimpleDateFormat("yyyy-MM-dd")
|
||||||
|
val outputFormat = SimpleDateFormat("dd.MM")
|
||||||
|
val date = inputFormat.parse(isoDate)
|
||||||
|
date?.let { outputFormat.format(it) } ?: isoDate
|
||||||
|
} catch (e: Exception) {
|
||||||
|
isoDate
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package ru.myitschool.work.booking.presentation
|
||||||
|
|
||||||
|
import ru.myitschool.work.booking.data.AvailablePlace
|
||||||
|
|
||||||
|
data class BookingState(
|
||||||
|
val isLoading: Boolean = true,
|
||||||
|
val error: String? = null,
|
||||||
|
val dates: List<String> = emptyList(),
|
||||||
|
val dateToPlacesMap: Map<String, List<AvailablePlace>> = emptyMap(),
|
||||||
|
val selectedDate: String? = null,
|
||||||
|
val availablePlaces: List<AvailablePlace> = emptyList(),
|
||||||
|
val selectedPlaceId: Int? = null,
|
||||||
|
val isBookingInProgress: Boolean = false,
|
||||||
|
val bookingSuccess: Boolean = false
|
||||||
|
)
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package ru.myitschool.work.booking.presentation
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import ru.myitschool.work.booking.data.BookingRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.util.DataStoreManager
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
class BookingViewModel : ViewModel() {
|
||||||
|
private val _state = MutableStateFlow(BookingState())
|
||||||
|
val state: StateFlow<BookingState> = _state.asStateFlow()
|
||||||
|
|
||||||
|
init {
|
||||||
|
onIntent(BookingIntent.LoadData)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onIntent(intent: BookingIntent) {
|
||||||
|
when (intent) {
|
||||||
|
is BookingIntent.LoadData -> loadData()
|
||||||
|
is BookingIntent.SelectDate -> selectDate(intent.date)
|
||||||
|
is BookingIntent.SelectPlace -> selectPlace(intent.placeId)
|
||||||
|
is BookingIntent.Book -> book()
|
||||||
|
is BookingIntent.Refresh -> refresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadData() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_state.value = _state.value.copy(isLoading = true, error = null)
|
||||||
|
|
||||||
|
val code = DataStoreManager.getAuthCode()
|
||||||
|
if (code == null) {
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
isLoading = false,
|
||||||
|
error = "Код авторизации не найден"
|
||||||
|
)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
|
when (val result = BookingRepository.getAvailableForBooking(code)) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
val dateToPlacesMap = result.data.dateToPlaces
|
||||||
|
|
||||||
|
val sortedDates = dateToPlacesMap.keys
|
||||||
|
.filter { date ->
|
||||||
|
val places = dateToPlacesMap[date]
|
||||||
|
places != null && places.isNotEmpty()
|
||||||
|
}
|
||||||
|
.sortedBy { parseDate(it) }
|
||||||
|
|
||||||
|
val firstDate = sortedDates.firstOrNull()
|
||||||
|
val places = firstDate?.let { dateToPlacesMap[it] } ?: emptyList()
|
||||||
|
|
||||||
|
_state.value = BookingState(
|
||||||
|
isLoading = false,
|
||||||
|
dates = sortedDates,
|
||||||
|
dateToPlacesMap = dateToPlacesMap,
|
||||||
|
selectedDate = firstDate,
|
||||||
|
availablePlaces = places,
|
||||||
|
selectedPlaceId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is MyResult.Error -> {
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
isLoading = false,
|
||||||
|
error = result.error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun selectDate(date: String) {
|
||||||
|
val currentState = _state.value
|
||||||
|
val places = currentState.dateToPlacesMap[date] ?: emptyList()
|
||||||
|
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
selectedDate = date,
|
||||||
|
availablePlaces = places,
|
||||||
|
selectedPlaceId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun selectPlace(placeId: Int) {
|
||||||
|
_state.value = _state.value.copy(selectedPlaceId = placeId)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun book() {
|
||||||
|
val currentState = _state.value
|
||||||
|
val date = currentState.selectedDate
|
||||||
|
val placeId = currentState.selectedPlaceId
|
||||||
|
|
||||||
|
if (date == null || placeId == null) return
|
||||||
|
|
||||||
|
viewModelScope.launch {
|
||||||
|
_state.value = _state.value.copy(isBookingInProgress = true, error = null)
|
||||||
|
|
||||||
|
val code = DataStoreManager.getAuthCode()
|
||||||
|
if (code == null) {
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
isBookingInProgress = false,
|
||||||
|
error = "Код авторизации не найден"
|
||||||
|
)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
|
when (val result = BookingRepository.createBooking(code, date, placeId)) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
isBookingInProgress = false,
|
||||||
|
bookingSuccess = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is MyResult.Error -> {
|
||||||
|
_state.value = _state.value.copy(
|
||||||
|
isBookingInProgress = false,
|
||||||
|
error = result.error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun refresh() {
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseDate(dateString: String): Long {
|
||||||
|
return try {
|
||||||
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
|
||||||
|
dateFormat.parse(dateString)?.time ?: 0L
|
||||||
|
} catch (e: Exception) {
|
||||||
|
0L
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
app/src/main/java/ru/myitschool/work/core/MyResult.kt
Normal file
6
app/src/main/java/ru/myitschool/work/core/MyResult.kt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package ru.myitschool.work.core
|
||||||
|
|
||||||
|
sealed class MyResult<out T> {
|
||||||
|
data class Success<T>(val data: T): MyResult<T>()
|
||||||
|
data class Error(val error: String): MyResult<Nothing>()
|
||||||
|
}
|
||||||
113
app/src/main/java/ru/myitschool/work/core/NetworkDataSource.kt
Normal file
113
app/src/main/java/ru/myitschool/work/core/NetworkDataSource.kt
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package ru.myitschool.work.core
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import io.ktor.client.HttpClient
|
||||||
|
import io.ktor.client.call.body
|
||||||
|
import io.ktor.client.engine.cio.CIO
|
||||||
|
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||||
|
import io.ktor.client.request.get
|
||||||
|
import io.ktor.client.request.post
|
||||||
|
import io.ktor.client.request.setBody
|
||||||
|
import io.ktor.client.statement.bodyAsText
|
||||||
|
import io.ktor.http.ContentType
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.http.contentType
|
||||||
|
import io.ktor.serialization.kotlinx.json.json
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import ru.myitschool.work.booking.data.AvailableBookingResponse
|
||||||
|
import ru.myitschool.work.booking.data.AvailablePlace
|
||||||
|
import ru.myitschool.work.booking.data.BookRequest
|
||||||
|
import ru.myitschool.work.user.data.UserInfoResponse
|
||||||
|
|
||||||
|
object NetworkDataSource {
|
||||||
|
private val client by lazy {
|
||||||
|
HttpClient(CIO) {
|
||||||
|
install(ContentNegotiation) {
|
||||||
|
json(
|
||||||
|
Json {
|
||||||
|
isLenient = true
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
explicitNulls = true
|
||||||
|
encodeDefaults = true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun checkAuth(code: String): MyResult<Boolean> = withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val response = client.get(getUrl(code, Constants.AUTH_URL))
|
||||||
|
when (response.status) {
|
||||||
|
HttpStatusCode.OK -> MyResult.Success(true)
|
||||||
|
else -> {
|
||||||
|
MyResult.Error("Неверный код или ошибка сервера")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
MyResult.Error("Сетевая ошибка")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getUserInfo(code: String): MyResult<UserInfoResponse> =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val response = client.get(getUrl(code, Constants.INFO_URL))
|
||||||
|
when (response.status) {
|
||||||
|
HttpStatusCode.OK -> {
|
||||||
|
val userInfo: UserInfoResponse = response.body()
|
||||||
|
MyResult.Success(userInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
MyResult.Error("Ошибка получения данных пользователя")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
MyResult.Error("Сетевая ошибка")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getAvailableForBooking(code: String): MyResult<AvailableBookingResponse> =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val response = client.get(getUrl(code, Constants.BOOKING_URL))
|
||||||
|
when (response.status) {
|
||||||
|
HttpStatusCode.OK -> {
|
||||||
|
val bookingResponse: Map<String, List<AvailablePlace>> = response.body()
|
||||||
|
MyResult.Success(bookingResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
MyResult.Error("Ошибка получения доступных мест")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
MyResult.Error("Сетевая ошибка")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun createBooking(code: String, date: String, placeId: Int): MyResult<Unit> =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
|
||||||
|
contentType(ContentType.Application.Json)
|
||||||
|
setBody(BookRequest(date = date, placeID = placeId))
|
||||||
|
}
|
||||||
|
when (response.status) {
|
||||||
|
HttpStatusCode.OK, HttpStatusCode.Created -> {
|
||||||
|
MyResult.Success(Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
MyResult.Error("Ошибка бронирования")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
MyResult.Error("Сетевая ошибка")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package ru.myitschool.work.data.repo
|
|
||||||
|
|
||||||
import ru.myitschool.work.data.source.NetworkDataSource
|
|
||||||
|
|
||||||
object AuthRepository {
|
|
||||||
|
|
||||||
private var codeCache: String? = null
|
|
||||||
|
|
||||||
suspend fun checkAndSave(text: String): Result<Boolean> {
|
|
||||||
return NetworkDataSource.checkAuth(text).onSuccess { success ->
|
|
||||||
if (success) {
|
|
||||||
codeCache = text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package ru.myitschool.work.data.source
|
|
||||||
|
|
||||||
import io.ktor.client.HttpClient
|
|
||||||
import io.ktor.client.engine.cio.CIO
|
|
||||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
|
||||||
import io.ktor.client.request.get
|
|
||||||
import io.ktor.client.statement.bodyAsText
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import io.ktor.serialization.kotlinx.json.json
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import ru.myitschool.work.core.Constants
|
|
||||||
|
|
||||||
object NetworkDataSource {
|
|
||||||
private val client by lazy {
|
|
||||||
HttpClient(CIO) {
|
|
||||||
install(ContentNegotiation) {
|
|
||||||
json(
|
|
||||||
Json {
|
|
||||||
isLenient = true
|
|
||||||
ignoreUnknownKeys = true
|
|
||||||
explicitNulls = true
|
|
||||||
encodeDefaults = true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun checkAuth(code: String): Result<Boolean> = withContext(Dispatchers.IO) {
|
|
||||||
return@withContext runCatching {
|
|
||||||
val response = client.get(getUrl(code, Constants.AUTH_URL))
|
|
||||||
when (response.status) {
|
|
||||||
HttpStatusCode.OK -> true
|
|
||||||
else -> error(response.bodyAsText())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package ru.myitschool.work.domain.auth
|
|
||||||
|
|
||||||
import ru.myitschool.work.data.repo.AuthRepository
|
|
||||||
|
|
||||||
class CheckAndSaveAuthCodeUseCase(
|
|
||||||
private val repository: AuthRepository
|
|
||||||
) {
|
|
||||||
suspend operator fun invoke(
|
|
||||||
text: String
|
|
||||||
): Result<Unit> {
|
|
||||||
return repository.checkAndSave(text).mapCatching { success ->
|
|
||||||
if (!success) error("Code is incorrect")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
87
app/src/main/java/ru/myitschool/work/ui/NavigationGraph.kt
Normal file
87
app/src/main/java/ru/myitschool/work/ui/NavigationGraph.kt
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package ru.myitschool.work.ui
|
||||||
|
|
||||||
|
import androidx.compose.animation.EnterTransition
|
||||||
|
import androidx.compose.animation.ExitTransition
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import androidx.navigation.NavHostController
|
||||||
|
import androidx.navigation.compose.NavHost
|
||||||
|
import androidx.navigation.compose.composable
|
||||||
|
import androidx.navigation.compose.rememberNavController
|
||||||
|
import ru.myitschool.work.auth.presentation.AuthScreen
|
||||||
|
import ru.myitschool.work.booking.presentation.BookingScreen
|
||||||
|
import ru.myitschool.work.user.presentation.MainScreen
|
||||||
|
import ru.myitschool.work.user.presentation.MainViewModel
|
||||||
|
import ru.myitschool.work.util.DataStoreManager
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AppNavHost(
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
navController: NavHostController = rememberNavController()
|
||||||
|
) {
|
||||||
|
var startDestination by remember { mutableStateOf<Screen>(Screen.Auth) }
|
||||||
|
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
val isAuth = DataStoreManager.isAuthenticated()
|
||||||
|
startDestination = if (isAuth) {
|
||||||
|
Screen.Main
|
||||||
|
} else {
|
||||||
|
Screen.Auth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NavHost(
|
||||||
|
modifier = modifier,
|
||||||
|
enterTransition = { EnterTransition.None },
|
||||||
|
exitTransition = { ExitTransition.None },
|
||||||
|
navController = navController,
|
||||||
|
startDestination = startDestination,
|
||||||
|
) {
|
||||||
|
composable<Screen.Auth> {
|
||||||
|
AuthScreen(navController = navController)
|
||||||
|
}
|
||||||
|
|
||||||
|
composable<Screen.Main> { backStackEntry ->
|
||||||
|
val mainViewModel: MainViewModel = viewModel()
|
||||||
|
|
||||||
|
LaunchedEffect(navController.currentBackStackEntry) {
|
||||||
|
navController.currentBackStackEntry
|
||||||
|
?.savedStateHandle
|
||||||
|
?.getStateFlow<Boolean>("booking_success", false)
|
||||||
|
?.collect { bookingSuccess ->
|
||||||
|
if (bookingSuccess) {
|
||||||
|
mainViewModel.refreshAfterBooking()
|
||||||
|
navController.currentBackStackEntry
|
||||||
|
?.savedStateHandle
|
||||||
|
?.set("booking_success", false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MainScreen(
|
||||||
|
viewModel = mainViewModel,
|
||||||
|
navController = navController
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
composable<Screen.Book> {
|
||||||
|
BookingScreen(
|
||||||
|
onNavigateBack = {
|
||||||
|
navController.popBackStack()
|
||||||
|
},
|
||||||
|
onBookingSuccess = {
|
||||||
|
navController.previousBackStackEntry
|
||||||
|
?.savedStateHandle
|
||||||
|
?.set("booking_success", true)
|
||||||
|
navController.popBackStack()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
app/src/main/java/ru/myitschool/work/ui/Screen.kt
Normal file
12
app/src/main/java/ru/myitschool/work/ui/Screen.kt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.myitschool.work.ui
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
sealed interface Screen{
|
||||||
|
@Serializable
|
||||||
|
data object Auth: Screen
|
||||||
|
@Serializable
|
||||||
|
data object Book: Screen
|
||||||
|
@Serializable
|
||||||
|
data object Main: Screen
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.nav
|
|
||||||
|
|
||||||
sealed interface AppDestination
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.nav
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object AuthScreenDestination: AppDestination
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.nav
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object BookScreenDestination: AppDestination
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.nav
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object MainScreenDestination: AppDestination
|
|
||||||
@@ -8,7 +8,7 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import ru.myitschool.work.ui.screen.AppNavHost
|
import ru.myitschool.work.ui.AppNavHost
|
||||||
import ru.myitschool.work.ui.theme.WorkTheme
|
import ru.myitschool.work.ui.theme.WorkTheme
|
||||||
|
|
||||||
class RootActivity : ComponentActivity() {
|
class RootActivity : ComponentActivity() {
|
||||||
@@ -17,9 +17,9 @@ class RootActivity : ComponentActivity() {
|
|||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
setContent {
|
setContent {
|
||||||
WorkTheme {
|
WorkTheme {
|
||||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
Scaffold(modifier = Modifier.Companion.fillMaxSize()) { innerPadding ->
|
||||||
AppNavHost(
|
AppNavHost(
|
||||||
modifier = Modifier
|
modifier = Modifier.Companion
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(innerPadding)
|
.padding(innerPadding)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.screen
|
|
||||||
|
|
||||||
import androidx.compose.animation.EnterTransition
|
|
||||||
import androidx.compose.animation.ExitTransition
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import androidx.navigation.compose.NavHost
|
|
||||||
import androidx.navigation.compose.composable
|
|
||||||
import androidx.navigation.compose.rememberNavController
|
|
||||||
import ru.myitschool.work.ui.nav.AuthScreenDestination
|
|
||||||
import ru.myitschool.work.ui.nav.BookScreenDestination
|
|
||||||
import ru.myitschool.work.ui.nav.MainScreenDestination
|
|
||||||
import ru.myitschool.work.ui.screen.auth.AuthScreen
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun AppNavHost(
|
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
navController: NavHostController = rememberNavController()
|
|
||||||
) {
|
|
||||||
NavHost(
|
|
||||||
modifier = modifier,
|
|
||||||
enterTransition = { EnterTransition.None },
|
|
||||||
exitTransition = { ExitTransition.None },
|
|
||||||
navController = navController,
|
|
||||||
startDestination = AuthScreenDestination,
|
|
||||||
) {
|
|
||||||
composable<AuthScreenDestination> {
|
|
||||||
AuthScreen(navController = navController)
|
|
||||||
}
|
|
||||||
composable<MainScreenDestination> {
|
|
||||||
Box(
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
Text(text = "Hello")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
composable<BookScreenDestination> {
|
|
||||||
Box(
|
|
||||||
contentAlignment = Alignment.Center
|
|
||||||
) {
|
|
||||||
Text(text = "Hello")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
|
||||||
|
|
||||||
sealed interface AuthIntent {
|
|
||||||
data class Send(val text: String): AuthIntent
|
|
||||||
data class TextInput(val text: String): AuthIntent
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Spacer
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.material3.Button
|
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextField
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.collectAsState
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.platform.testTag
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavController
|
|
||||||
import ru.myitschool.work.R
|
|
||||||
import ru.myitschool.work.core.TestIds
|
|
||||||
import ru.myitschool.work.ui.nav.MainScreenDestination
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun AuthScreen(
|
|
||||||
viewModel: AuthViewModel = viewModel(),
|
|
||||||
navController: NavController
|
|
||||||
) {
|
|
||||||
val state by viewModel.uiState.collectAsState()
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
viewModel.actionFlow.collect {
|
|
||||||
navController.navigate(MainScreenDestination)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(all = 24.dp),
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Center
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
text = stringResource(R.string.auth_title),
|
|
||||||
style = MaterialTheme.typography.headlineSmall,
|
|
||||||
textAlign = TextAlign.Center
|
|
||||||
)
|
|
||||||
when (val currentState = state) {
|
|
||||||
is AuthState.Data -> Content(viewModel, currentState)
|
|
||||||
is AuthState.Loading -> {
|
|
||||||
CircularProgressIndicator(
|
|
||||||
modifier = Modifier.size(64.dp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
private fun Content(
|
|
||||||
viewModel: AuthViewModel,
|
|
||||||
state: AuthState.Data
|
|
||||||
) {
|
|
||||||
var inputText by remember { mutableStateOf("") }
|
|
||||||
Spacer(modifier = Modifier.size(16.dp))
|
|
||||||
TextField(
|
|
||||||
modifier = Modifier.testTag(TestIds.Auth.CODE_INPUT).fillMaxWidth(),
|
|
||||||
value = inputText,
|
|
||||||
onValueChange = {
|
|
||||||
inputText = it
|
|
||||||
viewModel.onIntent(AuthIntent.TextInput(it))
|
|
||||||
},
|
|
||||||
label = { Text(stringResource(R.string.auth_label)) }
|
|
||||||
)
|
|
||||||
Spacer(modifier = Modifier.size(16.dp))
|
|
||||||
Button(
|
|
||||||
modifier = Modifier.testTag(TestIds.Auth.SIGN_BUTTON).fillMaxWidth(),
|
|
||||||
onClick = {
|
|
||||||
viewModel.onIntent(AuthIntent.Send(inputText))
|
|
||||||
},
|
|
||||||
enabled = true
|
|
||||||
) {
|
|
||||||
Text(stringResource(R.string.auth_sign_in))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
|
||||||
|
|
||||||
sealed interface AuthState {
|
|
||||||
object Loading: AuthState
|
|
||||||
object Data: AuthState
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package ru.myitschool.work.ui.screen.auth
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.flow.update
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import ru.myitschool.work.data.repo.AuthRepository
|
|
||||||
import ru.myitschool.work.domain.auth.CheckAndSaveAuthCodeUseCase
|
|
||||||
|
|
||||||
class AuthViewModel : ViewModel() {
|
|
||||||
private val checkAndSaveAuthCodeUseCase by lazy { CheckAndSaveAuthCodeUseCase(AuthRepository) }
|
|
||||||
private val _uiState = MutableStateFlow<AuthState>(AuthState.Data)
|
|
||||||
val uiState: StateFlow<AuthState> = _uiState.asStateFlow()
|
|
||||||
|
|
||||||
private val _actionFlow: MutableSharedFlow<Unit> = MutableSharedFlow()
|
|
||||||
val actionFlow: SharedFlow<Unit> = _actionFlow
|
|
||||||
|
|
||||||
fun onIntent(intent: AuthIntent) {
|
|
||||||
when (intent) {
|
|
||||||
is AuthIntent.Send -> {
|
|
||||||
viewModelScope.launch(Dispatchers.Default) {
|
|
||||||
_uiState.update { AuthState.Loading }
|
|
||||||
checkAndSaveAuthCodeUseCase.invoke("9999").fold(
|
|
||||||
onSuccess = {
|
|
||||||
_actionFlow.emit(Unit)
|
|
||||||
},
|
|
||||||
onFailure = { error ->
|
|
||||||
error.printStackTrace()
|
|
||||||
_actionFlow.emit(Unit)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is AuthIntent.TextInput -> Unit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.myitschool.work.user.data
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class UserInfoResponse(
|
||||||
|
val name: String,
|
||||||
|
val photoUrl: String,
|
||||||
|
val booking: Map<String, BookingItem> = emptyMap()
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class BookingItem(
|
||||||
|
val id: Int,
|
||||||
|
val place: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package ru.myitschool.work.user.data
|
||||||
|
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.core.NetworkDataSource
|
||||||
|
import ru.myitschool.work.user.domain.User
|
||||||
|
|
||||||
|
object UserRepository {
|
||||||
|
|
||||||
|
suspend fun getUserInfo(code: String): MyResult<User> {
|
||||||
|
return when (val result = NetworkDataSource.getUserInfo(code)) {
|
||||||
|
is MyResult.Success -> MyResult.Success(result.data.toUser())
|
||||||
|
is MyResult.Error -> result
|
||||||
|
}
|
||||||
|
/*return MyResult.Success(
|
||||||
|
User(
|
||||||
|
"kio",
|
||||||
|
"https://catalog-cdn.detmir.st/media/2fe02057f9915e72a378795d32c79ea9.jpeg",
|
||||||
|
mapOf(
|
||||||
|
"15.12.2025" to "Кабинет 305",
|
||||||
|
"20.12.2025" to "Аудитория 101",
|
||||||
|
"01.01.2026" to "Лаборатория 12"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)*/
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun UserInfoResponse.toUser() = User(
|
||||||
|
name = name,
|
||||||
|
imageUrl = photoUrl,
|
||||||
|
bookings = booking.mapValues { it.value.place }
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.myitschool.work.user.domain
|
||||||
|
|
||||||
|
import ru.myitschool.work.user.data.UserRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
|
||||||
|
class GetUserUseCase(
|
||||||
|
private val repository: UserRepository
|
||||||
|
) {
|
||||||
|
suspend operator fun invoke(code: String): MyResult<User> {
|
||||||
|
return repository.getUserInfo(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
7
app/src/main/java/ru/myitschool/work/user/domain/User.kt
Normal file
7
app/src/main/java/ru/myitschool/work/user/domain/User.kt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.myitschool.work.user.domain
|
||||||
|
|
||||||
|
data class User(
|
||||||
|
val name: String,
|
||||||
|
val imageUrl: String,
|
||||||
|
val bookings: Map<String, String> = emptyMap()
|
||||||
|
)
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package ru.myitschool.work.user.presentation
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.Card
|
||||||
|
import androidx.compose.material3.CardDefaults
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.testTag
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import ru.myitschool.work.core.TestIds
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun BookingItem(
|
||||||
|
booking: BookingDisplayItem,
|
||||||
|
index: Int
|
||||||
|
) {
|
||||||
|
Card(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.testTag(TestIds.Main.getIdItemByPosition(index)),
|
||||||
|
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(16.dp)
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = booking.date,
|
||||||
|
modifier = Modifier.testTag(TestIds.Main.ITEM_DATE),
|
||||||
|
style = MaterialTheme.typography.titleMedium
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(4.dp))
|
||||||
|
Text(
|
||||||
|
text = booking.place,
|
||||||
|
modifier = Modifier.testTag(TestIds.Main.ITEM_PLACE),
|
||||||
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.myitschool.work.user.presentation
|
||||||
|
|
||||||
|
sealed interface MainIntent {
|
||||||
|
data object LogOut: MainIntent
|
||||||
|
data object Refresh: MainIntent
|
||||||
|
data object AddBooking: MainIntent
|
||||||
|
}
|
||||||
@@ -0,0 +1,227 @@
|
|||||||
|
package ru.myitschool.work.user.presentation
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
|
import androidx.compose.foundation.lazy.itemsIndexed
|
||||||
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
|
import androidx.compose.material3.Button
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.platform.testTag
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import coil3.compose.AsyncImage
|
||||||
|
import ru.myitschool.work.core.TestIds
|
||||||
|
import ru.myitschool.work.ui.Screen
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun MainScreen(
|
||||||
|
viewModel: MainViewModel = viewModel(),
|
||||||
|
navController: NavController
|
||||||
|
) {
|
||||||
|
val state by viewModel.mainState.collectAsState()
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
viewModel.navigationEvent.collect { event ->
|
||||||
|
when (event) {
|
||||||
|
MainViewModel.NavigationEvent.ToAuth -> {
|
||||||
|
navController.navigate(Screen.Auth) {
|
||||||
|
popUpTo(Screen.Auth) { inclusive = true }
|
||||||
|
launchSingleTop = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MainViewModel.NavigationEvent.ToBooking -> {
|
||||||
|
navController.navigate(Screen.Book)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Scaffold { padding ->
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(padding)
|
||||||
|
) {
|
||||||
|
when {
|
||||||
|
state.error != null -> {
|
||||||
|
ErrorContent(
|
||||||
|
error = state.error!!,
|
||||||
|
onRefresh = { viewModel.onIntent(MainIntent.Refresh) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
state.isLoading -> {
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier.align(Alignment.Center)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
state.user != null -> {
|
||||||
|
MainContent(
|
||||||
|
state = state,
|
||||||
|
onLogout = { viewModel.onIntent(MainIntent.LogOut) },
|
||||||
|
onRefresh = { viewModel.onIntent(MainIntent.Refresh) },
|
||||||
|
onAddBooking = { viewModel.onIntent(MainIntent.AddBooking) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun ErrorContent(
|
||||||
|
error: String,
|
||||||
|
onRefresh: () -> Unit
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = error,
|
||||||
|
modifier = Modifier.testTag(TestIds.Main.ERROR),
|
||||||
|
style = MaterialTheme.typography.bodyLarge,
|
||||||
|
color = MaterialTheme.colorScheme.error
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
Button(
|
||||||
|
onClick = onRefresh,
|
||||||
|
modifier = Modifier.testTag(TestIds.Main.REFRESH_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Обновить")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun MainContent(
|
||||||
|
state: MainState,
|
||||||
|
onLogout: () -> Unit,
|
||||||
|
onRefresh: () -> Unit,
|
||||||
|
onAddBooking: () -> Unit
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp)
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.weight(1f)
|
||||||
|
) {
|
||||||
|
AsyncImage(
|
||||||
|
model = state.user?.imageUrl,
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier
|
||||||
|
.size(64.dp)
|
||||||
|
.clip(CircleShape)
|
||||||
|
.testTag(TestIds.Main.PROFILE_IMAGE)
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.width(16.dp))
|
||||||
|
Text(
|
||||||
|
text = state.user?.name ?: "",
|
||||||
|
modifier = Modifier.testTag(TestIds.Main.PROFILE_NAME),
|
||||||
|
style = MaterialTheme.typography.headlineSmall
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
|
) {
|
||||||
|
Button(
|
||||||
|
onClick = onRefresh,
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.testTag(TestIds.Main.REFRESH_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Обновить")
|
||||||
|
}
|
||||||
|
Button(
|
||||||
|
onClick = onLogout,
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.testTag(TestIds.Main.LOGOUT_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Выйти")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Button(
|
||||||
|
onClick = onAddBooking,
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.testTag(TestIds.Main.ADD_BUTTON)
|
||||||
|
) {
|
||||||
|
Text("Забронировать")
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
Text(
|
||||||
|
text = "Мои бронирования",
|
||||||
|
style = MaterialTheme.typography.titleLarge
|
||||||
|
)
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
|
||||||
|
if (state.bookings.isEmpty()) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
contentAlignment = Alignment.Center
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = "У вас пока нет бронирований",
|
||||||
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LazyColumn(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
|
) {
|
||||||
|
itemsIndexed(state.bookings) { index, booking ->
|
||||||
|
BookingItem(
|
||||||
|
booking = booking,
|
||||||
|
index = index
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ru.myitschool.work.user.presentation
|
||||||
|
|
||||||
|
import ru.myitschool.work.user.domain.User
|
||||||
|
|
||||||
|
data class MainState(
|
||||||
|
val user: User? = null,
|
||||||
|
val bookings: List<BookingDisplayItem> = emptyList(),
|
||||||
|
val isLoading: Boolean = false,
|
||||||
|
val error: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
data class BookingDisplayItem(
|
||||||
|
val date: String, // dd.MM.yyyy
|
||||||
|
val place: String,
|
||||||
|
val originalDate: String //для сортировки
|
||||||
|
)
|
||||||
|
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
package ru.myitschool.work.user.presentation
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import ru.myitschool.work.auth.data.AuthRepository
|
||||||
|
import ru.myitschool.work.user.data.UserRepository
|
||||||
|
import ru.myitschool.work.core.MyResult
|
||||||
|
import ru.myitschool.work.auth.domain.LogOutUseCase
|
||||||
|
import ru.myitschool.work.user.domain.GetUserUseCase
|
||||||
|
import ru.myitschool.work.util.DataStoreManager
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
class MainViewModel : ViewModel() {
|
||||||
|
private val logOutUseCase = LogOutUseCase(AuthRepository)
|
||||||
|
private val getUserUseCase = GetUserUseCase(UserRepository)
|
||||||
|
|
||||||
|
private val _mainState = MutableStateFlow(MainState())
|
||||||
|
val mainState = _mainState.asStateFlow()
|
||||||
|
|
||||||
|
private val _navigationEvent = MutableSharedFlow<NavigationEvent>(extraBufferCapacity = 1)
|
||||||
|
val navigationEvent: SharedFlow<NavigationEvent> = _navigationEvent.asSharedFlow()
|
||||||
|
|
||||||
|
init {
|
||||||
|
loadUser()
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class NavigationEvent {
|
||||||
|
data object ToAuth : NavigationEvent()
|
||||||
|
data object ToBooking : NavigationEvent()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onIntent(intent: MainIntent) {
|
||||||
|
when (intent) {
|
||||||
|
MainIntent.LogOut -> logOut()
|
||||||
|
MainIntent.Refresh -> loadUser()
|
||||||
|
MainIntent.AddBooking -> navigateToBooking()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun refreshAfterBooking() {
|
||||||
|
loadUser()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadUser() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_mainState.update { it.copy(isLoading = true, error = null) }
|
||||||
|
|
||||||
|
val code = DataStoreManager.getAuthCode()
|
||||||
|
if (code.isNullOrBlank()) {
|
||||||
|
_navigationEvent.emit(NavigationEvent.ToAuth)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
|
when (val result = getUserUseCase(code)) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
val bookings = convertBookingsToDisplayItems(result.data.bookings)
|
||||||
|
_mainState.update {
|
||||||
|
it.copy(
|
||||||
|
user = result.data,
|
||||||
|
bookings = bookings,
|
||||||
|
isLoading = false,
|
||||||
|
error = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is MyResult.Error -> {
|
||||||
|
_mainState.update {
|
||||||
|
it.copy(
|
||||||
|
isLoading = false,
|
||||||
|
error = result.error,
|
||||||
|
user = null,
|
||||||
|
bookings = emptyList()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun convertBookingsToDisplayItems(bookings: Map<String, String>): List<BookingDisplayItem> {
|
||||||
|
val serverFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
||||||
|
val displayFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault())
|
||||||
|
|
||||||
|
return bookings.map { (serverDate, place) ->
|
||||||
|
val displayDate = try {
|
||||||
|
val parsed = serverFormat.parse(serverDate)
|
||||||
|
displayFormat.format(parsed!!)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
serverDate
|
||||||
|
}
|
||||||
|
|
||||||
|
BookingDisplayItem(
|
||||||
|
date = displayDate,
|
||||||
|
place = place,
|
||||||
|
originalDate = serverDate
|
||||||
|
)
|
||||||
|
}.sortedBy { item ->
|
||||||
|
item.originalDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun logOut() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
when (logOutUseCase()) {
|
||||||
|
is MyResult.Success -> {
|
||||||
|
_mainState.update { MainState() }
|
||||||
|
_navigationEvent.emit(NavigationEvent.ToAuth)
|
||||||
|
}
|
||||||
|
is MyResult.Error -> {
|
||||||
|
_mainState.update { it.copy(error = "Ошибка при выходе") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun navigateToBooking() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_navigationEvent.emit(NavigationEvent.ToBooking)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package ru.myitschool.work.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.datastore.core.DataStore
|
||||||
|
import androidx.datastore.preferences.core.Preferences
|
||||||
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import ru.myitschool.work.App
|
||||||
|
|
||||||
|
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "auth_prefs")
|
||||||
|
object DataStoreManager {
|
||||||
|
private val AUTH_CODE_KEY = stringPreferencesKey("auth_code")
|
||||||
|
|
||||||
|
suspend fun saveAuthCode(code: String){
|
||||||
|
App.context.dataStore.edit { prefs ->
|
||||||
|
prefs[AUTH_CODE_KEY] = code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suspend fun getAuthCode(): String? {
|
||||||
|
return App.context.dataStore.data
|
||||||
|
.map { it[AUTH_CODE_KEY] }
|
||||||
|
.first()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
suspend fun clearAuthCode() {
|
||||||
|
App.context.dataStore.edit { prefs ->
|
||||||
|
prefs.remove(AUTH_CODE_KEY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suspend fun isAuthenticated(): Boolean {
|
||||||
|
return getAuthCode() != null
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user