diff --git a/app/src/main/java/com/kenkoro/taurus/client/di/AppModule.kt b/app/src/main/java/com/kenkoro/taurus/client/di/AppModule.kt index d634857..2453756 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/di/AppModule.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/di/AppModule.kt @@ -73,7 +73,7 @@ object AppModule { localDb: LocalDatabase, orderRepository: OrderRepositoryImpl, ): Pager { - val pageSize = 10 + val pageSize = 20 return Pager( config = PagingConfig(pageSize = pageSize), remoteMediator = diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/Navigation.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/Navigation.kt index c4a10d4..fe35e90 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/Navigation.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/Navigation.kt @@ -2,15 +2,22 @@ package com.kenkoro.taurus.client.feature.sewing.presentation import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext +import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController +import androidx.paging.compose.collectAsLazyPagingItems +import com.kenkoro.taurus.client.feature.sewing.data.source.remote.dto.response.GetUserResponseDto import com.kenkoro.taurus.client.feature.sewing.presentation.screen.dashboard.DashboardScreen +import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.LoginFieldsViewModel import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.LoginScreen +import com.kenkoro.taurus.client.feature.sewing.presentation.screen.order.OrderViewModel +import com.kenkoro.taurus.client.feature.sewing.presentation.screen.user.UserViewModel import com.kenkoro.taurus.client.feature.sewing.presentation.util.DecryptedCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.util.LocalCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.util.Screen +import io.ktor.client.call.body @Composable fun AppNavHost( @@ -29,6 +36,9 @@ fun AppNavHost( context = context, ).value + val loginFieldsViewModel: LoginFieldsViewModel = hiltViewModel() + val userViewModel: UserViewModel = hiltViewModel() + val orderViewModel: OrderViewModel = hiltViewModel() NavHost( navController = navController, startDestination = startDestination(locallyStoredSubject, locallyStoredPassword).route, @@ -38,6 +48,17 @@ fun AppNavHost( onLoginNavigate = { navController.navigate(Screen.DashboardScreen.route) }, + subject = loginFieldsViewModel.subject, + onSubjectChange = loginFieldsViewModel::subject, + password = loginFieldsViewModel.password, + onPasswordChange = loginFieldsViewModel::password, + onLoginAndEncryptCredentials = { loginRequestDto, context, encryptSubjectAndPassword -> + loginFieldsViewModel.loginAndEncryptCredentials( + loginRequestDto, + context, + encryptSubjectAndPassword, + ) + }, ) } composable(route = Screen.DashboardScreen.route) { @@ -45,6 +66,21 @@ fun AppNavHost( onDashboardNavigate = { navController.navigate(Screen.LoginScreen.route) }, + onLoginAndEncryptCredentials = { loginRequestDto, context, encryptSubjectAndPassword -> + loginFieldsViewModel.loginAndEncryptCredentials( + loginRequestDto, + context, + encryptSubjectAndPassword, + ) + }, + onGetUser = { subject, token -> + userViewModel + .getUser(subject, token) + .body() + }, + onGetUserResponseChange = userViewModel::onGetUserResponseDto, + user = userViewModel.user, + orders = orderViewModel.orderPagingFlow.collectAsLazyPagingItems(), ) } } diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardScreen.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardScreen.kt index b8c80b2..0e0d255 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardScreen.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardScreen.kt @@ -1,6 +1,7 @@ package com.kenkoro.taurus.client.feature.sewing.presentation.screen.dashboard import android.annotation.SuppressLint +import android.content.Context import android.util.Log import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement @@ -25,25 +26,28 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel +import androidx.paging.compose.LazyPagingItems import com.kenkoro.taurus.client.R import com.kenkoro.taurus.client.core.connectivity.ConnectivityObserver import com.kenkoro.taurus.client.core.connectivity.NetworkConnectivityObserver import com.kenkoro.taurus.client.core.connectivity.Status import com.kenkoro.taurus.client.feature.sewing.data.source.remote.dto.request.LoginRequestDto import com.kenkoro.taurus.client.feature.sewing.data.source.remote.dto.response.GetUserResponseDto +import com.kenkoro.taurus.client.feature.sewing.domain.model.Order +import com.kenkoro.taurus.client.feature.sewing.domain.model.User import com.kenkoro.taurus.client.feature.sewing.presentation.screen.dashboard.components.BottomBarHost -import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.LoginViewModel import com.kenkoro.taurus.client.feature.sewing.presentation.screen.order.OrderScreen import com.kenkoro.taurus.client.feature.sewing.presentation.screen.user.UserScreen -import com.kenkoro.taurus.client.feature.sewing.presentation.screen.user.UserViewModel import com.kenkoro.taurus.client.feature.sewing.presentation.shared.components.ErrorSnackbar import com.kenkoro.taurus.client.feature.sewing.presentation.shared.components.showErrorSnackbar import com.kenkoro.taurus.client.feature.sewing.presentation.shared.handlers.handleLoginWithLocallyScopedCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.shared.handlers.handleUserGetWithLocallyScopedCredentials -import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponseType +import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponse import com.kenkoro.taurus.client.ui.theme.AppTheme -import io.ktor.client.call.body +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext object BottomBarHostIndices { const val USER_SCREEN = 1 @@ -53,15 +57,17 @@ object BottomBarHostIndices { @Composable fun DashboardScreen( onDashboardNavigate: () -> Unit = {}, - loginViewModel: LoginViewModel = hiltViewModel(), - // dashboardViewModel: DashboardViewModel = hiltViewModel(), - userViewModel: UserViewModel = hiltViewModel(), + onLoginAndEncryptCredentials: suspend (LoginRequestDto, Context, Boolean) -> LoginResponse, + onGetUser: suspend (String, String) -> GetUserResponseDto, + onGetUserResponseChange: (GetUserResponseDto) -> Unit, + user: User?, + orders: LazyPagingItems, ) { val context = LocalContext.current val snackbarHostState = remember { SnackbarHostState() } - var loginResponseType by remember { - mutableStateOf(LoginResponseType.Pending) + var loginResponse by remember { + mutableStateOf(LoginResponse.Pending) } val networkConnectivityObserver: ConnectivityObserver = NetworkConnectivityObserver(context) val networkStatus by networkConnectivityObserver @@ -86,7 +92,7 @@ fun DashboardScreen( .background(MaterialTheme.colorScheme.background), ) { if (networkStatus != Status.Available) { - loginResponseType = LoginResponseType.RequestFailure + loginResponse = LoginResponse.RequestFailure showErrorSnackbar( snackbarHostState = snackbarHostState, key = networkStatus, @@ -95,71 +101,77 @@ fun DashboardScreen( ) } else { LaunchedEffect(Unit) { - loginResponseType = LoginResponseType.Pending - loginResponseType = - handleLoginWithLocallyScopedCredentials( - login = { subject, password, encryptSubjectAndPassword -> - loginViewModel.loginAndEncryptCredentials( - request = LoginRequestDto(subject, password), - context = context, - encryptSubjectAndPassword = encryptSubjectAndPassword, - ) - }, - context = context, - ) + loginResponse = LoginResponse.Pending + withContext(Dispatchers.IO) { + launch { + loginResponse = + async { + handleLoginWithLocallyScopedCredentials( + login = { subject, password, encryptThese -> + val request = + LoginRequestDto( + subject = subject, + password = password, + ) + onLoginAndEncryptCredentials(request, context, encryptThese) + }, + context = context, + ) + }.await() - handleUserGetWithLocallyScopedCredentials(context = context) { firstName, token -> - try { - userViewModel - .getUser(firstName, token) - .body().run { - userViewModel.onGetUserResponseDto(this) + handleUserGetWithLocallyScopedCredentials(context = context) { subject, token -> + try { + onGetUser(subject, token).run { + onGetUserResponseChange(this) + } + } catch (e: Exception) { + Log.d("kenkoro", e.message!!) } - } catch (e: Exception) { - Log.d("kenkoro", e.message!!) + } } } } } } - when (loginResponseType) { - LoginResponseType.Success -> { + when (loginResponse) { + LoginResponse.Success -> { BottomBarHost { index -> when (index) { BottomBarHostIndices.USER_SCREEN -> { UserScreen( - user = userViewModel.user, + user = user, networkStatus = networkStatus, ) } else -> OrderScreen( - user = userViewModel.user, + user = user, networkStatus = networkStatus, + orders = orders, ) } } } - LoginResponseType.RequestFailure -> { + LoginResponse.RequestFailure -> { showErrorSnackbar( snackbarHostState = snackbarHostState, - key = loginResponseType, + key = loginResponse, message = stringResource(id = R.string.request_error), ) } - LoginResponseType.Failure -> { + LoginResponse.Failure -> { onDashboardNavigate() } - LoginResponseType.BadCredentials -> { + LoginResponse.BadCredentials -> { onDashboardNavigate() } - LoginResponseType.Pending -> { + LoginResponse.Pending -> { Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardViewModel.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardViewModel.kt deleted file mode 100644 index 1fc8c15..0000000 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/DashboardViewModel.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.kenkoro.taurus.client.feature.sewing.presentation.screen.dashboard - -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.setValue -import androidx.lifecycle.ViewModel -import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponseType -import dagger.hilt.android.lifecycle.HiltViewModel -import javax.inject.Inject - -@HiltViewModel -class DashboardViewModel - @Inject - constructor() : ViewModel() { - var loginResponseType by mutableStateOf(LoginResponseType.Pending) - private set - - fun onResponse(response: LoginResponseType) { - loginResponseType = response - } - } \ No newline at end of file diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarContent.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarContent.kt index e62af6e..0f58532 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarContent.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarContent.kt @@ -46,9 +46,9 @@ fun BottomBarContent( HorizontalPager( state = pagerState, modifier = - Modifier - .fillMaxWidth() - .weight(1F), + Modifier + .fillMaxWidth() + .weight(1F), userScrollEnabled = false, ) { index -> content(index) @@ -68,19 +68,20 @@ fun BottomBarContent( onClick = { selectedTabIndex = index }, - text = if (onlyIcons) { - null - } else { - { Text(text = tabItem.title) } - }, + text = + if (onlyIcons) { + null + } else { + { Text(text = tabItem.title) } + }, icon = { Icon( imageVector = - if (selected) { - tabItem.selectedIcon - } else { - tabItem.unselectedIcon - }, + if (selected) { + tabItem.selectedIcon + } else { + tabItem.unselectedIcon + }, contentDescription = tabItem.title, ) }, diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarHost.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarHost.kt index 96ecd0e..f2aac59 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarHost.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/dashboard/components/BottomBarHost.kt @@ -42,12 +42,12 @@ fun BottomBarHost( BottomBarContent( modifier = - modifier - .statusBarsPadding() - .navigationBarsPadding(), + modifier + .statusBarsPadding() + .navigationBarsPadding(), tabItems = tabItems, pagerState = pagerState, - onlyIcons = true + onlyIcons = true, ) { index -> content(index) } diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginViewModel.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginFieldsViewModel.kt similarity index 92% rename from app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginViewModel.kt rename to app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginFieldsViewModel.kt index f45e0b4..ae02420 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginViewModel.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginFieldsViewModel.kt @@ -11,7 +11,7 @@ import com.kenkoro.taurus.client.feature.sewing.data.source.repository.UserRepos import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.util.LoginCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.util.EncryptedCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.util.LocalCredentials -import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponseType +import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponse import dagger.hilt.android.lifecycle.HiltViewModel import io.ktor.client.call.body import io.ktor.client.statement.HttpResponse @@ -23,7 +23,7 @@ import javax.inject.Inject value class JwtToken(val value: String) @HiltViewModel -class LoginViewModel +class LoginFieldsViewModel @Inject constructor( private val userRepository: UserRepositoryImpl, @@ -46,7 +46,7 @@ class LoginViewModel request: LoginRequestDto, context: Context, encryptSubjectAndPassword: Boolean = false, - ): LoginResponseType { + ): LoginResponse { return try { login(request).run { val status = this.status @@ -68,17 +68,17 @@ class LoginViewModel context = context, ) } - LoginResponseType.Success + LoginResponse.Success } else { if (apiUrlNotFound(status)) { - LoginResponseType.RequestFailure + LoginResponse.RequestFailure } else { - LoginResponseType.Failure + LoginResponse.Failure } } } } catch (_: Exception) { - LoginResponseType.RequestFailure + LoginResponse.RequestFailure } } diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginScreen.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginScreen.kt index dc7d0d3..51b5845 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginScreen.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/LoginScreen.kt @@ -1,5 +1,6 @@ package com.kenkoro.taurus.client.feature.sewing.presentation.screen.login +import android.content.Context import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize @@ -17,17 +18,27 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.kenkoro.taurus.client.core.local.LocalContentHeight import com.kenkoro.taurus.client.core.local.LocalContentWidth +import com.kenkoro.taurus.client.feature.sewing.data.source.remote.dto.request.LoginRequestDto import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.components.LoginFieldsContent import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.components.LoginHelpContent import com.kenkoro.taurus.client.feature.sewing.presentation.shared.components.ErrorSnackbar +import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponse import com.kenkoro.taurus.client.ui.theme.AppTheme @Composable -fun LoginScreen(onLoginNavigate: () -> Unit) { - val snackbarHostState = remember { SnackbarHostState() } +fun LoginScreen( + onLoginNavigate: () -> Unit, + subject: String, + onSubjectChange: (String) -> Unit, + password: String, + onPasswordChange: (String) -> Unit, + onLoginAndEncryptCredentials: suspend (LoginRequestDto, Context, Boolean) -> LoginResponse, +) { val contentWidth = LocalContentWidth.current val contentHeight = LocalContentHeight.current + val snackbarHostState = remember { SnackbarHostState() } + AppTheme { Scaffold( snackbarHost = { @@ -55,6 +66,11 @@ fun LoginScreen(onLoginNavigate: () -> Unit) { .weight(9F), onLoginNavigate = onLoginNavigate, snackbarHostState = snackbarHostState, + subject = subject, + onSubjectChange = onSubjectChange, + password = password, + onPasswordChange = onPasswordChange, + onLoginAndEncryptCredentials = onLoginAndEncryptCredentials, ) LoginHelpContent( modifier = Modifier.weight(1F), diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/components/LoginFieldsContent.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/components/LoginFieldsContent.kt index b60b8c9..a797c08 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/components/LoginFieldsContent.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/login/components/LoginFieldsContent.kt @@ -1,5 +1,6 @@ package com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.components +import android.content.Context import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -32,8 +33,6 @@ import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation -import androidx.hilt.navigation.compose.hiltViewModel -import androidx.lifecycle.viewModelScope import com.kenkoro.taurus.client.R import com.kenkoro.taurus.client.core.connectivity.ConnectivityObserver import com.kenkoro.taurus.client.core.connectivity.NetworkConnectivityObserver @@ -42,16 +41,21 @@ import com.kenkoro.taurus.client.core.local.LocalContentHeight import com.kenkoro.taurus.client.core.local.LocalContentWidth import com.kenkoro.taurus.client.core.local.LocalShape import com.kenkoro.taurus.client.feature.sewing.data.source.remote.dto.request.LoginRequestDto -import com.kenkoro.taurus.client.feature.sewing.presentation.screen.login.LoginViewModel import com.kenkoro.taurus.client.feature.sewing.presentation.shared.components.showErrorSnackbar -import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponseType +import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponse +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @Composable fun LoginFieldsContent( snackbarHostState: SnackbarHostState, - loginViewModel: LoginViewModel = hiltViewModel(), onLoginNavigate: () -> Unit, + subject: String, + onSubjectChange: (String) -> Unit, + password: String, + onPasswordChange: (String) -> Unit, + onLoginAndEncryptCredentials: suspend (LoginRequestDto, Context, Boolean) -> LoginResponse, modifier: Modifier, ) { val context = LocalContext.current @@ -59,9 +63,7 @@ fun LoginFieldsContent( val contentWidth = LocalContentWidth.current val contentHeight = LocalContentHeight.current - val subject = loginViewModel.subject - val password = loginViewModel.password - val loginViewModelScope = loginViewModel.viewModelScope + val scope = CoroutineScope(Dispatchers.IO) val requestErrorMessage = stringResource(id = R.string.request_error) val subjectAndPasswordCannotBeBlankMessage = @@ -75,7 +77,7 @@ fun LoginFieldsContent( FieldData( value = subject, onValueChange = { - loginViewModel.subject(it) + onSubjectChange(it) }, placeholderText = stringResource(id = R.string.login_subject), keyboardOptions = @@ -88,7 +90,7 @@ fun LoginFieldsContent( FieldData( value = password, onValueChange = { - loginViewModel.password(it) + onPasswordChange(it) }, placeholderText = stringResource(id = R.string.login_password), keyboardOptions = @@ -153,25 +155,22 @@ fun LoginFieldsContent( .size(width = contentWidth.halfStandard, height = contentHeight.standard), shape = RoundedCornerShape(shape.medium), onClick = { - loginViewModelScope.launch { + scope.launch(Dispatchers.IO) { val response = if (subject.isNotBlank() && password.isNotBlank()) { - loginViewModel.loginAndEncryptCredentials( - request = - LoginRequestDto( - subject = subject, - password = password, - ), - context = context, - encryptSubjectAndPassword = true, - ) + val request = + LoginRequestDto( + subject = subject, + password = password, + ) + onLoginAndEncryptCredentials(request, context, true) } else { - LoginResponseType.BadCredentials + LoginResponse.BadCredentials } - if (response != LoginResponseType.Success) { + if (response != LoginResponse.Success) { val message = - if (response == LoginResponseType.BadCredentials) { + if (response == LoginResponse.BadCredentials) { subjectAndPasswordCannotBeBlankMessage } else { requestErrorMessage diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/OrderScreen.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/OrderScreen.kt index 3460d4a..1fc5141 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/OrderScreen.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/OrderScreen.kt @@ -25,12 +25,12 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel -import androidx.paging.compose.collectAsLazyPagingItems +import androidx.paging.compose.LazyPagingItems import com.kenkoro.taurus.client.core.connectivity.Status import com.kenkoro.taurus.client.core.local.LocalContentHeight import com.kenkoro.taurus.client.core.local.LocalContentWidth import com.kenkoro.taurus.client.core.local.LocalShape +import com.kenkoro.taurus.client.feature.sewing.domain.model.Order import com.kenkoro.taurus.client.feature.sewing.domain.model.User import com.kenkoro.taurus.client.feature.sewing.presentation.screen.order.components.OrderContent import com.kenkoro.taurus.client.feature.sewing.presentation.shared.components.ErrorSnackbar @@ -39,7 +39,7 @@ import com.kenkoro.taurus.client.ui.theme.AppTheme @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @Composable fun OrderScreen( - orderViewModel: OrderViewModel = hiltViewModel(), + orders: LazyPagingItems, user: User?, networkStatus: Status, ) { @@ -48,7 +48,6 @@ fun OrderScreen( val contentWidth = LocalContentWidth.current val snackbarHostState = remember { SnackbarHostState() } - val orders = orderViewModel.orderPagingFlow.collectAsLazyPagingItems() AppTheme { Scaffold( @@ -62,18 +61,19 @@ fun OrderScreen( }, bottomBar = { Column( - modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.background), + modifier = + Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.background), horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center + verticalArrangement = Arrangement.Center, ) { Spacer(modifier = Modifier.height(contentHeight.medium)) Button( enabled = networkStatus == Status.Available, modifier = - Modifier - .size(contentWidth.standard + 30.dp, contentHeight.halfStandard), + Modifier + .size(contentWidth.standard + 30.dp, contentHeight.halfStandard), shape = RoundedCornerShape(shape.small), onClick = { /*TODO*/ }, ) { @@ -81,19 +81,19 @@ fun OrderScreen( } Spacer(modifier = Modifier.height(contentHeight.medium)) } - } + }, ) { Surface( modifier = - Modifier - .fillMaxSize() - .background(MaterialTheme.colorScheme.background), + Modifier + .fillMaxSize() + .background(MaterialTheme.colorScheme.background), ) { OrderContent( orders = orders, snackbarHostState = snackbarHostState, user = user, - networkStatus = networkStatus + networkStatus = networkStatus, ) } } diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/components/OrderContent.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/components/OrderContent.kt index 577773e..6013dc6 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/components/OrderContent.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/screen/order/components/OrderContent.kt @@ -29,7 +29,7 @@ fun OrderContent( orders: LazyPagingItems, snackbarHostState: SnackbarHostState, user: User?, - networkStatus: Status + networkStatus: Status, ) { val arrangement = LocalArrangement.current val contentHeight = LocalContentHeight.current diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/shared/handlers/Handlers.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/shared/handlers/Handlers.kt index c161a5e..5525845 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/shared/handlers/Handlers.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/shared/handlers/Handlers.kt @@ -3,12 +3,17 @@ package com.kenkoro.taurus.client.feature.sewing.presentation.shared.handlers import android.content.Context import com.kenkoro.taurus.client.feature.sewing.presentation.util.DecryptedCredentials import com.kenkoro.taurus.client.feature.sewing.presentation.util.LocalCredentials -import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponseType +import com.kenkoro.taurus.client.feature.sewing.presentation.util.LoginResponse +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.launch suspend fun handleUserGetWithLocallyScopedCredentials( context: Context, getUser: suspend (String, String) -> Unit, ) { + val scope = CoroutineScope(Dispatchers.IO) val firstName = DecryptedCredentials.getDecryptedCredential( filename = LocalCredentials.SUBJECT_FILENAME, @@ -19,13 +24,17 @@ suspend fun handleUserGetWithLocallyScopedCredentials( filename = LocalCredentials.TOKEN_FILENAME, context = context, ).value - getUser(firstName, token) + + scope.launch { + getUser(firstName, token) + } } suspend fun handleLoginWithLocallyScopedCredentials( - login: suspend (String, String, Boolean) -> LoginResponseType, + login: suspend (String, String, Boolean) -> LoginResponse, context: Context, -): LoginResponseType { +): LoginResponse { + val scope = CoroutineScope(Dispatchers.IO) val locallyStoredSubject = DecryptedCredentials.getDecryptedCredential( filename = LocalCredentials.SUBJECT_FILENAME, @@ -38,8 +47,10 @@ suspend fun handleLoginWithLocallyScopedCredentials( ).value return if (locallyStoredSubject.isNotBlank() && locallyStoredPassword.isNotBlank()) { - login(locallyStoredSubject, locallyStoredPassword, false) + scope.async { + login(locallyStoredSubject, locallyStoredPassword, false) + }.await() } else { - LoginResponseType.BadCredentials + LoginResponse.BadCredentials } } \ No newline at end of file diff --git a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponseType.kt b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponse.kt similarity index 81% rename from app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponseType.kt rename to app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponse.kt index a6a5f0f..33170d5 100644 --- a/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponseType.kt +++ b/app/src/main/java/com/kenkoro/taurus/client/feature/sewing/presentation/util/LoginResponse.kt @@ -1,6 +1,6 @@ package com.kenkoro.taurus.client.feature.sewing.presentation.util -enum class LoginResponseType { +enum class LoginResponse { Success, Failure, BadCredentials,