From 6d1b2482c0ba96bb45042e63c9891690d5bbecf7 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Thu, 25 Aug 2022 13:17:00 -0700 Subject: [PATCH] rename flow activity to save activity --- .../Omnivore/app/src/main/AndroidManifest.xml | 2 +- .../omnivore/ui/save/NewFlowActivity.kt | 12 - .../omnivore/ui/save/SaveSheetActivity.kt | 242 +++++++++--------- 3 files changed, 123 insertions(+), 133 deletions(-) delete mode 100644 android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/NewFlowActivity.kt diff --git a/android/Omnivore/app/src/main/AndroidManifest.xml b/android/Omnivore/app/src/main/AndroidManifest.xml index bf65b39f7..e0b3e5563 100644 --- a/android/Omnivore/app/src/main/AndroidManifest.xml +++ b/android/Omnivore/app/src/main/AndroidManifest.xml @@ -29,7 +29,7 @@ diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/NewFlowActivity.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/NewFlowActivity.kt deleted file mode 100644 index 571a622f8..000000000 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/NewFlowActivity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package app.omnivore.omnivore.ui.save - -import androidx.compose.material.ExperimentalMaterialApi -import app.omnivore.omnivore.ui.save.SaveSheetActivity - -// Not sure why we need this class, but directly opening SaveSheetActivity -// causes the app to crash. - -@OptIn(ExperimentalMaterialApi::class) -class NewFlowActivity : SaveSheetActivity() { - -} diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveSheetActivity.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveSheetActivity.kt index 4ffc0fcae..39cea5518 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveSheetActivity.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveSheetActivity.kt @@ -23,148 +23,150 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch +// Not sure why we need this class, but directly opening SaveSheetActivity +// causes the app to crash. +class SaveSheetActivity : SaveSheetActivityBase() {} @AndroidEntryPoint @OptIn(ExperimentalMaterialApi::class) -abstract class SaveSheetActivity: AppCompatActivity() { +abstract class SaveSheetActivityBase: AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + val viewModel: SaveViewModel by viewModels() + var extractedText: String? = null - val viewModel: SaveViewModel by viewModels() - var extractedText: String? = null - - when (intent?.action) { - Intent.ACTION_SEND -> { - if (intent.type?.startsWith("text/plain") == true) { - intent.getStringExtra(Intent.EXTRA_TEXT)?.let { - Log.d(ContentValues.TAG, "Extracted text: $extractedText") - extractedText = it - viewModel.saveURL(it) - } - } - - if (intent.type?.startsWith("text/html") == true) { - intent.getStringExtra(Intent.EXTRA_HTML_TEXT)?.let { - extractedText = it - } - } - } - else -> { - // Handle other intents, such as being started from the home screen - } + when (intent?.action) { + Intent.ACTION_SEND -> { + if (intent.type?.startsWith("text/plain") == true) { + intent.getStringExtra(Intent.EXTRA_TEXT)?.let { + Log.d(ContentValues.TAG, "Extracted text: $extractedText") + extractedText = it + viewModel.saveURL(it) + } } - setContent { - val coroutineScope = rememberCoroutineScope() - val modalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden) - val isSheetOpened = remember { mutableStateOf(false) } - - ModalBottomSheetLayout( - sheetBackgroundColor = Color.Transparent, - sheetState = modalBottomSheetState, - sheetContent = { - BottomSheetUI { - ScreenContent(viewModel, modalBottomSheetState) - } - } - ) {} - - BackHandler { - onFinish(coroutineScope, modalBottomSheetState) - } - - // Take action based on hidden state - LaunchedEffect(modalBottomSheetState.currentValue) { - when (modalBottomSheetState.currentValue) { - ModalBottomSheetValue.Hidden -> { - handleBottomSheetAtHiddenState( - isSheetOpened, - modalBottomSheetState - ) - } - else -> { - Log.i(TAG, "Bottom sheet ${modalBottomSheetState.currentValue} state") - } - } - } + if (intent.type?.startsWith("text/html") == true) { + intent.getStringExtra(Intent.EXTRA_HTML_TEXT)?.let { + extractedText = it + } } + } + else -> { + // Handle other intents, such as being started from the home screen + } } - @Composable - private fun BottomSheetUI(content: @Composable () -> Unit) { - Box( - modifier = Modifier - .wrapContentHeight() - .fillMaxWidth() - .clip(RoundedCornerShape(topEnd = 20.dp, topStart = 20.dp)) - .background(Color.White) - .statusBarsPadding() - ) { - content() + setContent { + val coroutineScope = rememberCoroutineScope() + val modalBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden) + val isSheetOpened = remember { mutableStateOf(false) } - Divider( - color = Color.Gray, - thickness = 5.dp, - modifier = Modifier - .padding(top = 15.dp) - .align(TopCenter) - .width(80.dp) - .clip(RoundedCornerShape(50.dp)) + ModalBottomSheetLayout( + sheetBackgroundColor = Color.Transparent, + sheetState = modalBottomSheetState, + sheetContent = { + BottomSheetUI { + ScreenContent(viewModel, modalBottomSheetState) + } + } + ) {} + + BackHandler { + onFinish(coroutineScope, modalBottomSheetState) + } + + // Take action based on hidden state + LaunchedEffect(modalBottomSheetState.currentValue) { + when (modalBottomSheetState.currentValue) { + ModalBottomSheetValue.Hidden -> { + handleBottomSheetAtHiddenState( + isSheetOpened, + modalBottomSheetState ) + } + else -> { + Log.i(TAG, "Bottom sheet ${modalBottomSheetState.currentValue} state") + } } + } } + } - // Helper methods - private suspend fun handleBottomSheetAtHiddenState( - isSheetOpened: MutableState, - modalBottomSheetState: ModalBottomSheetState + @Composable + private fun BottomSheetUI(content: @Composable () -> Unit) { + Box( + modifier = Modifier + .wrapContentHeight() + .fillMaxWidth() + .clip(RoundedCornerShape(topEnd = 20.dp, topStart = 20.dp)) + .background(Color.White) + .statusBarsPadding() ) { - when { - !isSheetOpened.value -> initializeModalLayout(isSheetOpened, modalBottomSheetState) - else -> exit() - } - } + content() - private suspend fun initializeModalLayout( - isSheetOpened: MutableState, - modalBottomSheetState: ModalBottomSheetState - ) { - isSheetOpened.value = true - modalBottomSheetState.show() + Divider( + color = Color.Gray, + thickness = 5.dp, + modifier = Modifier + .padding(top = 15.dp) + .align(TopCenter) + .width(80.dp) + .clip(RoundedCornerShape(50.dp)) + ) } + } - open fun exit() = finish() - - private fun onFinish( - coroutineScope: CoroutineScope, - modalBottomSheetState: ModalBottomSheetState, - withResults: Boolean = false, - result: Intent? = null - ) { - coroutineScope.launch { - if (withResults) setResult(RESULT_OK) - result?.let { intent = it} - modalBottomSheetState.hide() // will trigger the LaunchedEffect - } + // Helper methods + private suspend fun handleBottomSheetAtHiddenState( + isSheetOpened: MutableState, + modalBottomSheetState: ModalBottomSheetState + ) { + when { + !isSheetOpened.value -> initializeModalLayout(isSheetOpened, modalBottomSheetState) + else -> exit() } + } - @Composable - fun ScreenContent( - viewModel: SaveViewModel, - modalBottomSheetState: ModalBottomSheetState - ) { - Box(modifier = Modifier.height(300.dp).background(Color.White)) { - SaveContent(viewModel, modalBottomSheetState, modifier = Modifier.fillMaxSize()) - } - } + private suspend fun initializeModalLayout( + isSheetOpened: MutableState, + modalBottomSheetState: ModalBottomSheetState + ) { + isSheetOpened.value = true + modalBottomSheetState.show() + } - override fun onPause() { - super.onPause() - overridePendingTransition(0, 0) - } + open fun exit() = finish() - companion object { - private val TAG = SaveSheetActivity::class.java.simpleName + private fun onFinish( + coroutineScope: CoroutineScope, + modalBottomSheetState: ModalBottomSheetState, + withResults: Boolean = false, + result: Intent? = null + ) { + coroutineScope.launch { + if (withResults) setResult(RESULT_OK) + result?.let { intent = it} + modalBottomSheetState.hide() // will trigger the LaunchedEffect } + } + + @Composable + fun ScreenContent( + viewModel: SaveViewModel, + modalBottomSheetState: ModalBottomSheetState + ) { + Box(modifier = Modifier.height(300.dp).background(Color.White)) { + SaveContent(viewModel, modalBottomSheetState, modifier = Modifier.fillMaxSize()) + } + } + + override fun onPause() { + super.onPause() + overridePendingTransition(0, 0) + } + + companion object { + private val TAG = SaveSheetActivity::class.java.simpleName + } }