diff --git a/app/src/main/java/com/example/myapplication/api/ApiStatus.kt b/app/src/main/java/com/example/myapplication/api/ApiStatus.kt new file mode 100644 index 0000000..3952e73 --- /dev/null +++ b/app/src/main/java/com/example/myapplication/api/ApiStatus.kt @@ -0,0 +1,5 @@ +package com.example.myapplication.api + +enum class ApiStatus { + LOADING, ERROR, DONE +} \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/database/room/viewmodel/LoadingViewModel.kt b/app/src/main/java/com/example/myapplication/database/room/viewmodel/LoadingViewModel.kt new file mode 100644 index 0000000..3d3ef70 --- /dev/null +++ b/app/src/main/java/com/example/myapplication/database/room/viewmodel/LoadingViewModel.kt @@ -0,0 +1,50 @@ +package com.example.myapplication.database.room.viewmodel + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.myapplication.api.ApiStatus +import kotlinx.coroutines.launch +import retrofit2.HttpException +import java.io.IOException + +open class LoadingViewModel : ViewModel() { + var apiStatus by mutableStateOf(ApiStatus.DONE) + private set + + var apiError by mutableStateOf("") + private set + + fun runInScope( + actionSuccess: suspend () -> Unit, + actionError: suspend () -> Unit, + needLoadingScreen: Boolean = true, + ) { + viewModelScope.launch { + if (needLoadingScreen) + apiStatus = ApiStatus.LOADING + runCatching { + actionSuccess() + apiStatus = ApiStatus.DONE + apiError = "" + }.onFailure { e: Throwable -> + when (e) { + is IOException, + is HttpException -> { + actionError() + apiStatus = ApiStatus.ERROR + apiError = e.localizedMessage ?: e.toString() + } + + else -> throw e + } + } + } + } + + fun runInScope(actionSuccess: suspend () -> Unit) { + runInScope(actionSuccess, actionError = {}) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/ui/view/LoadingView.kt b/app/src/main/java/com/example/myapplication/ui/view/LoadingView.kt new file mode 100644 index 0000000..e44f58b --- /dev/null +++ b/app/src/main/java/com/example/myapplication/ui/view/LoadingView.kt @@ -0,0 +1,64 @@ +package com.example.myapplication.ui.view + +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.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.TextUnit +import androidx.compose.ui.unit.TextUnitType +import androidx.compose.ui.unit.dp +import com.example.myapplication.R + +@Composable +fun LoadingView() { + Column( + modifier = Modifier + .fillMaxSize() + .padding(10.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Text( + modifier = Modifier.fillMaxWidth(), + textAlign = TextAlign.Center, + fontSize = TextUnit(value = 25F, type = TextUnitType.Sp), + text = stringResource(id = R.string.loading) + ) + } +} + +@Composable +fun ErrorView(message: String, onBack: () -> Unit) { + Column( + modifier = Modifier + .fillMaxSize() + .padding(10.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Text( + modifier = Modifier.fillMaxWidth(), + textAlign = TextAlign.Center, + fontSize = TextUnit(value = 20F, type = TextUnitType.Sp), + text = message, + color = Color(0xFFFF1744) + ) + Spacer(modifier = Modifier.padding(bottom = 10.dp)) + Button( + modifier = Modifier.fillMaxWidth(), + onClick = { onBack() } + ) { + Text(stringResource(id = R.string.back)) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f32b9ba..33d2c02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,4 +21,6 @@ Велосипед не указан Размер загруженного изображения: %1$dx%2$d Загрузите изображение + Загрузка… + Назад \ No newline at end of file