Skip to content

Commit

Permalink
Added app version in settings screen (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob729 authored Jun 7, 2024
1 parent 8fef3c3 commit fda33ba
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
97 changes: 59 additions & 38 deletions app/src/main/java/com/rob729/newsfeed/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.rob729.newsfeed.AppPreferences
import com.rob729.newsfeed.model.ui.IconData
Expand All @@ -23,9 +28,11 @@ import com.rob729.newsfeed.ui.components.Separator
import com.rob729.newsfeed.ui.components.SettingsCategory
import com.rob729.newsfeed.ui.components.SwitchPreference
import com.rob729.newsfeed.ui.components.Toolbar
import com.rob729.newsfeed.ui.theme.lexendDecaFontFamily
import com.rob729.newsfeed.utils.CommonUtils
import com.rob729.newsfeed.utils.Constants
import com.rob729.newsfeed.utils.Constants.SETTINGS_TOOLBAR_TITLE
import com.rob729.newsfeed.utils.SettingsConstants
import com.rob729.newsfeed.utils.Theme
import com.rob729.newsfeed.utils.name
import com.rob729.newsfeed.utils.toAppTheme
import com.rob729.newsfeed.vm.SettingsViewModel
Expand All @@ -37,15 +44,10 @@ fun SettingsScreen(
viewModel: SettingsViewModel = koinViewModel()
) {

val context = LocalContext.current
val appPreference =
viewModel.appPreferencesFlow.collectAsState(initial = AppPreferences.getDefaultInstance())

val themes = listOf(
Theme.SYSTEM to "System default",
Theme.LIGHT to "Light",
Theme.DARK to "Dark"
)

Box(
modifier = Modifier
.fillMaxSize()
Expand All @@ -58,41 +60,60 @@ fun SettingsScreen(
toolbarElevation = 12.dp
)

LazyColumn {
item {
SettingsCategory(
modifier = Modifier.padding(horizontal = 16.dp),
text = "Interface"
)
}
Column {
SettingsCategory(
modifier = Modifier.padding(horizontal = 16.dp),
text = SettingsConstants.INTERFACE_SECTION_HEADER_TITLE
)

Spacer(modifier = Modifier.height(8.dp))

DropDownPreference(
title = SettingsConstants.THEME_PREF_TITLE,
subtitle = appPreference.value.theme.name(),
items = Constants.themes,
onItemSelected = {
viewModel.updatePreference { appPreferences ->
appPreferences.toBuilder().setTheme(it.toAppTheme()).build()
}
})

item { Spacer(modifier = Modifier.height(8.dp)) }
Separator()

item {
DropDownPreference(
title = SettingsConstants.THEME_PREF_TITLE,
subtitle = appPreference.value.theme.name(),
items = themes,
onItemSelected = {
viewModel.updatePreference { appPreferences ->
appPreferences.toBuilder().setTheme(it.toAppTheme()).build()
}
})
SwitchPreference(
title = SettingsConstants.IN_APP_BROWSER_PREF_TITLE,
subtitle = SettingsConstants.IN_APP_BROWSER_PREF_SUBTITLE,
checked = appPreference.value.shouldOpenLinksUsingInAppBrowser
) { shouldOpenLinksUsingInAppBrowser ->
viewModel.updatePreference { appPreference ->
appPreference.toBuilder().setShouldOpenLinksUsingInAppBrowser(
shouldOpenLinksUsingInAppBrowser
).build()
}
}

item { Separator() }
Box(modifier = Modifier
.fillMaxSize()
.padding(bottom = 16.dp)) {
Column(modifier = Modifier.align(Alignment.BottomCenter)) {
Text(
text = Constants.HOME_TOOLBAR_TITLE,
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
fontSize = 18.sp,
fontFamily = lexendDecaFontFamily,
)

item {
SwitchPreference(
title = SettingsConstants.IN_APP_BROWSER_PREF_TITLE,
subtitle = SettingsConstants.IN_APP_BROWSER_PREF_SUBTITLE,
checked = appPreference.value.shouldOpenLinksUsingInAppBrowser
) { shouldOpenLinksUsingInAppBrowser ->
viewModel.updatePreference { appPreference ->
appPreference.toBuilder().setShouldOpenLinksUsingInAppBrowser(
shouldOpenLinksUsingInAppBrowser
).build()
}
Spacer(modifier = Modifier.height(4.dp))

Text(
modifier = Modifier.align(Alignment.CenterHorizontally),
text = "v${CommonUtils.getAppVersion(context)}",
textAlign = TextAlign.Center,
fontWeight = FontWeight.SemiBold,
fontSize = 14.sp,
fontFamily = lexendDecaFontFamily,
)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/rob729/newsfeed/utils/CommonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ object CommonUtils {
data = Uri.parse(url)
})
}

fun getAppVersion(context: Context): String =
context.packageManager.getPackageInfo(context.packageName, 0).versionName
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/rob729/newsfeed/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ object Constants {
const val PREFS_NAME = "prefs_name"
const val MAX_TOOLBAR_ELEVATION = 12
const val OVERFLOW_MENU_ITEM_SETTINGS = "Settings"
val themes = listOf(
Theme.SYSTEM to "System default",
Theme.LIGHT to "Light",
Theme.DARK to "Dark"
)
}

object SettingsConstants {
const val THEME_PREF_TITLE = "Theme"
const val IN_APP_BROWSER_PREF_TITLE = "Use in-app browser"
const val IN_APP_BROWSER_PREF_SUBTITLE = "When turned off, links open in your default browser"
const val INTERFACE_SECTION_HEADER_TITLE = "Interface"
}

enum class ScreenType {
Expand Down

0 comments on commit fda33ba

Please sign in to comment.