Skip to content

Commit

Permalink
refactor: Offline cleanup (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsPronay authored Nov 12, 2024
1 parent 9903b74 commit 50ca30a
Show file tree
Hide file tree
Showing 42 changed files with 932 additions and 856 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Error
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
Expand All @@ -24,20 +25,24 @@ fun MifosErrorContent(
isRefreshEnabled: Boolean = false,
imageVector: ImageVector? = null,
onRefresh: () -> Unit = {},
refreshButtonText: String = ""
refreshButtonText: String = "",
) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
verticalArrangement = Arrangement.Center,
) {
Icon(
imageVector = imageVector ?: Icons.Default.Error,
contentDescription = null,
modifier = Modifier.size(48.dp)
modifier = Modifier.size(48.dp),
)
if (isRefreshEnabled) {
Text(text = message, modifier = Modifier.padding(vertical = 16.dp))
Text(
text = message,
modifier = Modifier.padding(vertical = 16.dp),
color = MaterialTheme.colorScheme.onBackground,
)
Button(onClick = onRefresh) {
Text(text = refreshButtonText)
}
Expand Down
9 changes: 9 additions & 0 deletions feature/offline/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
plugins {
alias(libs.plugins.mifos.android.feature)
alias(libs.plugins.mifos.android.library.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.offline

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.TestCase.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +28,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.mifos.feature.offline.test", appContext.packageName)
}
}
}
9 changes: 9 additions & 0 deletions feature/offline/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 Mifos Initiative
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
See https://github.com/openMF/android-client/blob/master/LICENSE.md
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.offline.dashboard

import android.util.Log
Expand All @@ -10,16 +19,13 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AssignmentTurnedIn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
Expand All @@ -31,24 +37,24 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.mifos.core.designsystem.component.MifosErrorContent
import com.mifos.core.designsystem.component.MifosScaffold
import com.mifos.core.designsystem.icon.MifosIcons
import com.mifos.core.ui.components.MifosEmptyUi
import com.mifos.feature.offline.R

/**
* Created by Pronay Sarker on 27/08/2024 (12:09 AM)
*/
@Composable
fun OfflineDashboardRoute(
internal fun OfflineDashboardRoute(
onBackPressed: () -> Unit,
syncClientPayload: () -> Unit,
syncGroupPayload: () -> Unit,
syncCenterPayload: () -> Unit,
syncLoanRepayment: () -> Unit,
syncSavingsAccountTransactions: () -> Unit
syncSavingsAccountTransactions: () -> Unit,
viewModel: OfflineDashboardViewModel = hiltViewModel(),
) {
val viewModel: OfflineDashboardViewModel = hiltViewModel()
val uiState by viewModel.offlineDashboardUiState.collectAsStateWithLifecycle()

LaunchedEffect(key1 = Unit) {
Expand All @@ -66,19 +72,20 @@ fun OfflineDashboardRoute(
syncGroupPayload = syncGroupPayload,
syncCenterPayload = syncCenterPayload,
syncSavingsAccountTransactions = syncSavingsAccountTransactions,
syncLoanRepayment = syncLoanRepayment
syncLoanRepayment = syncLoanRepayment,
)
}

@Composable
fun OfflineDashboardScreen(
internal fun OfflineDashboardScreen(
uiState: OfflineDashboardUiState,
onBackPressed: () -> Unit,
syncClientPayload: () -> Unit,
syncGroupPayload: () -> Unit,
syncCenterPayload: () -> Unit,
syncLoanRepayment: () -> Unit,
syncSavingsAccountTransactions: () -> Unit
modifier: Modifier = Modifier,
syncSavingsAccountTransactions: () -> Unit,
) {
val snackBarHostState = remember { SnackbarHostState() }
val context = LocalContext.current
Expand All @@ -89,31 +96,35 @@ fun OfflineDashboardScreen(
var mPayloadIndex = 5

uiState.list.forEach { item ->
Log.d("itemCountOfflineSync", "${item.count}")
if (item.count == 0) {
mPayloadIndex -= 1
if (mPayloadIndex == 0) {
errorVisibility = true
}
} else errorVisibility = false
} else {
errorVisibility = false
}
}
}
}

MifosScaffold(
modifier = modifier,
snackbarHostState = snackBarHostState,
onBackPressed = onBackPressed,
icon = MifosIcons.arrowBack,
title = stringResource(id = R.string.feature_offline_offline_Sync)
title = stringResource(id = R.string.feature_offline_offline_Sync),
) {
Box(
modifier = Modifier
.fillMaxSize()
.padding(it)
.padding(it),
) {
if (errorVisibility) {
MifosErrorContent(
message = stringResource(id = R.string.feature_offline_nothing_to_sync),
imageVector = MifosIcons.assignmentTurnedIn
MifosEmptyUi(
text = stringResource(id = R.string.feature_offline_nothing_to_sync),
icon = MifosIcons.assignmentTurnedIn,
)
} else {
when (uiState) {
Expand All @@ -132,7 +143,7 @@ fun OfflineDashboardScreen(
Type.SYNC_LOAN_REPAYMENTS -> syncLoanRepayment()
Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION -> syncSavingsAccountTransactions()
}
}
},
)
}

Expand All @@ -152,52 +163,75 @@ fun OfflineDashboardScreen(
}

@Composable
fun OfflineDashboardItemCard(
private fun OfflineDashboardItemCard(
modifier: Modifier = Modifier,
paymentItem: Int = -1,
count: Int = 0,
onClick: () -> Unit
onClick: () -> Unit,
) {
OutlinedCard(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
onClick = onClick
onClick = onClick,
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.Center
verticalArrangement = Arrangement.Center,
) {
Text(
text = stringResource(id = paymentItem),
style = MaterialTheme.typography.bodyLarge
style = MaterialTheme.typography.bodyLarge,
)

Text(
text = "$count",
style = MaterialTheme.typography.bodyLarge
style = MaterialTheme.typography.bodyLarge,
)
}
}
}

@Composable
@Preview (showSystemUi = true)
fun PreviewOfflineDashboardScreen(modifier: Modifier = Modifier) {
@Preview(showSystemUi = true)
private fun PreviewOfflineDashboardScreen(modifier: Modifier = Modifier) {
val data = listOf(
SyncStateData(count = 3, name = R.string.feature_offline_sync_clients, type = Type.SYNC_CLIENTS),
SyncStateData(count = 3, name = R.string.feature_offline_sync_groups, type = Type.SYNC_GROUPS),
SyncStateData(count = 2, name = R.string.feature_offline_sync_centers, type = Type.SYNC_CENTERS),
SyncStateData(count = 4, name = R.string.feature_offline_sync_loanRepayments, type = Type.SYNC_LOAN_REPAYMENTS),
SyncStateData(count = 5, name = R.string.feature_offline_sync_savingsAccountTransactions, type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION)
SyncStateData(
count = 3,
name = R.string.feature_offline_sync_clients,
type = Type.SYNC_CLIENTS,
),
SyncStateData(
count = 3,
name = R.string.feature_offline_sync_groups,
type = Type.SYNC_GROUPS,
),
SyncStateData(
count = 2,
name = R.string.feature_offline_sync_centers,
type = Type.SYNC_CENTERS,
),
SyncStateData(
count = 4,
name = R.string.feature_offline_sync_loanRepayments,
type = Type.SYNC_LOAN_REPAYMENTS,
),
SyncStateData(
count = 5,
name = R.string.feature_offline_sync_savingsAccountTransactions,
type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION,
),
)

OfflineDashboardScreen(
modifier = modifier,
uiState = OfflineDashboardUiState.SyncUiState(data),
onBackPressed = { },
syncClientPayload = { },
syncGroupPayload = { },
syncCenterPayload = { },
syncLoanRepayment = { }) {
syncLoanRepayment = { },
) {
}
}

Expand All @@ -206,5 +240,5 @@ enum class Type {
SYNC_GROUPS,
SYNC_CENTERS,
SYNC_LOAN_REPAYMENTS,
SYNC_SAVINGS_ACCOUNT_TRANSACTION
}
SYNC_SAVINGS_ACCOUNT_TRANSACTION,
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.offline.dashboard

/**
* Created by Aditya Gupta on 16/08/23.
*/
sealed class OfflineDashboardUiState {
data class SyncUiState(val list : List<SyncStateData>) : OfflineDashboardUiState()
data class SyncUiState(val list: List<SyncStateData>) : OfflineDashboardUiState()
}

data class SyncStateData(
var count: Int = 0,
val name : Int = -1,
val type : Type,
var errorMsg : String? = null
)
val name: Int = -1,
val type: Type,
var errorMsg: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.offline.dashboard

import androidx.lifecycle.ViewModel
Expand All @@ -20,7 +29,7 @@ import javax.inject.Inject

@HiltViewModel
class OfflineDashboardViewModel @Inject constructor(
private val repository: OfflineDashboardRepository
private val repository: OfflineDashboardRepository,
) : ViewModel() {

private val _offlineDashboardUiState = MutableStateFlow(OfflineDashboardUiState.SyncUiState(initGetSyncData()))
Expand Down Expand Up @@ -132,13 +141,13 @@ class OfflineDashboardViewModel @Inject constructor(
}
}

private fun initGetSyncData () : List<SyncStateData>{
private fun initGetSyncData(): List<SyncStateData> {
return listOf(
SyncStateData(count = 0, name = R.string.feature_offline_sync_clients, type = Type.SYNC_CLIENTS),
SyncStateData(count = 0, name = R.string.feature_offline_sync_groups, type = Type.SYNC_GROUPS),
SyncStateData(count = 0, name = R.string.feature_offline_sync_centers, type = Type.SYNC_CENTERS),
SyncStateData(count = 0, name = R.string.feature_offline_sync_loanRepayments, type = Type.SYNC_LOAN_REPAYMENTS),
SyncStateData(count = 0, name = R.string.feature_offline_sync_savingsAccountTransactions, type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION)
SyncStateData(count = 0, name = R.string.feature_offline_sync_savingsAccountTransactions, type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION),
)
}
}
Loading

0 comments on commit 50ca30a

Please sign in to comment.