Skip to content

Commit

Permalink
Adding new currency selection dialog and page component
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuppan committed Nov 26, 2023
1 parent 9d46711 commit d1dca6c
Show file tree
Hide file tree
Showing 17 changed files with 603 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.nkuppan.country.buildsrc

object Versions {
const val compileSdk = 33
const val compileSdk = 34
const val minSdk = 21
const val targetSdk = 33
const val targetSdk = 34

const val versionCode = 18
const val versionName = "1.0.18"
const val versionCode = 20
const val versionName = "1.1.0"

const val groupId = "com.github.nkuppan"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,116 +1,25 @@
package com.github.nkuppan.countrycompose.presentation.country

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Icon
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Close
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.github.nkuppan.country.core.R
import com.github.nkuppan.country.domain.model.Country
import com.github.nkuppan.countrycompose.ui.components.CountryItemView
import com.github.nkuppan.countrycompose.ui.components.CountrySearchView
import com.github.nkuppan.countrycompose.ui.theme.CountryAppTheme
import com.github.nkuppan.countrycompose.utils.getCountryImage

@Composable
internal fun CountrySearchView(
dismiss: (() -> Unit)? = null,
searchCallback: ((String) -> Unit)? = null
) {

var showClear by remember { mutableStateOf(false) }

var searchText by remember { mutableStateOf("") }

Surface(
elevation = 8.dp
) {
Row(modifier = Modifier.padding(start = 16.dp, end = 16.dp, bottom = 12.dp, top = 4.dp)) {
OutlinedTextField(
modifier = Modifier
.background(color = Color.Transparent)
.weight(1f),
value = searchText,
onValueChange = {
searchText = it
showClear = it.isNotBlank()
searchCallback?.invoke(it)
},
label = {
Text(
modifier = Modifier.align(
Alignment.CenterVertically
),
text = stringResource(
id = R.string.search_country
),
textAlign = TextAlign.Center
)
},
leadingIcon = {
Icon(
modifier = Modifier
.padding(8.dp)
.size(24.dp)
.align(Alignment.CenterVertically)
.clickable {
dismiss?.invoke()
},
imageVector = Icons.Default.ArrowBack,
contentDescription = ""
)
},
trailingIcon = {
if (showClear) {
Icon(
modifier = Modifier
.padding(8.dp)
.size(24.dp)
.align(Alignment.CenterVertically)
.clickable {
searchText = ""
showClear = false
searchCallback?.invoke("")
},
imageVector = Icons.Default.Close,
contentDescription = ""
)
}
}
)
}
}
}

@Composable
internal fun CountrySearchAndListView(
internal fun CountryListAndSearchView(
modifier: Modifier = Modifier,
countryListViewModel: CountryListViewModel,
dismiss: (() -> Unit)? = null,
Expand All @@ -119,22 +28,22 @@ internal fun CountrySearchAndListView(

val countries by countryListViewModel.countryList.collectAsState()

CountrySearchAndListView(
modifier,
countries,
countryListViewModel::loadCountries,
dismiss,
selection
CountryListAndSearchView(
countries = countries,
selection = selection,
modifier = modifier,
searchCallback = countryListViewModel::loadCountries,
dismiss = dismiss
)
}

@Composable
internal fun CountrySearchAndListView(
modifier: Modifier = Modifier,
internal fun CountryListAndSearchView(
countries: List<Country>,
selection: ((Country) -> Unit)?,
modifier: Modifier = Modifier,
searchCallback: ((String) -> Unit)? = null,
dismiss: (() -> Unit)? = null,
selection: ((Country) -> Unit)?
dismiss: (() -> Unit)? = null
) {
Scaffold(
topBar = {
Expand All @@ -144,63 +53,43 @@ internal fun CountrySearchAndListView(
)
},
) {
it.calculateTopPadding()
Column(modifier = modifier.fillMaxSize()) {
CountryListView(countries, selection)
}
CountryListView(
countries = countries,
selection = selection,
modifier = modifier
.fillMaxSize()
.padding(it)
)
}
}

@Composable
internal fun CountryListView(
countries: List<Country>,
selection: ((Country) -> Unit)?
selection: ((Country) -> Unit)?,
modifier: Modifier = Modifier
) {
val context = LocalContext.current

LazyColumn(modifier = Modifier.fillMaxWidth()) {
LazyColumn(modifier = modifier) {
items(countries) {
CountryDetailsView(
CountryItemView(
modifier = Modifier.clickable {
selection?.invoke(it)
},
name = it.name ?: "",
flagImage = it.getCountryImage(context)
flagImage = it.countryCode.getCountryImage(context)
)
}
}
}

@Composable
internal fun CountryDetailsView(
modifier: Modifier = Modifier,
name: String,
flagImage: Int
) {
Row(modifier = modifier.fillMaxWidth()) {
Image(
modifier = Modifier
.padding(16.dp)
.size(32.dp)
.align(Alignment.CenterVertically),
painter = painterResource(flagImage),
contentDescription = ""
)
Text(
modifier = Modifier
.align(Alignment.CenterVertically)
.fillMaxWidth(),
text = name,
)
}
}


@Composable
@Preview(showBackground = true)
private fun CountryDetailsPreview() {
CountryAppTheme(isDarkTheme = true) {
CountrySearchAndListView(
CountryListAndSearchView(
countries = listOf(
Country("India", "in", ""),
Country("USA", "us", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class CountryListViewModel(

private var currentJob: Job? = null

init {
loadCountries()
}

fun loadCountries(countryName: String? = "") {

currentJob?.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ fun CountrySelectionDialog(
onDismissRequest: (() -> Unit)? = null,
selection: ((Country) -> Unit)? = null
) {

countryListViewModel.loadCountries()

CountryAppTheme {
Dialog(
onDismissRequest = {
onDismissRequest?.invoke()
}
) {
CountrySearchAndListView(
CountryListAndSearchView(
modifier,
countryListViewModel,
onDismissRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ package com.github.nkuppan.countrycompose.presentation.country

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.github.nkuppan.country.domain.model.Country
import com.github.nkuppan.countrycompose.ui.theme.CountryAppTheme


@Preview
@Composable
fun CountrySelectionPage(
modifier: Modifier = Modifier,
countryListViewModel: CountryListViewModel = viewModel(factory = ViewModelFactory),
onDismissRequest: (() -> Unit)? = null,
selection: ((Country) -> Unit)? = null
) {
countryListViewModel.loadCountries()

CountryAppTheme {
CountrySearchAndListView(
CountryListAndSearchView(
modifier,
countryListViewModel,
onDismissRequest,
Expand Down
Loading

0 comments on commit d1dca6c

Please sign in to comment.