Merge remote-tracking branch 'origin/lab5' into lab5

# Conflicts:
#	app/src/main/java/com/example/myapplication/api/MyServerService.kt
#	app/src/main/java/com/example/myapplication/database/AppViewModelProvider.kt
#	app/src/main/java/com/example/myapplication/database/entities/composeui/edit/BikeEdit.kt
#	app/src/main/java/com/example/myapplication/database/entities/composeui/edit/BikeEditViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/bike/BikeListViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/bike/BikeViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/cart/CartViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/item/ItemEditViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/item/ItemListViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/rent/RentListViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/rent/RentViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/rent/ReportViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/user/CurrentUserViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/user/EntryUserViewModel.kt
#	app/src/main/java/com/example/myapplication/database/room/viewmodel/user/RegisterUserViewModel.kt
#	app/src/main/java/com/example/myapplication/ui/edit/ItemEdit.kt
#	app/src/main/java/com/example/myapplication/ui/list/BikeList.kt
#	app/src/main/java/com/example/myapplication/ui/list/ItemList.kt
#	app/src/main/java/com/example/myapplication/ui/navigation/MainNavbar.kt
#	app/src/main/java/com/example/myapplication/ui/view/BikeView.kt
#	app/src/main/res/xml/network_security_config.xml
#	server/data.json
This commit is contained in:
Кашин Максим 2023-12-27 14:08:37 +04:00
commit a4676f90eb
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.example.myapplication.api
enum class ApiStatus {
LOADING, ERROR, DONE
}

View File

@ -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 = {})
}
}

View File

@ -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))
}
}
}

View File

@ -21,4 +21,6 @@
<string name="item_bike_not_select">Велосипед не указан</string>
<string name="size">Размер загруженного изображения: %1$dx%2$d</string>
<string name="not_uploaded">Загрузите изображение</string>
<string name="loading">Загрузка…</string>
<string name="back">Назад</string>
</resources>