Skip to content

Commit

Permalink
fix nightMode
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrossh committed Jul 25, 2024
1 parent 702bdb1 commit 2f264a5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 61 deletions.
9 changes: 0 additions & 9 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions app/src/main/java/dev/pyrossh/onlyBible/AppTheme.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.pyrossh.onlyBible

import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED
import android.app.UiModeManager
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
Expand Down Expand Up @@ -44,8 +42,7 @@ val darkHighlights = listOf(


fun isLightTheme(uiMode: Int, isSystemDark: Boolean): Boolean {
val maskedMode = uiMode and UI_MODE_NIGHT_MASK
return maskedMode == UI_MODE_NIGHT_NO || (maskedMode == UI_MODE_NIGHT_UNDEFINED && !isSystemDark)
return uiMode == UiModeManager.MODE_NIGHT_NO || (uiMode == UiModeManager.MODE_NIGHT_AUTO && !isSystemDark)
}

@Composable
Expand All @@ -55,7 +52,7 @@ fun AppTheme(
) {
val context = LocalContext.current
val systemUiController = rememberSystemUiController()
val colorScheme = if (isLightTheme(model.uiMode, isSystemInDarkTheme()))
val colorScheme = if (isLightTheme(model.nightMode, isSystemInDarkTheme()))
dynamicLightColorScheme(context).copy(
onSurface = Color.Black,
outline = Color.LightGray,
Expand All @@ -66,8 +63,7 @@ fun AppTheme(
surface = Color(0xFF090F12),
outline = Color(0xAA5D4979),
)
println("AppTheme ${model.uiMode}")
LaunchedEffect(key1 = model.uiMode) {
LaunchedEffect(key1 = model.nightMode) {
systemUiController.setSystemBarsColor(
color = colorScheme.background
)
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/dev/pyrossh/onlyBible/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package dev.pyrossh.onlyBible
import android.app.Application
import android.app.LocaleConfig
import android.app.LocaleManager
import android.app.UiModeManager
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.Context.UI_MODE_SERVICE
import android.content.Intent
import android.os.Build
import android.os.LocaleList
Expand Down Expand Up @@ -72,7 +74,8 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
var fontType by mutableStateOf(FontType.Sans)
var fontSizeDelta by mutableIntStateOf(0)
var fontBoldEnabled by mutableStateOf(false)
var uiMode by mutableIntStateOf(0)
var lineSpacingDelta by mutableStateOf(0)
var nightMode by mutableStateOf(UiModeManager.MODE_NIGHT_AUTO)
var scrollState = LazyListState(
0,
0
Expand Down Expand Up @@ -129,9 +132,14 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
showBottomSheet = false
}

fun setApplicationNightMode(v: Int) {
val uiModeManager = context.getSystemService(UI_MODE_SERVICE) as UiModeManager
uiModeManager.setApplicationNightMode(v)
nightMode = v
}

fun loadData() {
viewModelScope.launch(Dispatchers.IO) {
uiMode = context.applicationContext.resources.configuration.uiMode
loadedOnce = prefs.getBoolean("loadedOnce", false)
if (!loadedOnce) {
initLocales()
Expand All @@ -144,6 +152,8 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
)
fontSizeDelta = prefs.getInt("fontSizeDelta", 0)
fontBoldEnabled = prefs.getBoolean("fontBoldEnabled", false)
lineSpacingDelta = prefs.getInt("lineSpacingDelta", 0)
nightMode = prefs.getInt("nightMode", UiModeManager.MODE_NIGHT_AUTO)
highlightedVerses.value = JSONObject(prefs.getString("highlightedVerses", "{}") ?: "{}")
scrollState = LazyListState(
prefs.getInt("scrollIndex", 0),
Expand Down Expand Up @@ -197,6 +207,8 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
putString("fontType", fontType.name)
putInt("fontSizeDelta", fontSizeDelta)
putBoolean("fontBoldEnabled", fontBoldEnabled)
putInt("lineSpacingDelta", lineSpacingDelta)
putInt("nightMode", nightMode)
putString("highlightedVerses", highlightedVerses.value.toString())
putInt("scrollIndex", scrollState.firstVisibleItemIndex)
putInt("scrollOffset", scrollState.firstVisibleItemScrollOffset)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/dev/pyrossh/onlyBible/ChapterScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ fun ChapterScreen(
) {
val view = LocalView.current
val context = LocalContext.current
println("Locales ${LocaleList.current.localeList.joinToString { "${it}" }}")
val verses by model.verses.collectAsState()
val bookNames by model.bookNames.collectAsState()
var selectedVerses by rememberSaveable {
Expand Down Expand Up @@ -260,7 +259,7 @@ fun ChapterScreen(
state = rememberSaveable(saver = LazyListState.Saver) {
model.scrollState
},
verticalArrangement = Arrangement.spacedBy(12.dp),
verticalArrangement = Arrangement.spacedBy(16.dp + (model.lineSpacingDelta * 2).dp),
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/dev/pyrossh/onlyBible/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package dev.pyrossh.onlyBible

import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch

Expand All @@ -21,7 +20,6 @@ class MainActivity : AppCompatActivity() {
model.loadData()
}
addOnConfigurationChangedListener {
model.uiMode = it.uiMode
}
setContent {
AppTheme {
Expand Down
48 changes: 18 additions & 30 deletions app/src/main/java/dev/pyrossh/onlyBible/TextSettingsBottomSheet.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package dev.pyrossh.onlyBible

import android.app.UiModeManager
import android.content.Context.UI_MODE_SERVICE
import android.content.res.Configuration
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.app.UiModeManager.MODE_NIGHT_AUTO
import android.app.UiModeManager.MODE_NIGHT_NO
import android.app.UiModeManager.MODE_NIGHT_YES
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -37,7 +34,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
Expand All @@ -47,8 +43,6 @@ import kotlinx.coroutines.launch
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun TextSettingsBottomSheet(model: AppViewModel) {
val uiModeManager = LocalContext.current.getSystemService(UI_MODE_SERVICE) as UiModeManager
val uiMode = model.uiMode and Configuration.UI_MODE_NIGHT_MASK
val scope = rememberCoroutineScope()
val sheetState = rememberModalBottomSheetState()
return ModalBottomSheet(
Expand Down Expand Up @@ -168,7 +162,13 @@ fun TextSettingsBottomSheet(model: AppViewModel) {
.height(60.dp)
.padding(end = 16.dp)
.weight(1f),
onClick = {}) {
onClick = {
if (model.lineSpacingDelta > 5) {
model.lineSpacingDelta = 0
} else {
model.lineSpacingDelta += 1
}
}) {
Column(
modifier = Modifier.background(MaterialTheme.colorScheme.background),
verticalArrangement = Arrangement.Center,
Expand Down Expand Up @@ -232,17 +232,17 @@ fun TextSettingsBottomSheet(model: AppViewModel) {
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
) {
listOf(UI_MODE_NIGHT_NO, UI_MODE_NIGHT_YES, UI_MODE_NIGHT_UNDEFINED).map {
listOf(MODE_NIGHT_NO, MODE_NIGHT_YES, MODE_NIGHT_AUTO).map {
Surface(
shape = RoundedCornerShape(8.dp),
border = if (uiMode == it) BorderStroke(
border = if (model.nightMode == it) BorderStroke(
2.dp, MaterialTheme.colorScheme.primary
) else null,
color = if (uiMode == it)
color = if (model.nightMode == it)
MaterialTheme.colorScheme.primary
else
MaterialTheme.colorScheme.onSurface,
contentColor = if (uiMode == it)
contentColor = if (model.nightMode == it)
MaterialTheme.colorScheme.primary
else
MaterialTheme.colorScheme.onSurface,
Expand All @@ -255,40 +255,28 @@ fun TextSettingsBottomSheet(model: AppViewModel) {
scope.launch {
sheetState.hide()
model.closeSheet()
when (it) {
UI_MODE_NIGHT_NO -> uiModeManager.setApplicationNightMode(
UiModeManager.MODE_NIGHT_NO
)

UI_MODE_NIGHT_YES -> uiModeManager.setApplicationNightMode(
UiModeManager.MODE_NIGHT_YES
)

UI_MODE_NIGHT_UNDEFINED -> uiModeManager.setApplicationNightMode(
UiModeManager.MODE_NIGHT_AUTO
)
}
model.setApplicationNightMode(it)
}
}
) {
when (it) {
UI_MODE_NIGHT_NO -> Icon(
MODE_NIGHT_NO -> Icon(
imageVector = Icons.Filled.LightMode,
contentDescription = "Light",
modifier = Modifier
.background(MaterialTheme.colorScheme.background)
.padding(12.dp)
)

UI_MODE_NIGHT_YES -> Icon(
MODE_NIGHT_YES -> Icon(
imageVector = Icons.Filled.DarkMode,
contentDescription = "Dark",
modifier = Modifier
.background(MaterialTheme.colorScheme.background)
.padding(12.dp)
)

UI_MODE_NIGHT_UNDEFINED -> Column(
MODE_NIGHT_AUTO -> Column(
modifier = Modifier.background(
color = MaterialTheme.colorScheme.background,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun VerseView(
var barYPosition by remember {
mutableIntStateOf(0)
}
val isLight = isLightTheme(model.uiMode, isSystemInDarkTheme())
val isLight = isLightTheme(model.nightMode, isSystemInDarkTheme())
val fontSizeDelta = model.fontSizeDelta
val boldWeight = if (model.fontBoldEnabled) FontWeight.W700 else FontWeight.W400
val buttonInteractionSource = remember { MutableInteractionSource() }
Expand Down
8 changes: 2 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ The only bible app you will ever need.
No ads, No in-app purchases, No distractions.

Optimized for reading and highlighting.
Only Bibles which are in the public domain are available.
Verse by verse audio is also supported for some of the languages using Azure TTS.
Bibles which are in the public domain are supported.
Verse by verse audio playback is supported for most of the languages using Azure TTS.
Many languages supported Indian, European and Asian.

## Setup
Expand Down Expand Up @@ -37,7 +37,3 @@ fastlane supply --aab ../build/app/outputs/bundle/release/app-release.aab
flutter build ipa --release --dart-define-from-file=.env
fastlane deliver --ipa "../build/ios/ipa/only-bible-app.ipa" --automatic_release --submit_for_review
```

## TODO

* Improve Paging in dark mode

0 comments on commit 2f264a5

Please sign in to comment.