Start to move bottomsheet handling into viewModel so it can control the current bottom state

This is needed so we can implement the "Open Link" bottom sheet
and re-enable the Labels bottom sheet.
This commit is contained in:
Jackson Harper
2023-05-10 13:32:50 +08:00
parent f8fc1306df
commit 431866da8b
4 changed files with 35 additions and 67 deletions

View File

@ -9,9 +9,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
@ -28,50 +26,9 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import app.omnivore.omnivore.persistence.entities.SavedItemLabel
import app.omnivore.omnivore.ui.library.LibraryViewModel
import app.omnivore.omnivore.ui.reader.WebReaderParams
import app.omnivore.omnivore.ui.reader.WebReaderViewModel
@Composable
fun WebReaderLabelsSelectionSheet(viewModel: WebReaderViewModel) {
val isActive: Boolean by viewModel.showLabelsSelectionSheetLiveData.observeAsState(false)
val labels: List<SavedItemLabel> by viewModel.savedItemLabelsLiveData.observeAsState(listOf())
val webReaderParams: WebReaderParams? by viewModel.webReaderParamsLiveData.observeAsState(null)
val modalBottomSheetState = rememberModalBottomSheetState(
ModalBottomSheetValue.HalfExpanded,
)
if (isActive) {
ModalBottomSheetLayout(
sheetBackgroundColor = Color.Transparent,
sheetState = modalBottomSheetState,
sheetContent = {
BottomSheetUI {
LabelsSelectionSheetContent(
labels = labels,
initialSelectedLabels = webReaderParams?.labels ?: listOf(),
onCancel = {
viewModel.showLabelsSelectionSheetLiveData.value = false
},
isLibraryMode = false,
onSave = {
if (it != labels) {
viewModel.updateSavedItemLabels(savedItemID = webReaderParams?.item?.savedItemId ?: "", labels = it)
}
viewModel.showLabelsSelectionSheetLiveData.value = false
},
onCreateLabel = { newLabelName, labelHexValue ->
viewModel.createNewSavedItemLabel(newLabelName, labelHexValue)
}
)
}
}
) {}
}
}
@Composable
fun LabelsSelectionSheet(viewModel: LibraryViewModel) {

View File

@ -11,6 +11,7 @@ import android.view.*
import android.view.View.OnScrollChangeListener
import android.view.ViewTreeObserver.OnScrollChangedListener
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.isSystemInDarkTheme
@ -62,6 +63,17 @@ fun WebReader(
viewModel?.showNavBar()
view?.animate()?.alpha(1.0f)?.duration = 200
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
Log.d("reader","SHOULD OVERRIDE REQUEST: ${request?.url} hasGesture: ${request?.hasGesture()} isForMainFrame: ${request?.isForMainFrame()}")
if ((request?.isForMainFrame == true) && (request?.hasGesture() == true)) {
return false
}
return super.shouldOverrideUrlLoading(view, request)
}
}
val javascriptInterface = AndroidWebKitMessenger { actionID, json ->

View File

@ -34,7 +34,6 @@ import app.omnivore.omnivore.MainActivity
import app.omnivore.omnivore.R
import app.omnivore.omnivore.persistence.entities.SavedItemLabel
import app.omnivore.omnivore.ui.components.LabelsSelectionSheetContent
import app.omnivore.omnivore.ui.components.WebReaderLabelsSelectionSheet
import app.omnivore.omnivore.ui.notebook.NotebookView
import app.omnivore.omnivore.ui.notebook.NotebookViewModel
import app.omnivore.omnivore.ui.savedItemViews.SavedItemContextMenu
@ -124,7 +123,7 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
val onBackPressedDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
var isMenuExpanded by remember { mutableStateOf(false) }
var bottomSheetState by remember { mutableStateOf(BottomSheetState.NONE) }
val bottomSheetState: BottomSheetState? by webReaderViewModel.bottomSheetStateLiveData.observeAsState(BottomSheetState.NONE)
val isDarkMode = isSystemInDarkTheme()
val currentThemeKey = webReaderViewModel.currentThemeKey.observeAsState()
@ -175,22 +174,6 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
}
} ?: Color(0xFF000000)
annotation?.let {
bottomSheetState = BottomSheetState.HIGHLIGHTNOTE
coroutineScope.launch {
modalBottomSheetState.animateTo(ModalBottomSheetValue.Expanded)
}
}
val showLabelsSelector: Boolean by webReaderViewModel.showLabelsSelectionSheetLiveData.observeAsState(false)
if (showLabelsSelector) {
bottomSheetState = BottomSheetState.LABELS
coroutineScope.launch {
modalBottomSheetState.animateTo(ModalBottomSheetValue.HalfExpanded)
}
}
ModalBottomSheetLayout(
modifier = Modifier
.statusBarsPadding(),
@ -219,14 +202,14 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
webReaderViewModel.saveAnnotation(it)
coroutineScope.launch {
modalBottomSheetState.hide()
bottomSheetState = BottomSheetState.NONE
webReaderViewModel.resetBottomSheet()
}
},
onCancel = {
webReaderViewModel.cancelAnnotationEdit()
coroutineScope.launch {
modalBottomSheetState.hide()
bottomSheetState = BottomSheetState.NONE
webReaderViewModel.resetBottomSheet()
}
}
)
@ -241,7 +224,7 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
onCancel = {
coroutineScope.launch {
modalBottomSheetState.hide()
bottomSheetState = BottomSheetState.NONE
webReaderViewModel.resetBottomSheet()
}
},
isLibraryMode = false,
@ -253,7 +236,7 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
}
coroutineScope.launch {
modalBottomSheetState.hide()
bottomSheetState = BottomSheetState.NONE
webReaderViewModel.resetBottomSheet()
}
},
onCreateLabel = { newLabelName, labelHexValue ->
@ -264,6 +247,9 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
}
BottomSheetState.NONE -> {
}
else -> {
}
}
Spacer(modifier = Modifier.weight(1.0F))
@ -304,7 +290,7 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
webReaderParams?.let {
IconButton(onClick = {
coroutineScope.launch {
bottomSheetState = BottomSheetState.NOTEBOOK
webReaderViewModel.setBottomSheet(BottomSheetState.NOTEBOOK)
modalBottomSheetState.animateTo(ModalBottomSheetValue.Expanded)
}
}) {
@ -317,7 +303,7 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null,
}
IconButton(onClick = {
coroutineScope.launch {
bottomSheetState = BottomSheetState.PREFERENCES
webReaderViewModel.setBottomSheet(BottomSheetState.PREFERENCES)
modalBottomSheetState.animateTo(ModalBottomSheetValue.HalfExpanded)
}
}) {

View File

@ -2,6 +2,7 @@ package app.omnivore.omnivore.ui.reader
import android.util.Log
import androidx.compose.foundation.ScrollState
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
@ -64,6 +65,8 @@ class WebReaderViewModel @Inject constructor(
val showLabelsSelectionSheetLiveData = MutableLiveData(false)
val savedItemLabelsLiveData = dataService.db.savedItemLabelDao().getSavedItemLabelsLiveData()
val bottomSheetStateLiveData = MutableLiveData<BottomSheetState>(BottomSheetState.NONE)
var hasTappedExistingHighlight = false
var lastTapCoordinates: TapCoordinates? = null
private var isLoading = false
@ -85,6 +88,14 @@ class WebReaderViewModel @Inject constructor(
onScrollChange(maxToolbarHeightPx)
}
fun setBottomSheet(state: BottomSheetState) {
bottomSheetStateLiveData.postValue(state)
}
fun resetBottomSheet() {
bottomSheetStateLiveData.postValue(BottomSheetState.NONE)
}
fun onScrollChange(delta: Float) {
val newHeight = (currentToolbarHeightLiveData.value ?: 0.0f) + delta
currentToolbarHeightLiveData.value = newHeight.coerceIn(0f, maxToolbarHeightPx)
@ -185,6 +196,7 @@ class WebReaderViewModel @Inject constructor(
}
SavedItemAction.EditLabels -> {
showLabelsSelectionSheetLiveData.value = true
bottomSheetStateLiveData.postValue(BottomSheetState.LABELS)
}
}
}
@ -225,6 +237,7 @@ class WebReaderViewModel @Inject constructor(
.fromJson(jsonString, AnnotationWebViewMessage::class.java)
.annotation ?: ""
annotationLiveData.value = annotation
bottomSheetStateLiveData.postValue(BottomSheetState.HIGHLIGHTNOTE)
}
}
"shareHighlight" -> {