From 5e05345c08acc05ee843e5c46ff9815169986eff Mon Sep 17 00:00:00 2001 From: Raj Date: Sun, 18 Aug 2024 14:25:26 +0530 Subject: [PATCH] Replace region preference with room, PR review improvements --- .../datasource/database/dao/UserRegionDao.kt | 18 +++++++++++++++ .../database/entity/UserRegionEntity.kt | 16 +++++++++++++ .../ui/components/RegionEditDialog.kt | 23 +++++++------------ .../composeactors/utils/Utilities.kt | 11 +++++++++ app/src/main/res/drawable/ic_close.xml | 10 -------- 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/developersbreach/composeactors/data/datasource/database/dao/UserRegionDao.kt create mode 100644 app/src/main/java/com/developersbreach/composeactors/data/datasource/database/entity/UserRegionEntity.kt delete mode 100644 app/src/main/res/drawable/ic_close.xml diff --git a/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/dao/UserRegionDao.kt b/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/dao/UserRegionDao.kt new file mode 100644 index 0000000..66a1e53 --- /dev/null +++ b/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/dao/UserRegionDao.kt @@ -0,0 +1,18 @@ +package com.developersbreach.composeactors.data.datasource.database.dao + +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.OnConflictStrategy +import androidx.room.Query +import androidx.room.Update +import com.developersbreach.composeactors.data.datasource.database.entity.UserRegionEntity + +@Dao +interface UserRegionDao { + + @Update(onConflict = OnConflictStrategy.REPLACE) + fun editUserRegion(userRegionEntity: UserRegionEntity) + + @Query("SELECT * FROM user_region_table") + fun getSavedUserRegion(): LiveData +} \ No newline at end of file diff --git a/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/entity/UserRegionEntity.kt b/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/entity/UserRegionEntity.kt new file mode 100644 index 0000000..91d169d --- /dev/null +++ b/app/src/main/java/com/developersbreach/composeactors/data/datasource/database/entity/UserRegionEntity.kt @@ -0,0 +1,16 @@ +package com.developersbreach.composeactors.data.datasource.database.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "user_region_table") +data class UserRegionEntity( + + @PrimaryKey + @ColumnInfo(name = "user_region_country_code") + val countryCode: Int, + + @ColumnInfo(name = "user_region_country_name") + val countryName: String +) \ No newline at end of file diff --git a/app/src/main/java/com/developersbreach/composeactors/ui/components/RegionEditDialog.kt b/app/src/main/java/com/developersbreach/composeactors/ui/components/RegionEditDialog.kt index 6443c91..d37e568 100644 --- a/app/src/main/java/com/developersbreach/composeactors/ui/components/RegionEditDialog.kt +++ b/app/src/main/java/com/developersbreach/composeactors/ui/components/RegionEditDialog.kt @@ -16,6 +16,8 @@ import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.MaterialTheme import androidx.compose.material.Text +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.rounded.Close import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf @@ -23,16 +25,18 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip -import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Popup import com.developersbreach.composeactors.R -import java.util.* +import com.developersbreach.composeactors.utils.getCountryListWithCode @Composable -fun RegionEditDialog(popupState: MutableState, onRegionSelect: (String, String) -> Unit) { +fun RegionEditDialog( + popupState: MutableState, + onRegionSelect: (String, String) -> Unit +) { if (popupState.value) { val regions = getCountryListWithCode() Popup(alignment = Alignment.Center, onDismissRequest = { popupState.value = false }) { @@ -98,7 +102,7 @@ fun Header(popupState: MutableState) { style = MaterialTheme.typography.subtitle1 ) Image( - painter = painterResource(id = R.drawable.ic_close), + imageVector = Icons.Rounded.Close, contentDescription = stringResource(R.string.close_pop_up_icon), modifier = Modifier.clickable { popupState.value = false @@ -107,17 +111,6 @@ fun Header(popupState: MutableState) { } } -private fun getCountryListWithCode(): List { - val region = arrayListOf() - val isoCountryCodes: Array = Locale.getISOCountries() - for (countryCode in isoCountryCodes) { - val locale = Locale("", countryCode) - val countryName: String = locale.displayCountry - region.add("$countryCode,$countryName") - } - return region -} - @Composable fun DropdownMenuItem( text: String, diff --git a/app/src/main/java/com/developersbreach/composeactors/utils/Utilities.kt b/app/src/main/java/com/developersbreach/composeactors/utils/Utilities.kt index 0a95d5f..3774688 100644 --- a/app/src/main/java/com/developersbreach/composeactors/utils/Utilities.kt +++ b/app/src/main/java/com/developersbreach/composeactors/utils/Utilities.kt @@ -59,4 +59,15 @@ fun getMovieRuntimeFormatted( val hours: Int? = runtime?.div(60) val minutes: Int? = runtime?.rem(60) return "${hours}h:${minutes}m" +} + +fun getCountryListWithCode(): List { + val region = arrayListOf() + val isoCountryCodes: Array = Locale.getISOCountries() + for (countryCode in isoCountryCodes) { + val locale = Locale("", countryCode) + val countryName: String = locale.displayCountry + region.add("$countryCode,$countryName") + } + return region } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_close.xml b/app/src/main/res/drawable/ic_close.xml deleted file mode 100644 index 35b1348..0000000 --- a/app/src/main/res/drawable/ic_close.xml +++ /dev/null @@ -1,10 +0,0 @@ - - -