Skip to content

Commit

Permalink
Merge pull request #432 from skniyajali/419-enhancement-address
Browse files Browse the repository at this point in the history
Enhancement - AddOnItem, Address
  • Loading branch information
niyajali authored Nov 12, 2023
2 parents b8bb91f + 174f22d commit 67168b6
Show file tree
Hide file tree
Showing 37 changed files with 214 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object RootNavGraph : NavGraphSpec {

override val destinationsByRoute = emptyMap<String, DestinationSpec<*>>()

override val startRoute = AddonitemNavGraph
override val startRoute = AddressNavGraph

override val nestedNavGraphs = listOf(
AddonitemNavGraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object AddressTestTags {
//Address Screen

const val ADDRESS_SCREEN_TITLE = "Addresses"
const val ADDRESS_NOT_AVAIlABLE = "Address Not Available"
const val ADDRESS_NOT_AVAILABLE = "Address Not Available"
const val ADDRESS_SEARCH_PLACEHOLDER = "Search for Addresses..."

const val CREATE_ADDRESS_SCREEN = "Create New Address"
Expand Down Expand Up @@ -34,17 +34,18 @@ object AddressTestTags {

const val ADDRESS_ITEM_TAG = "Tag-"


const val ADDRESS_SETTINGS_TITLE = "Address Settings"

const val IMPORT_ADDRESS_TITLE = "Import Address Item"
const val IMPORT_ADDRESS_TITLE = "Import Address"
const val IMPORT_ADDRESS_BTN_TEXT = "Import Address"
const val IMPORT_ADDRESS_OPN_FILE = "Open File"
const val IMPORT_ADDRESS_NOTE_TEXT = "Make sure to open addresses.json file."

const val EXPORT_ADDRESS_TITLE = "Export Address Item"
const val EXPORT_ADDRESS_TITLE = "Export Address"
const val EXPORT_ADDRESS_BTN_TEXT = "Export Address"
const val EXPORT_ADDRESS_BTN = "Export Address"

const val EXPORT_ADDRESS_FILE_NAME = "addresses"

const val ADDRESS_SCREEN_NOTE_TEXT = "Click on any address to view details."
}
40 changes: 39 additions & 1 deletion core/ui/src/main/java/com/niyaj/ui/components/TextWithIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ErrorOutline
import androidx.compose.material.icons.filled.Info
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.niyaj.designsystem.theme.SpaceMedium
import com.niyaj.designsystem.theme.SpaceMini
import com.niyaj.designsystem.theme.SpaceSmall
Expand Down Expand Up @@ -273,7 +280,7 @@ fun NoteText(
}

@Composable
fun NoteCard(
fun InfoText(
modifier: Modifier = Modifier,
iconModifier: Modifier = Modifier.size(SpaceMedium),
text: String,
Expand Down Expand Up @@ -316,3 +323,34 @@ fun NoteCard(
}
}
}


@Composable
fun NoteCard(
modifier: Modifier = Modifier,
text: String,
icon: ImageVector = Icons.Default.Info,
height: Dp = 48.dp,
containerColor: Color = MaterialTheme.colorScheme.tertiaryContainer,
textStyle: TextStyle = MaterialTheme.typography.labelMedium
) {
ListItem(
modifier = modifier
.height(height)
.fillMaxWidth()
.clip(RoundedCornerShape(SpaceMini)),
headlineContent = {
Text(
text = text,
style = textStyle
)
},
leadingContent = {
Icon(
imageVector = icon,
contentDescription = "info"
)
},
colors = ListItemDefaults.colors(containerColor = containerColor)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.niyaj.common.result.Resource
import com.niyaj.common.utils.capitalizeWords
import com.niyaj.common.utils.safeInt
import com.niyaj.data.repository.AddOnItemRepository
import com.niyaj.data.repository.validation.AddOnItemValidationRepository
Expand Down Expand Up @@ -112,7 +113,7 @@ class AddEditAddOnItemViewModel @Inject constructor(
if (nameError.value == null && priceError.value == null) {
val addOnItem = AddOnItem(
itemId = addOnItemId,
itemName = addEditState.itemName,
itemName = addEditState.itemName.capitalizeWords.trimEnd(),
itemPrice = addEditState.itemPrice,
isApplicable = addEditState.isApplicable,
createdAt = System.currentTimeMillis(),
Expand All @@ -129,7 +130,8 @@ class AddEditAddOnItemViewModel @Inject constructor(
}

is Resource.Success -> {
_eventFlow.emit(UiEvent.OnSuccess("AddOn Item Created Or Updated Successfully."))
val message = if (addOnItemId == 0) "Created" else "Updated"
_eventFlow.emit(UiEvent.OnSuccess("AddOn Item $message Successfully."))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import com.niyaj.designsystem.theme.SpaceSmallMax
import com.niyaj.model.AddOnItem
import com.niyaj.ui.components.ItemNotAvailable
import com.niyaj.ui.components.NAV_SEARCH_BTN
import com.niyaj.ui.components.NoteCard
import com.niyaj.ui.components.InfoText
import com.niyaj.ui.components.ScrollToTop
import com.niyaj.ui.components.StandardButton
import com.niyaj.ui.components.StandardScaffoldNew
Expand Down Expand Up @@ -76,7 +76,7 @@ fun AddOnExportScreen(
val showSearchBar = viewModel.showSearchBar.collectAsStateWithLifecycle().value
val searchText = viewModel.searchText.value

val showBottomBar = viewModel.totalItems.isNotEmpty()
val showBottomBar = addOnItems.isNotEmpty()

val selectedItems = viewModel.selectedItems.toList()

Expand Down Expand Up @@ -185,7 +185,7 @@ fun AddOnExportScreen(
.padding(SpaceSmallMax),
verticalArrangement = Arrangement.spacedBy(SpaceSmall)
) {
NoteCard(text = "${if (selectedItems.isEmpty()) "All" else "${selectedItems.size}"} addon items will be exported.")
InfoText(text = "${if (selectedItems.isEmpty()) "All" else "${selectedItems.size}"} addon items will be exported.")

StandardButton(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import com.niyaj.designsystem.theme.SpaceSmall
import com.niyaj.designsystem.theme.SpaceSmallMax
import com.niyaj.model.AddOnItem
import com.niyaj.ui.components.EmptyImportScreen
import com.niyaj.ui.components.NoteCard
import com.niyaj.ui.components.InfoText
import com.niyaj.ui.components.ScrollToTop
import com.niyaj.ui.components.StandardButton
import com.niyaj.ui.components.StandardScaffoldNew
Expand Down Expand Up @@ -142,7 +142,7 @@ fun AddOnImportScreen(
.padding(SpaceSmallMax),
verticalArrangement = Arrangement.spacedBy(SpaceSmall)
) {
NoteCard(text = "${if (selectedItems.isEmpty()) "All" else "${selectedItems.size}"} addon item will be imported.")
InfoText(text = "${if (selectedItems.isEmpty()) "All" else "${selectedItems.size}"} addon item will be imported.")

StandardButton(
modifier = Modifier
Expand Down
55 changes: 46 additions & 9 deletions feature/address/src/main/java/com/niyaj/address/AddressScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
Expand Down Expand Up @@ -43,9 +44,12 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.niyaj.address.destinations.AddEditAddressScreenDestination
import com.niyaj.address.destinations.AddressDetailsScreenDestination
import com.niyaj.address.destinations.AddressExportScreenDestination
import com.niyaj.address.destinations.AddressImportScreenDestination
import com.niyaj.address.destinations.AddressSettingsScreenDestination
import com.niyaj.common.tags.AddressTestTags.ADDRESS_ITEM_TAG
import com.niyaj.common.tags.AddressTestTags.ADDRESS_NOT_AVAIlABLE
import com.niyaj.common.tags.AddressTestTags.ADDRESS_NOT_AVAILABLE
import com.niyaj.common.tags.AddressTestTags.ADDRESS_SCREEN_NOTE_TEXT
import com.niyaj.common.tags.AddressTestTags.ADDRESS_SCREEN_TITLE
import com.niyaj.common.tags.AddressTestTags.ADDRESS_SEARCH_PLACEHOLDER
import com.niyaj.common.tags.AddressTestTags.CREATE_NEW_ADDRESS
Expand All @@ -57,6 +61,7 @@ import com.niyaj.model.Address
import com.niyaj.ui.components.CircularBox
import com.niyaj.ui.components.ItemNotAvailable
import com.niyaj.ui.components.LoadingIndicator
import com.niyaj.ui.components.NoteCard
import com.niyaj.ui.components.ScaffoldNavActions
import com.niyaj.ui.components.StandardFAB
import com.niyaj.ui.components.StandardScaffold
Expand All @@ -72,23 +77,22 @@ import com.ramcosta.composedestinations.result.ResultRecipient
import kotlinx.coroutines.launch

@RootNavGraph(start = true)
@Destination(
route = Screens.ADDRESS_SCREEN
)
@Destination(route = Screens.ADDRESS_SCREEN)
@Composable
fun AddressScreen(
navController: NavController,
viewModel: AddressViewModel = hiltViewModel(),
resultRecipient: ResultRecipient<AddEditAddressScreenDestination, String>,
exportRecipient: ResultRecipient<AddressExportScreenDestination, String>,
importRecipient: ResultRecipient<AddressImportScreenDestination, String>,
) {
val scope = rememberCoroutineScope()
val lazyGridState = rememberLazyGridState()
val snackbarState = remember { SnackbarHostState() }

val uiState = viewModel.addOnItems.collectAsStateWithLifecycle().value

val selectedItems = viewModel.selectedItems.toList()

val lazyGridState = rememberLazyGridState()

val showFab = viewModel.totalItems.isNotEmpty()

val event = viewModel.eventFlow.collectAsStateWithLifecycle(initialValue = null).value
Expand Down Expand Up @@ -121,6 +125,8 @@ fun AddressScreen(
viewModel.deselectItems()
} else if (showSearchBar) {
viewModel.closeSearchBar()
} else {
navController.navigateUp()
}
}

Expand All @@ -139,6 +145,28 @@ fun AddressScreen(
}
}

exportRecipient.onNavResult { result ->
when(result) {
is NavResult.Canceled -> {}
is NavResult.Value -> {
scope.launch {
snackbarState.showSnackbar(result.value)
}
}
}
}

importRecipient.onNavResult { result ->
when(result) {
is NavResult.Canceled -> {}
is NavResult.Value -> {
scope.launch {
snackbarState.showSnackbar(result.value)
}
}
}
}

StandardScaffold(
navController = navController,
title = if (selectedItems.isEmpty()) ADDRESS_SCREEN_TITLE else "${selectedItems.size} Selected",
Expand All @@ -162,7 +190,7 @@ fun AddressScreen(
placeholderText = ADDRESS_SEARCH_PLACEHOLDER,
showSettingsIcon = true,
selectionCount = selectedItems.size,
showSearchIcon = true,
showSearchIcon = showFab,
showSearchBar = showSearchBar,
searchText = searchText,
onEditClick = {
Expand Down Expand Up @@ -196,7 +224,7 @@ fun AddressScreen(

is UiState.Empty -> {
ItemNotAvailable(
text = if (searchText.isEmpty()) ADDRESS_NOT_AVAIlABLE else SEARCH_ITEM_NOT_FOUND,
text = if (searchText.isEmpty()) ADDRESS_NOT_AVAILABLE else SEARCH_ITEM_NOT_FOUND,
buttonText = CREATE_NEW_ADDRESS,
onClick = {
navController.navigate(AddEditAddressScreenDestination())
Expand All @@ -211,6 +239,15 @@ fun AddressScreen(
columns = GridCells.Fixed(2),
state = lazyGridState,
) {
item(
span = { GridItemSpan(2) }
) {
NoteCard(
modifier = Modifier.padding(SpaceSmall),
text = ADDRESS_SCREEN_NOTE_TEXT
)
}

items(
items = state.data,
key = {
Expand Down
Loading

0 comments on commit 67168b6

Please sign in to comment.