Skip to content

Commit

Permalink
refactor: (#33) viewModels di
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkoro committed Mar 30, 2024
1 parent b120a2c commit 86afa01
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object AppModule {
localDb: LocalDatabase,
orderRepository: OrderRepositoryImpl,
): Pager<Int, OrderEntity> {
val pageSize = 10
val pageSize = 20
return Pager(
config = PagingConfig(pageSize = pageSize),
remoteMediator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -38,13 +48,39 @@ 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) {
DashboardScreen(
onDashboardNavigate = {
navController.navigate(Screen.LoginScreen.route)
},
onLoginAndEncryptCredentials = { loginRequestDto, context, encryptSubjectAndPassword ->
loginFieldsViewModel.loginAndEncryptCredentials(
loginRequestDto,
context,
encryptSubjectAndPassword,
)
},
onGetUser = { subject, token ->
userViewModel
.getUser(subject, token)
.body<GetUserResponseDto>()
},
onGetUserResponseChange = userViewModel::onGetUserResponseDto,
user = userViewModel.user,
orders = orderViewModel.orderPagingFlow.collectAsLazyPagingItems(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<Order>,
) {
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
Expand All @@ -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,
Expand All @@ -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<GetUserResponseDto>().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,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fun BottomBarContent(
HorizontalPager(
state = pagerState,
modifier =
Modifier
.fillMaxWidth()
.weight(1F),
Modifier
.fillMaxWidth()
.weight(1F),
userScrollEnabled = false,
) { index ->
content(index)
Expand All @@ -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,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -46,7 +46,7 @@ class LoginViewModel
request: LoginRequestDto,
context: Context,
encryptSubjectAndPassword: Boolean = false,
): LoginResponseType {
): LoginResponse {
return try {
login(request).run {
val status = this.status
Expand All @@ -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
}
}

Expand Down
Loading

0 comments on commit 86afa01

Please sign in to comment.