Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Apply spotless and detekt on Center module #2257

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ internal fun CenterDetailsScreen(
onActivateCenter: (Int) -> Unit,
addSavingsAccount: (Int) -> Unit,
groupList: (Int) -> Unit,
viewModel: CenterDetailsViewModel = hiltViewModel(),
) {
val viewModel: CenterDetailsViewModel = hiltViewModel()
val centerId by viewModel.centerId.collectAsStateWithLifecycle()
val state by viewModel.centerDetailsUiState.collectAsStateWithLifecycle()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ import com.mifos.feature.center.R
internal fun GroupListScreen(
onBackPressed: () -> Unit,
loadClientsOfGroup: (List<Client>) -> Unit,
viewModel: GroupListViewModel = hiltViewModel(),
) {
val viewModel: GroupListViewModel = hiltViewModel()
val centerId by viewModel.centerId.collectAsStateWithLifecycle()
val state by viewModel.groupListUiState.collectAsStateWithLifecycle()
val groupAssociationState by viewModel.groupAssociationState.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -89,8 +90,8 @@ internal fun CenterListScreen(
paddingValues: PaddingValues,
createNewCenter: () -> Unit,
onCenterSelect: (Int) -> Unit,
viewModel: CenterListViewModel = hiltViewModel(),
) {
val viewModel: CenterListViewModel = hiltViewModel()
val refreshState by viewModel.isRefreshing.collectAsStateWithLifecycle()
val state by viewModel.centerListUiState.collectAsStateWithLifecycle()

Expand Down Expand Up @@ -122,7 +123,7 @@ internal fun CenterListScreen(
) {
val snackbarHostState = remember { SnackbarHostState() }
var isInSelectionMode by rememberSaveable { mutableStateOf(false) }
val selectedItems = remember { mutableStateListOf<Center>() }
val selectedItems = remember { SelectedItemsState() }
val resetSelectionMode = {
isInSelectionMode = false
selectedItems.clear()
Expand All @@ -141,7 +142,7 @@ internal fun CenterListScreen(

LaunchedEffect(
key1 = isInSelectionMode,
key2 = selectedItems.size,
key2 = selectedItems.size(),
) {
if (isInSelectionMode && selectedItems.isEmpty()) {
isInSelectionMode = false
Expand All @@ -153,7 +154,7 @@ internal fun CenterListScreen(
topBar = {
if (isInSelectionMode) {
SelectionModeTopAppBar(
itemCount = selectedItems.size,
itemCount = selectedItems.size(),
resetSelectionMode = resetSelectionMode,
actions = {
FilledTonalButton(
Expand Down Expand Up @@ -241,11 +242,41 @@ internal fun CenterListScreen(
}
}

private class SelectedItemsState(initialSelectedItems: List<Center> = emptyList()) {
private val _selectedItems = mutableStateListOf<Center>().also { it.addAll(initialSelectedItems) }
val selectedItems: State<List<Center>> = derivedStateOf { _selectedItems }

fun add(item: Center) {
_selectedItems.add(item)
}

fun remove(item: Center) {
_selectedItems.remove(item)
}
fun toList(): List<Center> {
return _selectedItems.toList()
}
fun contains(item: Center): Boolean {
return _selectedItems.contains(item)
}

fun clear() {
_selectedItems.clear()
}

fun size(): Int {
return _selectedItems.size
}
fun isEmpty(): Boolean {
return _selectedItems.isEmpty()
}
}

@Composable
private fun CenterListContent(
centerPagingList: LazyPagingItems<Center>,
isInSelectionMode: Boolean,
selectedItems: SnapshotStateList<Center>,
selectedItems: SelectedItemsState,
onRefresh: () -> Unit,
onCenterSelect: (Int) -> Unit,
selectedMode: () -> Unit,
Expand All @@ -265,7 +296,7 @@ private fun CenterListContent(
LazyColumn {
items(centerPagingList.itemCount) { index ->

val isSelected = selectedItems.contains(centerPagingList[index])
val isSelected = selectedItems.contains(centerPagingList[index]!!)
var cardColor by remember { mutableStateOf(White) }

OutlinedCard(
Expand All @@ -275,7 +306,7 @@ private fun CenterListContent(
onClick = {
if (isInSelectionMode) {
cardColor = if (isSelected) {
selectedItems.remove(centerPagingList[index])
centerPagingList[index]?.let { selectedItems.remove(it) }
White
} else {
centerPagingList[index]?.let { selectedItems.add(it) }
Expand All @@ -288,7 +319,7 @@ private fun CenterListContent(
onLongClick = {
if (isInSelectionMode) {
cardColor = if (isSelected) {
selectedItems.remove(centerPagingList[index])
centerPagingList[index]?.let { selectedItems.remove(it) }
White
} else {
centerPagingList[index]?.let { selectedItems.add(it) }
Expand Down Expand Up @@ -571,7 +602,7 @@ private fun CenterListContentPreview() {
CenterListContent(
centerPagingList = sampleCenterList.collectAsLazyPagingItems(),
isInSelectionMode = false,
selectedItems = rememberSaveable { mutableStateListOf() },
selectedItems = SelectedItemsState(),
onRefresh = {},
onCenterSelect = {},
selectedMode = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ import java.util.Locale
@Composable
internal fun CreateNewCenterScreen(
onCreateSuccess: () -> Unit,
viewModel: CreateNewCenterViewModel = hiltViewModel(),
) {
val viewModel: CreateNewCenterViewModel = hiltViewModel()
val state by viewModel.createNewCenterUiState.collectAsStateWithLifecycle()

LaunchedEffect(Unit) {
Expand Down Expand Up @@ -189,117 +189,118 @@ private fun CreateNewCenterContent(offices: List<Office>, createCenter: (CenterP
}
}
}
Column {
if (showDatePicker) {
DatePickerDialog(
onDismissRequest = {
showDatePicker = false
},
confirmButton = {
TextButton(
onClick = {
showDatePicker = false
datePickerState.selectedDateMillis?.let {
activateDate = it
}
},
) { Text(stringResource(id = R.string.feature_center_select)) }
},
dismissButton = {
TextButton(
onClick = {
showDatePicker = false
},
) { Text(stringResource(id = R.string.feature_center_cancel)) }
},
) {
DatePicker(state = datePickerState)
}
}

if (showDatePicker) {
DatePickerDialog(
onDismissRequest = {
showDatePicker = false
},
confirmButton = {
TextButton(
onClick = {
showDatePicker = false
datePickerState.selectedDateMillis?.let {
activateDate = it
}
},
) { Text(stringResource(id = R.string.feature_center_select)) }
MifosOutlinedTextField(
value = centerName,
onValueChange = { centerName = it },
label = R.string.feature_center_center_name,
error = null,
)

MifosTextFieldDropdown(
value = selectedOffice,
onValueChanged = {
selectedOffice = it
},
dismissButton = {
TextButton(
onClick = {
showDatePicker = false
},
) { Text(stringResource(id = R.string.feature_center_cancel)) }
onOptionSelected = { index, value ->
selectedOffice = value
offices[index].id?.let {
officeId = it
}
},
label = R.string.feature_center_office,
options = offices.map { it.name.toString() },
readOnly = true,
)

Row(
modifier = Modifier.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
DatePicker(state = datePickerState)
Checkbox(
checked = isActivate,
onCheckedChange = {
isActivate = it
},
)
Text(text = stringResource(id = R.string.feature_center_activate))
}
}

MifosOutlinedTextField(
value = centerName,
onValueChange = { centerName = it },
label = R.string.feature_center_center_name,
error = null,
)

MifosTextFieldDropdown(
value = selectedOffice,
onValueChanged = {
selectedOffice = it
},
onOptionSelected = { index, value ->
selectedOffice = value
offices[index].id?.let {
officeId = it
}
},
label = R.string.feature_center_office,
options = offices.map { it.name.toString() },
readOnly = true,
)
if (isActivate) {
MifosDatePickerTextField(
value = SimpleDateFormat("dd MMMM yyyy", Locale.getDefault()).format(
activateDate,
),
label = R.string.feature_center_activation_date,
openDatePicker = {
showDatePicker = true
},
)
Spacer(modifier = Modifier.height(16.dp))
}

Row(
modifier = Modifier.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Checkbox(
checked = isActivate,
onCheckedChange = {
isActivate = it
Button(
onClick = {
if (validateAllFields()) {
createCenter(
CenterPayload(
name = centerName.text,
active = isActivate,
activationDate = if (isActivate) {
SimpleDateFormat(
"dd MMMM yyyy",
Locale.getDefault(),
).format(
activateDate,
)
} else {
null
},
officeId = officeId,
dateFormat = "dd MMMM yyyy",
locale = "en",
),
)
}
},
)
Text(text = stringResource(id = R.string.feature_center_activate))
}

if (isActivate) {
MifosDatePickerTextField(
value = SimpleDateFormat("dd MMMM yyyy", Locale.getDefault()).format(
activateDate,
modifier = Modifier
.fillMaxWidth()
.heightIn(44.dp)
.padding(start = 16.dp, end = 16.dp),
contentPadding = PaddingValues(),
colors = ButtonDefaults.buttonColors(
containerColor = if (isSystemInDarkTheme()) BluePrimaryDark else BluePrimary,
),
label = R.string.feature_center_activation_date,
openDatePicker = {
showDatePicker = true
},
)
Spacer(modifier = Modifier.height(16.dp))
}

Button(
onClick = {
if (validateAllFields()) {
createCenter(
CenterPayload(
name = centerName.text,
active = isActivate,
activationDate = if (isActivate) {
SimpleDateFormat(
"dd MMMM yyyy",
Locale.getDefault(),
).format(
activateDate,
)
} else {
null
},
officeId = officeId,
dateFormat = "dd MMMM yyyy",
locale = "en",
),
)
}
},
modifier = Modifier
.fillMaxWidth()
.heightIn(44.dp)
.padding(start = 16.dp, end = 16.dp),
contentPadding = PaddingValues(),
colors = ButtonDefaults.buttonColors(
containerColor = if (isSystemInDarkTheme()) BluePrimaryDark else BluePrimary,
),
) {
Text(text = stringResource(id = R.string.feature_center_create), fontSize = 16.sp)
) {
Text(text = stringResource(id = R.string.feature_center_create), fontSize = 16.sp)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import com.mifos.feature.center.R

@Composable
internal fun SyncCenterDialogScreen(
viewModel: SyncCentersDialogViewModel = hiltViewModel(),
dismiss: () -> Unit,
hide: () -> Unit,
centers: List<Center>? = listOf(),
viewModel: SyncCentersDialogViewModel = hiltViewModel(),
) {
val uiState by viewModel.syncCentersDialogUiState.collectAsStateWithLifecycle()
val uiData by viewModel.syncCenterData.collectAsStateWithLifecycle()
Expand Down
Loading
Loading