~repos /only-bible-app

#kotlin#android#ios

git clone https://pyrossh.dev/repos/only-bible-app.git

The only bible app you will ever need. No ads. No in-app purchases. No distractions.


3a7f9b60 Peter John

1 year ago
fix locales
.idea/deploymentTargetSelector.xml CHANGED
@@ -4,10 +4,10 @@
4
4
  <selectionStates>
5
5
  <SelectionState runConfigName="app">
6
6
  <option name="selectionMode" value="DROPDOWN" />
7
- <DropdownSelection timestamp="2024-07-23T14:38:17.117558Z">
7
+ <DropdownSelection timestamp="2024-07-25T14:10:39.254239Z">
8
8
  <Target type="DEFAULT_BOOT">
9
9
  <handle>
10
- <DeviceId pluginId="LocalEmulator" identifier="path=/Users/peterjohn/.android/avd/Pixel_8_Pro_API_30.avd" />
10
+ <DeviceId pluginId="LocalEmulator" identifier="path=/Users/peterjohn/.android/avd/Pixel_8_API_34-ext11.avd" />
11
11
  </handle>
12
12
  </Target>
13
13
  </DropdownSelection>
app/src/main/java/dev/pyrossh/onlyBible/AppDrawer.kt CHANGED
@@ -102,7 +102,7 @@ fun AppDrawer(
102
102
  .padding(bottom = 16.dp),
103
103
  ) {
104
104
  if (menuType == MenuType.Bible) {
105
- val locales = getSupportedLocales(context)
105
+ val locales = context.getSupportedLocales()
106
106
  LazyVerticalGrid(
107
107
  modifier = Modifier.fillMaxSize(),
108
108
  verticalArrangement = Arrangement.spacedBy(10.dp),
@@ -140,7 +140,7 @@ fun AppDrawer(
140
140
  scope.launch {
141
141
  drawerState.close()
142
142
  }.invokeOnCompletion {
143
- setLocale(context, loc)
143
+ context.setLocale(loc)
144
144
  }
145
145
  }
146
146
  }
app/src/main/java/dev/pyrossh/onlyBible/AppViewModel.kt CHANGED
@@ -60,6 +60,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
60
60
  speechService.SynthesisCompleted.addEventListener(completed)
61
61
  }
62
62
 
63
+ private var loadedOnce = false;
63
64
  var isPlaying by mutableStateOf(false)
64
65
  val verses = MutableStateFlow(listOf<Verse>())
65
66
  val bookNames = MutableStateFlow(engTitles)
@@ -130,17 +131,11 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
130
131
 
131
132
  fun loadData() {
132
133
  viewModelScope.launch(Dispatchers.IO) {
133
- // Run this only once
134
- // AppCompatDelegate.setApplicationLocales(
135
- // LocaleListCompat.create(
136
- // Locale("en"),
137
- // Locale("hi"),
138
- // Locale("ne"),
139
- // Locale("pa"),
140
- // Locale("ta"),
141
- // )
142
- // )
143
134
  uiMode = context.applicationContext.resources.configuration.uiMode
135
+ loadedOnce = prefs.getBoolean("loadedOnce", false)
136
+ if (!loadedOnce) {
137
+ initLocales()
138
+ }
144
139
  bookIndex = prefs.getInt("bookIndex", 0)
145
140
  chapterIndex = prefs.getInt("chapterIndex", 0)
146
141
  fontType =
@@ -163,7 +158,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
163
158
  val buffer =
164
159
  context.assets.open(
165
160
  "bibles/${
166
- getCurrentLocale(context).getDisplayLanguage(Locale.ENGLISH)
161
+ context.getCurrentLocale().getDisplayLanguage(Locale.ENGLISH)
167
162
  }.txt"
168
163
  )
169
164
  .bufferedReader()
@@ -196,6 +191,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
196
191
  fun saveData() {
197
192
  viewModelScope.launch(Dispatchers.IO) {
198
193
  with(prefs.edit()) {
194
+ putBoolean("loadedOnce", true)
199
195
  putInt("bookIndex", bookIndex)
200
196
  putInt("chapterIndex", chapterIndex)
201
197
  putString("fontType", fontType.name)
@@ -277,21 +273,44 @@ fun shareVerses(context: Context, verses: List<Verse>) {
277
273
  context.startActivity(shareIntent)
278
274
  }
279
275
 
276
+ fun initLocales() {
277
+ if (Build.VERSION.SDK_INT >= 33) {
278
+ // do nothing for now
279
+ } else {
280
+ AppCompatDelegate.setApplicationLocales(
281
+ LocaleListCompat.create(
282
+ Locale("en"),
283
+ Locale("bn"),
284
+ Locale("gu"),
285
+ Locale("hi"),
286
+ Locale("kn"),
287
+ Locale("ml"),
288
+ Locale("ne"),
289
+ Locale("or"),
290
+ Locale("pa"),
291
+ Locale("ta"),
292
+ Locale("te"),
293
+ )
294
+ )
295
+ }
296
+ }
297
+
280
- fun getCurrentLocale(context: Context): Locale {
298
+ fun Context.getCurrentLocale(): Locale {
281
299
  return if (Build.VERSION.SDK_INT >= 33) {
282
- context.resources.configuration.locales.get(0)
300
+ resources.configuration.locales.get(0)
283
301
  } else {
284
302
  AppCompatDelegate.getApplicationLocales().get(0) ?: Locale("en")
285
303
  }
286
304
  }
287
305
 
288
- fun getSupportedLocales(context: Context): List<Locale> {
306
+ fun Context.getSupportedLocales(): List<Locale> {
289
307
  if (Build.VERSION.SDK_INT >= 33) {
290
- val localeList = LocaleConfig(context).supportedLocales!!
308
+ val localeList = LocaleConfig(this).supportedLocales!!
291
309
  return arrayOfNulls<String>(localeList.size())
292
310
  .mapIndexed { i, _ -> localeList[i] }
293
311
  .sortedBy { it.getDisplayName(Locale.ENGLISH) }
294
312
  } else {
313
+ androidx.compose.ui.text.intl.LocaleList.current.localeList
295
314
  val localeList = AppCompatDelegate.getApplicationLocales()
296
315
  return arrayOfNulls<String>(localeList.size())
297
316
  .mapIndexed { i, _ -> localeList[i]!! }
@@ -299,12 +318,12 @@ fun getSupportedLocales(context: Context): List<Locale> {
299
318
  }
300
319
  }
301
320
 
302
- fun setLocale(context: Context, loc: Locale) {
321
+ fun Context.setLocale(loc: Locale) {
303
322
  if (Build.VERSION.SDK_INT >= 33) {
304
- val localeManager = context.getSystemService(LocaleManager::class.java)
323
+ val localeManager = getSystemService(LocaleManager::class.java)
305
324
  localeManager.applicationLocales = LocaleList(loc)
306
325
  } else {
307
- val locales = (listOf(loc) + getSupportedLocales(context))
326
+ val locales = (listOf(loc) + getSupportedLocales())
308
327
  .joinToString(separator = ",") { it.language }
309
328
  AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(locales))
310
329
  }
app/src/main/java/dev/pyrossh/onlyBible/ChapterScreen.kt CHANGED
@@ -41,6 +41,7 @@ import androidx.compose.ui.platform.LocalContext
41
41
  import androidx.compose.ui.platform.LocalView
42
42
  import androidx.compose.ui.text.TextStyle
43
43
  import androidx.compose.ui.text.font.FontWeight
44
+ import androidx.compose.ui.text.intl.LocaleList
44
45
  import androidx.compose.ui.unit.dp
45
46
  import androidx.compose.ui.unit.sp
46
47
  import dev.pyrossh.onlyBible.composables.EmbeddedSearchBar
@@ -51,6 +52,7 @@ import kotlinx.parcelize.Parcelize
51
52
  import kotlinx.serialization.Serializable
52
53
  import kotlin.math.abs
53
54
 
55
+
54
56
  @Serializable
55
57
  @Parcelize
56
58
  data class ChapterScreenProps(
@@ -108,6 +110,7 @@ suspend fun PointerInputScope.detectSwipe(
108
110
  }
109
111
  )
110
112
 
113
+
111
114
  @OptIn(ExperimentalMaterial3Api::class)
112
115
  @Composable
113
116
  fun ChapterScreen(
@@ -120,6 +123,7 @@ fun ChapterScreen(
120
123
  ) {
121
124
  val view = LocalView.current
122
125
  val context = LocalContext.current
126
+ println("Locales ${LocaleList.current.localeList.joinToString { "${it}" }}")
123
127
  val verses by model.verses.collectAsState()
124
128
  val bookNames by model.bookNames.collectAsState()
125
129
  var selectedVerses by rememberSaveable {
@@ -229,7 +233,7 @@ fun ChapterScreen(
229
233
  }
230
234
  TextButton(onClick = { openDrawer(MenuType.Bible, bookIndex) }) {
231
235
  Text(
232
- text = getCurrentLocale(context).language.uppercase(),
236
+ text = context.getCurrentLocale().language.uppercase(),
233
237
  style = TextStyle(
234
238
  fontSize = 18.sp,
235
239
  fontWeight = FontWeight.W500,
app/src/main/java/dev/pyrossh/onlyBible/MainActivity.kt CHANGED
@@ -5,6 +5,8 @@ import androidx.activity.compose.setContent
5
5
  import androidx.activity.enableEdgeToEdge
6
6
  import androidx.activity.viewModels
7
7
  import androidx.appcompat.app.AppCompatActivity
8
+ import androidx.appcompat.app.AppCompatDelegate
9
+ import androidx.core.os.LocaleListCompat
8
10
  import androidx.lifecycle.lifecycleScope
9
11
  import kotlinx.coroutines.launch
10
12