~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.


6c7345b1 pyrossh

1 year ago
Fix LifeCycle
composeApp/src/androidMain/kotlin/dev/pyrossh/only_bible_app/MainActivity.kt CHANGED
@@ -5,9 +5,7 @@ import androidx.activity.ComponentActivity
5
5
  import androidx.activity.compose.setContent
6
6
  import androidx.activity.enableEdgeToEdge
7
7
  import androidx.activity.viewModels
8
- import androidx.lifecycle.lifecycleScope
9
8
  import com.russhwolf.settings.SharedPreferencesSettings
10
- import kotlinx.coroutines.launch
11
9
 
12
10
  class MainActivity : ComponentActivity() {
13
11
 
@@ -21,20 +19,8 @@ class MainActivity : ComponentActivity() {
21
19
  super.onCreate(savedInstanceState)
22
20
  enableEdgeToEdge()
23
21
  ShareKit.setActivityProvider { return@setActivityProvider this }
24
- if (savedInstanceState == null) {
25
- lifecycleScope.launch {
26
- model.loadData(settings)
27
- }
28
- }
29
22
  setContent {
30
- App(model = model)
31
- }
32
- }
33
-
34
- override fun onSaveInstanceState(outState: Bundle) {
35
- super.onSaveInstanceState(outState)
36
- lifecycleScope.launch {
37
- model.saveData(settings)
23
+ App(model = model, settings = settings)
38
24
  }
39
25
  }
40
26
  }
composeApp/src/androidMain/kotlin/dev/pyrossh/only_bible_app/Platform.android.kt CHANGED
@@ -25,6 +25,8 @@ import dev.pyrossh.only_bible_app.config.BuildKonfig
25
25
  import theme.darkScheme
26
26
  import theme.lightScheme
27
27
 
28
+ actual fun getPlatform() = Platform.Android
29
+
28
30
  @Composable
29
31
  actual fun getScreenWidth(): Dp = LocalConfiguration.current.screenWidthDp.dp
30
32
 
composeApp/src/commonMain/kotlin/dev/pyrossh/only_bible_app/App.kt CHANGED
@@ -1,10 +1,24 @@
1
1
  package dev.pyrossh.only_bible_app
2
2
 
3
3
  import androidx.compose.runtime.Composable
4
+ import androidx.lifecycle.DefaultLifecycleObserver
5
+ import androidx.lifecycle.LifecycleOwner
6
+ import androidx.lifecycle.compose.LocalLifecycleOwner
4
7
  import androidx.lifecycle.viewmodel.compose.viewModel
8
+ import com.russhwolf.settings.Settings
5
9
 
6
10
  @Composable
7
- fun App(model: AppViewModel = viewModel { AppViewModel() }) {
11
+ fun App(model: AppViewModel = viewModel { AppViewModel() }, settings: Settings) {
12
+ LocalLifecycleOwner.current.lifecycle.addObserver(object : DefaultLifecycleObserver {
13
+ override fun onCreate(owner: LifecycleOwner) {
14
+ super.onCreate(owner)
15
+ model.loadData(settings)
16
+ }
17
+ override fun onPause(owner: LifecycleOwner) {
18
+ super.onPause(owner)
19
+ model.saveData(settings)
20
+ }
21
+ })
8
22
  AppTheme(themeType = model.themeType) {
9
23
  AppHost(model = model)
10
24
  }
composeApp/src/commonMain/kotlin/dev/pyrossh/only_bible_app/Platform.kt CHANGED
@@ -1,11 +1,16 @@
1
1
  package dev.pyrossh.only_bible_app
2
2
 
3
- import androidx.compose.material3.ColorScheme
4
3
  import androidx.compose.runtime.Composable
5
4
  import androidx.compose.ui.unit.Dp
6
- import dev.pyrossh.only_bible_app.domain.Verse
7
5
 
8
6
 
7
+ enum class Platform {
8
+ Android,
9
+ IOS
10
+ }
11
+
12
+ expect fun getPlatform(): Platform
13
+
9
14
  @Composable
10
15
  expect fun getScreenWidth(): Dp
11
16
 
composeApp/src/commonMain/kotlin/dev/pyrossh/only_bible_app/composables/VerseText.kt CHANGED
@@ -50,10 +50,12 @@ import androidx.compose.ui.unit.IntOffset
50
50
  import androidx.compose.ui.unit.dp
51
51
  import androidx.compose.ui.unit.sp
52
52
  import androidx.compose.ui.window.Popup
53
+ import dev.pyrossh.only_bible_app.Platform
53
54
  import dev.pyrossh.only_bible_app.ShareKit
54
55
  import dev.pyrossh.only_bible_app.SpeechService
55
56
  import dev.pyrossh.only_bible_app.darkHighlights
56
57
  import dev.pyrossh.only_bible_app.domain.Verse
58
+ import dev.pyrossh.only_bible_app.getPlatform
57
59
  import dev.pyrossh.only_bible_app.isLightTheme
58
60
  import kotlinx.coroutines.Dispatchers
59
61
  import kotlinx.coroutines.IO
@@ -250,26 +252,28 @@ private fun Menu(
250
252
  )
251
253
  }
252
254
  }
255
+ if (getPlatform() == Platform.Android) {
253
- IconButton(onClick = {
256
+ IconButton(onClick = {
254
257
  // view.playSoundEffect(SoundEffectConstants.CLICK)
255
- if (model.isAudioPlaying) {
258
+ if (model.isAudioPlaying) {
256
- SpeechService.stopTextToSpeech()
259
+ SpeechService.stopTextToSpeech()
257
- } else {
260
+ } else {
258
- scope.launch(Dispatchers.IO) {
261
+ scope.launch(Dispatchers.IO) {
259
- for (v in selectedVerses.sortedBy { it.verseIndex }) {
262
+ for (v in selectedVerses.sortedBy { it.verseIndex }) {
260
- SpeechService.startTextToSpeech(model.bible.voiceName, v.text)
263
+ SpeechService.startTextToSpeech(model.bible.voiceName, v.text)
264
+ }
261
265
  }
262
266
  }
263
- }
264
- }) {
267
+ }) {
265
- Icon(
268
+ Icon(
266
269
  // modifier = Modifier.size(36.dp),
267
- imageVector = if (model.isAudioPlaying)
270
+ imageVector = if (model.isAudioPlaying)
268
- Icons.Outlined.PauseCircle
271
+ Icons.Outlined.PauseCircle
269
- else
272
+ else
270
- Icons.Outlined.PlayCircle,
273
+ Icons.Outlined.PlayCircle,
271
- contentDescription = "Audio",
274
+ contentDescription = "Audio",
272
- )
275
+ )
276
+ }
273
277
  }
274
278
  IconButton(onClick = {
275
279
  // view.playSoundEffect(SoundEffectConstants.CLICK)
composeApp/src/iosMain/kotlin/dev/pyrossh/only_bible_app/MainViewController.kt CHANGED
@@ -10,21 +10,6 @@ import platform.Foundation.NSNotificationCenter
10
10
  val settings = NSUserDefaultsSettings.Factory().create()
11
11
  val model = AppViewModel()
12
12
 
13
- @OptIn(ExperimentalForeignApi::class)
14
- fun MainViewController() = ComposeUIViewController(configure = {
13
+ fun MainViewController() = ComposeUIViewController() {
15
- delegate = object : ComposeUIViewControllerDelegate {
16
- override fun viewWillAppear(animated: Boolean) {
17
- super.viewWillAppear(animated)
18
- model.loadData(settings)
14
+ App(model = model, settings = settings)
19
- NSNotificationCenter.defaultCenter.addObserver(this)
20
- }
21
-
22
- override fun viewWillDisappear(animated: Boolean) {
23
- super.viewWillDisappear(animated)
24
- model.saveData(settings)
25
-
26
- }
27
- }
28
- }) {
29
- App(model = model)
30
15
  }
composeApp/src/iosMain/kotlin/dev/pyrossh/only_bible_app/Platform.ios.kt CHANGED
@@ -18,6 +18,8 @@ import theme.lightScheme
18
18
 
19
19
  //import platform.AVKit.Audio
20
20
 
21
+ actual fun getPlatform() = Platform.IOS
22
+
21
23
  @OptIn(ExperimentalComposeUiApi::class)
22
24
  @Composable
23
25
  actual fun getScreenWidth(): Dp = LocalWindowInfo.current.containerSize.width.pxToPoint().dp