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


45556897 pyrossh

1 year ago
implement ShareKit
composeApp/src/androidMain/kotlin/dev/pyrossh/only_bible_app/MainActivity.kt CHANGED
@@ -19,8 +19,8 @@ class MainActivity : ComponentActivity() {
19
19
 
20
20
  override fun onCreate(savedInstanceState: Bundle?) {
21
21
  super.onCreate(savedInstanceState)
22
-
23
22
  enableEdgeToEdge()
23
+ ShareKit.setActivityProvider { return@setActivityProvider this }
24
24
  if (savedInstanceState == null) {
25
25
  lifecycleScope.launch {
26
26
  model.loadData(settings)
composeApp/src/androidMain/kotlin/dev/pyrossh/only_bible_app/Platform.android.kt CHANGED
@@ -1,5 +1,7 @@
1
1
  package dev.pyrossh.only_bible_app
2
2
 
3
+ import android.app.Activity
4
+ import android.app.Activity.RESULT_OK
3
5
  import android.content.Intent
4
6
  import android.view.SoundEffectConstants
5
7
  import androidx.activity.ComponentActivity
@@ -32,27 +34,28 @@ actual fun getScreenHeight(): Dp = LocalConfiguration.current.screenHeightDp.dp
32
34
  @Composable
33
35
  actual fun playClickSound() = LocalView.current.playSoundEffect(SoundEffectConstants.CLICK)
34
36
 
35
- @Composable
37
+
36
- actual fun rememberShareVerses(): (verses: List<Verse>) -> Unit {
37
- val context = LocalContext.current
38
- return { verses ->
39
- val versesThrough =
38
+ actual object ShareKit {
39
+
40
- if (verses.size >= 3) "${verses.first().verseIndex + 1}-${verses.last().verseIndex + 1}" else verses.map { it.verseIndex + 1 }
40
+ private var activityProvider: () -> Activity = {
41
- .joinToString(",")
41
+ throw IllegalArgumentException(
42
- val title = "${verses[0].bookName} ${verses[0].chapterIndex + 1}:${versesThrough}"
42
+ "You need to implement the 'activityProvider' to provide the required Activity. " +
43
- val text = verses.joinToString("\n") {
43
+ "Just make sure to set a valid activity using " +
44
- it.text.replace("<span style=\"color:red;\">", "")
45
- .replace("<em>", "")
46
- .replace("</span>", "")
44
+ "the 'setActivityProvider()' method."
47
- .replace("</em>", "")
45
+ )
48
- }
46
+ }
47
+
48
+ fun setActivityProvider(provider: () -> Activity) {
49
+ activityProvider = provider
50
+ }
51
+
52
+ actual fun shareText(text: String) {
49
- val sendIntent = Intent().apply {
53
+ val intent = Intent(Intent.ACTION_SEND).apply {
50
- action = Intent.ACTION_SEND
51
- putExtra(Intent.EXTRA_TEXT, "${title}\n${text}")
52
54
  type = "text/plain"
55
+ putExtra(Intent.EXTRA_TEXT, text)
53
56
  }
54
- val shareIntent = Intent.createChooser(sendIntent, null)
57
+ val intentChooser = Intent.createChooser(intent, null)
55
- context.startActivity(shareIntent)
58
+ activityProvider.invoke().startActivity(intentChooser)
56
59
  }
57
60
  }
58
61
 
composeApp/src/commonMain/kotlin/dev/pyrossh/only_bible_app/Platform.kt CHANGED
@@ -15,8 +15,9 @@ expect fun getScreenHeight(): Dp
15
15
  @Composable
16
16
  expect fun playClickSound()
17
17
 
18
- @Composable
18
+ expect object ShareKit {
19
- expect fun rememberShareVerses(): (verses: List<Verse>) -> Unit
19
+ fun shareText(text: String)
20
+ }
20
21
 
21
22
  @Composable
22
23
  expect fun onThemeChange(themeType: ThemeType)
composeApp/src/commonMain/kotlin/dev/pyrossh/only_bible_app/composables/VerseText.kt CHANGED
@@ -50,6 +50,7 @@ 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.ShareKit
53
54
  import dev.pyrossh.only_bible_app.SpeechService
54
55
  import dev.pyrossh.only_bible_app.darkHighlights
55
56
  import dev.pyrossh.only_bible_app.domain.Verse
@@ -58,7 +59,6 @@ import kotlinx.coroutines.Dispatchers
58
59
  import kotlinx.coroutines.IO
59
60
  import kotlinx.coroutines.launch
60
61
  import dev.pyrossh.only_bible_app.lightHighlights
61
- import dev.pyrossh.only_bible_app.rememberShareVerses
62
62
  import dev.pyrossh.only_bible_app.utils.SimpleParser
63
63
  import dev.pyrossh.only_bible_app.utils.TagNode
64
64
  import dev.pyrossh.only_bible_app.utils.TextNode
@@ -190,7 +190,6 @@ private fun Menu(
190
190
  val navController = LocalNavController.current
191
191
  val scope = rememberCoroutineScope()
192
192
  val selectedVerses by model.selectedVerses.collectAsState()
193
- val shareVerses = rememberShareVerses()
194
193
  Popup(
195
194
  alignment = Alignment.TopCenter,
196
195
  offset = IntOffset(0, y = barYPosition),
@@ -274,9 +273,17 @@ private fun Menu(
274
273
  }
275
274
  IconButton(onClick = {
276
275
  // view.playSoundEffect(SoundEffectConstants.CLICK)
277
- shareVerses(
278
- selectedVerses.sortedBy { it.verseIndex },
276
+ val verses = selectedVerses.sortedBy { it.verseIndex }
277
+ val versesThrough =
278
+ if (verses.size >= 3) "${verses.first().verseIndex + 1}-${verses.last().verseIndex + 1}" else verses.map { it.verseIndex + 1 }
279
+ .joinToString(",")
280
+ val title =
281
+ "${verses[0].bookName} ${verses[0].chapterIndex + 1}:${versesThrough}"
282
+ val text = verses.joinToString("\n") {
283
+ it.text.replace("<red>", "")
284
+ .replace("</red>", "")
279
- )
285
+ }
286
+ ShareKit.shareText("$title\n$text")
280
287
  model.setSelectedVerses(listOf())
281
288
  }) {
282
289
  Icon(
composeApp/src/iosMain/kotlin/dev/pyrossh/only_bible_app/MainViewController.kt CHANGED
@@ -1,11 +1,30 @@
1
1
  package dev.pyrossh.only_bible_app
2
2
 
3
+ import androidx.compose.ui.uikit.ComposeUIViewControllerDelegate
3
4
  import androidx.compose.ui.window.ComposeUIViewController
4
5
  import com.russhwolf.settings.NSUserDefaultsSettings
6
+ import kotlinx.cinterop.ExperimentalForeignApi
7
+ import platform.Foundation.NSNotification
8
+ import platform.Foundation.NSNotificationCenter
5
9
 
6
- fun MainViewController() = ComposeUIViewController {
7
- val settings = NSUserDefaultsSettings.Factory().create()
10
+ val settings = NSUserDefaultsSettings.Factory().create()
8
- val model = AppViewModel()
11
+ val model = AppViewModel()
12
+
13
+ @OptIn(ExperimentalForeignApi::class)
14
+ fun MainViewController() = ComposeUIViewController(configure = {
15
+ delegate = object : ComposeUIViewControllerDelegate {
16
+ override fun viewWillAppear(animated: Boolean) {
17
+ super.viewWillAppear(animated)
9
- model.loadData(settings)
18
+ model.loadData(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
+ }) {
10
29
  App(model = model)
11
30
  }
composeApp/src/iosMain/kotlin/dev/pyrossh/only_bible_app/Platform.ios.kt CHANGED
@@ -10,6 +10,8 @@ import androidx.compose.ui.unit.Dp
10
10
  import androidx.compose.ui.unit.dp
11
11
  import dev.pyrossh.only_bible_app.config.BuildKonfig
12
12
  import dev.pyrossh.only_bible_app.domain.Verse
13
+ import platform.UIKit.UIActivityViewController
14
+ import platform.UIKit.UIApplication
13
15
  import platform.UIKit.UIScreen
14
16
  import theme.darkScheme
15
17
  import theme.lightScheme
@@ -32,9 +34,16 @@ actual fun playClickSound() {
32
34
  // AudioServicesPlayAlertSound(SystemSoundID(1322))
33
35
  }
34
36
 
35
- @Composable
36
- actual fun rememberShareVerses(): (verses: List<Verse>) -> Unit {
37
- return { verses ->
37
+ actual object ShareKit {
38
+
39
+ actual fun shareText(text: String) {
40
+ val currentViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
41
+ val activityViewController = UIActivityViewController(listOf(text), null)
42
+ currentViewController?.presentViewController(
43
+ viewControllerToPresent = activityViewController,
44
+ animated = true,
45
+ completion = null
46
+ )
38
47
  }
39
48
  }
40
49