Browse Source

ImageViewer Android return back key (#2996)

pull/2995/head
dima.avdeev 2 years ago committed by GitHub
parent
commit
35ecc36b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt
  2. 22
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/ImageViewer.android.kt
  3. 8
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt
  4. 4
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt
  5. 7
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/ImageViewer.desktop.kt

16
examples/imageviewer/androidApp/src/androidMain/kotlin/example/imageviewer/MainActivity.kt

@ -1,15 +1,29 @@
package example.imageviewer package example.imageviewer
import android.os.Bundle import android.os.Bundle
import androidx.activity.addCallback
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import example.imageviewer.view.ImageViewerAndroid import example.imageviewer.view.ImageViewerAndroid
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
val externalEvents = MutableSharedFlow<ExternalImageViewerEvent>(
replay = 0,
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContent { setContent {
ImageViewerAndroid() ImageViewerAndroid(externalEvents)
}
onBackPressedDispatcher.addCallback {
externalEvents.tryEmit(ExternalImageViewerEvent.ReturnBack)
} }
} }
} }

22
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.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext 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.filter.PlatformContext
import example.imageviewer.ioDispatcher
import example.imageviewer.model.PictureData import example.imageviewer.model.PictureData
import example.imageviewer.storage.AndroidImageStorage import example.imageviewer.storage.AndroidImageStorage
import example.imageviewer.style.ImageViewerTheme import example.imageviewer.style.ImageViewerTheme
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@Composable @Composable
fun ImageViewerAndroid() { fun ImageViewerAndroid(externalEvents: Flow<ExternalImageViewerEvent>) {
val context: Context = LocalContext.current val context: Context = LocalContext.current
val ioScope = rememberCoroutineScope { ioDispatcher } val ioScope = rememberCoroutineScope { ioDispatcher }
val dependencies = remember(context, ioScope) { getDependencies(context, ioScope) } val dependencies = remember(context, ioScope) {
getDependencies(context, ioScope, externalEvents)
}
ImageViewerTheme { ImageViewerTheme {
ImageViewerCommon(dependencies) ImageViewerCommon(dependencies)
} }
} }
private fun getDependencies(context: Context, ioScope: CoroutineScope) = object : Dependencies() { private fun getDependencies(
context: Context,
ioScope: CoroutineScope,
externalEvents: Flow<ExternalImageViewerEvent>
) = object : Dependencies() {
override val notification: Notification = object : PopupNotification(localization) { override val notification: Notification = object : PopupNotification(localization) {
override fun showPopUpMessage(text: String) { override fun showPopUpMessage(text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show() Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
@ -53,4 +66,5 @@ private fun getDependencies(context: Context, ioScope: CoroutineScope) = object
} }
} }
} }
override val externalEvents: Flow<ExternalImageViewerEvent> = externalEvents
} }

8
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt

@ -8,9 +8,9 @@ import example.imageviewer.model.*
import example.imageviewer.view.* import example.imageviewer.view.*
enum class ExternalImageViewerEvent { enum class ExternalImageViewerEvent {
Foward, Next,
Back, Previous,
Escape, ReturnBack,
} }
@Composable @Composable
@ -38,7 +38,7 @@ internal fun ImageViewerWithProvidedDependencies(
val externalEvents = LocalInternalEvents.current val externalEvents = LocalInternalEvents.current
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
externalEvents.collect { externalEvents.collect {
if (it == ExternalImageViewerEvent.Escape) { if (it == ExternalImageViewerEvent.ReturnBack) {
navigationStack.back() navigationStack.back()
} }
} }

4
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/GalleryScreen.kt

@ -57,8 +57,8 @@ internal fun GalleryScreen(
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
externalEvents.collect { externalEvents.collect {
when (it) { when (it) {
ExternalImageViewerEvent.Foward -> nextImage() ExternalImageViewerEvent.Next -> nextImage()
ExternalImageViewerEvent.Back -> previousImage() ExternalImageViewerEvent.Previous -> previousImage()
else -> {} else -> {}
} }
} }

7
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.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import java.awt.Desktop
import java.awt.Dimension import java.awt.Dimension
import java.awt.Toolkit import java.awt.Toolkit
@ -63,15 +62,15 @@ fun ApplicationScope.ImageViewerDesktop() {
if (it.type == KeyEventType.KeyUp) { if (it.type == KeyEventType.KeyUp) {
when (it.key) { when (it.key) {
Key.DirectionLeft -> externalNavigationEventBus.produceEvent( Key.DirectionLeft -> externalNavigationEventBus.produceEvent(
ExternalImageViewerEvent.Back ExternalImageViewerEvent.Previous
) )
Key.DirectionRight -> externalNavigationEventBus.produceEvent( Key.DirectionRight -> externalNavigationEventBus.produceEvent(
ExternalImageViewerEvent.Foward ExternalImageViewerEvent.Next
) )
Key.Escape -> externalNavigationEventBus.produceEvent( Key.Escape -> externalNavigationEventBus.produceEvent(
ExternalImageViewerEvent.Escape ExternalImageViewerEvent.ReturnBack
) )
} }
} }

Loading…
Cancel
Save