From 1fa95b6e1728eec292f5dd5d1eee2f7864fe83f8 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Wed, 4 Dec 2024 21:21:19 +0700 Subject: [PATCH] Handle navigate back --- .../canvas/presentation/ArkCanvasFragment.kt | 4 +- .../canvas/presentation/edit/EditScreen.kt | 71 +++++-------------- .../sample/canvas/FilePickerFragment.kt | 7 +- 3 files changed, 22 insertions(+), 60 deletions(-) diff --git a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/ArkCanvasFragment.kt b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/ArkCanvasFragment.kt index 0bbb9db..5ff31c1 100644 --- a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/ArkCanvasFragment.kt +++ b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/ArkCanvasFragment.kt @@ -86,8 +86,8 @@ class ArkCanvasFragment : Fragment() { EditScreen( imagePath = null, imageUri = null, - fragmentManager = childFragmentManager, - navigateBack = { /*TODO*/ }, + fragmentManager = requireActivity().supportFragmentManager, + navigateBack = { requireActivity().supportFragmentManager.popBackStackImmediate() }, launchedFromIntent = false, maxResolution = Resolution(350, 720), onSaveSvg = { /*TODO*/ }, diff --git a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/edit/EditScreen.kt b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/edit/EditScreen.kt index 6f6337c..58e109b 100644 --- a/canvas/src/main/java/dev/arkbuilders/canvas/presentation/edit/EditScreen.kt +++ b/canvas/src/main/java/dev/arkbuilders/canvas/presentation/edit/EditScreen.kt @@ -4,8 +4,6 @@ package dev.arkbuilders.canvas.presentation.edit import android.os.Build import android.view.MotionEvent -import android.widget.Toast -import androidx.activity.compose.BackHandler import androidx.annotation.RequiresApi import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.tween @@ -13,40 +11,40 @@ import androidx.compose.animation.expandVertically import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut import androidx.compose.animation.shrinkVertically -import androidx.compose.foundation.clickable import androidx.compose.foundation.background -import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.clickable import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.IntrinsicSize import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.IntrinsicSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.wrapContentHeight -import androidx.compose.foundation.layout.BoxScope +import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Icon -import androidx.compose.material.Text -import androidx.compose.material.TextButton import androidx.compose.material.AlertDialog import androidx.compose.material.Button +import androidx.compose.material.Icon +import androidx.compose.material.MaterialTheme import androidx.compose.material.Slider +import androidx.compose.material.Text +import androidx.compose.material.TextButton import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.KeyboardArrowDown import androidx.compose.material.icons.filled.KeyboardArrowLeft import androidx.compose.material.icons.filled.KeyboardArrowRight import androidx.compose.material.icons.filled.KeyboardArrowUp import androidx.compose.runtime.Composable -import androidx.compose.runtime.remember -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier @@ -120,48 +118,11 @@ fun EditScreen( viewModel = viewModel, navigateBack = { navigateBack() - viewModel.isLoaded = false +// viewModel.isLoaded = false }, launchedFromIntent = launchedFromIntent, ) - BackHandler { - val editManager = viewModel.editManager - if ( - editManager.isCropMode.value || editManager.isRotateMode.value || - editManager.isResizeMode.value || editManager.isEyeDropperMode.value || - editManager.isBlurMode.value - ) { - viewModel.cancelOperation() - return@BackHandler - } - if (editManager.isZoomMode.value) { - editManager.toggleZoomMode() - return@BackHandler - } - if (editManager.isPanMode.value) { - editManager.togglePanMode() - return@BackHandler - } - if (editManager.canUndo.value) { - editManager.undo() - return@BackHandler - } - if (viewModel.exitConfirmed) { - if (launchedFromIntent) - context.getActivity()?.finish() - else - navigateBack() - return@BackHandler - } - if (!viewModel.exitConfirmed) { - Toast.makeText(context, "Tap back again to exit", Toast.LENGTH_SHORT) - .show() - viewModel.confirmExit() - return@BackHandler - } - } - HandleImageSavedEffect(viewModel, launchedFromIntent, navigateBack) if (!showDefaultsDialog.value) diff --git a/sample/src/main/java/dev/arkbuilders/sample/canvas/FilePickerFragment.kt b/sample/src/main/java/dev/arkbuilders/sample/canvas/FilePickerFragment.kt index aecf458..ba3ed12 100644 --- a/sample/src/main/java/dev/arkbuilders/sample/canvas/FilePickerFragment.kt +++ b/sample/src/main/java/dev/arkbuilders/sample/canvas/FilePickerFragment.kt @@ -32,9 +32,9 @@ class FilePickerFragment : Fragment() { ) setContent { // Set Content here - PickerScreen(fragmentManager = childFragmentManager, + PickerScreen(fragmentManager = requireActivity().supportFragmentManager, onNavigateToEdit = { path, resolution -> - onNavigateToEdit(path, parentFragmentManager) + onNavigateToEdit(path, requireActivity().supportFragmentManager) }) } } @@ -47,7 +47,8 @@ class FilePickerFragment : Fragment() { fragmentManager .beginTransaction() - .replace(R.id.canvas_content, canvasFragment) + .add(R.id.canvas_content, canvasFragment) + .addToBackStack(null) .commit() }