diff --git a/examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt b/examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt index d51d73c14d..dfcc639b0f 100755 --- a/examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt +++ b/examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt @@ -1,15 +1,29 @@ package example.imageviewer import android.os.Bundle +import androidx.activity.addCallback import androidx.activity.compose.setContent import androidx.appcompat.app.AppCompatActivity import example.imageviewer.view.ImageViewerAndroid +import kotlinx.coroutines.channels.BufferOverflow +import kotlinx.coroutines.flow.MutableSharedFlow class MainActivity : AppCompatActivity() { + + val externalEvents = MutableSharedFlow( + replay = 0, + extraBufferCapacity = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST, + ) + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - ImageViewerAndroid() + ImageViewerAndroid(externalEvents) + } + onBackPressedDispatcher.addCallback { + externalEvents.tryEmit(ExternalImageViewerEvent.ReturnBack) } } + } diff --git a/examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ImageViewer.android.kt b/examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ImageViewer.android.kt index 54fa4364e4..4c6fac9344 100755 --- a/examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ImageViewer.android.kt +++ b/examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ImageViewer.android.kt @@ -7,28 +7,41 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalContext -import example.imageviewer.* +import example.imageviewer.Dependencies +import example.imageviewer.ExternalImageViewerEvent +import example.imageviewer.ImageViewerCommon +import example.imageviewer.Notification +import example.imageviewer.PopupNotification +import example.imageviewer.SharePicture import example.imageviewer.filter.PlatformContext +import example.imageviewer.ioDispatcher import example.imageviewer.model.PictureData import example.imageviewer.storage.AndroidImageStorage import example.imageviewer.style.ImageViewerTheme import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @Composable -fun ImageViewerAndroid() { +fun ImageViewerAndroid(externalEvents: Flow) { val context: Context = LocalContext.current val ioScope = rememberCoroutineScope { ioDispatcher } - val dependencies = remember(context, ioScope) { getDependencies(context, ioScope) } + val dependencies = remember(context, ioScope) { + getDependencies(context, ioScope, externalEvents) + } ImageViewerTheme { ImageViewerCommon(dependencies) } } -private fun getDependencies(context: Context, ioScope: CoroutineScope) = object : Dependencies() { +private fun getDependencies( + context: Context, + ioScope: CoroutineScope, + externalEvents: Flow +) = object : Dependencies() { override val notification: Notification = object : PopupNotification(localization) { override fun showPopUpMessage(text: String) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show() @@ -53,4 +66,5 @@ private fun getDependencies(context: Context, ioScope: CoroutineScope) = object } } } + override val externalEvents: Flow = externalEvents } diff --git a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt index f9a7491b6e..fcfceed36c 100644 --- a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt +++ b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt @@ -8,9 +8,9 @@ import example.imageviewer.model.* import example.imageviewer.view.* enum class ExternalImageViewerEvent { - Foward, - Back, - Escape, + Next, + Previous, + ReturnBack, } @Composable @@ -38,7 +38,7 @@ internal fun ImageViewerWithProvidedDependencies( val externalEvents = LocalInternalEvents.current LaunchedEffect(Unit) { externalEvents.collect { - if (it == ExternalImageViewerEvent.Escape) { + if (it == ExternalImageViewerEvent.ReturnBack) { navigationStack.back() } } diff --git a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt index 4e6d031618..028129f418 100755 --- a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt +++ b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt @@ -57,8 +57,8 @@ internal fun GalleryScreen( LaunchedEffect(Unit) { externalEvents.collect { when (it) { - ExternalImageViewerEvent.Foward -> nextImage() - ExternalImageViewerEvent.Back -> previousImage() + ExternalImageViewerEvent.Next -> nextImage() + ExternalImageViewerEvent.Previous -> previousImage() else -> {} } } diff --git a/examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ImageViewer.desktop.kt b/examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ImageViewer.desktop.kt index 7d9abafa43..1c302f5089 100755 --- a/examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ImageViewer.desktop.kt +++ b/examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ImageViewer.desktop.kt @@ -24,7 +24,6 @@ import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow -import java.awt.Desktop import java.awt.Dimension import java.awt.Toolkit @@ -63,15 +62,15 @@ fun ApplicationScope.ImageViewerDesktop() { if (it.type == KeyEventType.KeyUp) { when (it.key) { Key.DirectionLeft -> externalNavigationEventBus.produceEvent( - ExternalImageViewerEvent.Back + ExternalImageViewerEvent.Previous ) Key.DirectionRight -> externalNavigationEventBus.produceEvent( - ExternalImageViewerEvent.Foward + ExternalImageViewerEvent.Next ) Key.Escape -> externalNavigationEventBus.produceEvent( - ExternalImageViewerEvent.Escape + ExternalImageViewerEvent.ReturnBack ) } }