load web reader via activity rather than composable

This commit is contained in:
Satindar Dhillon
2023-02-08 09:46:15 -08:00
parent e51de533e7
commit 94c107ebc1
5 changed files with 27 additions and 57 deletions

View File

@ -15,7 +15,6 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import app.omnivore.omnivore.ui.auth.LoginViewModel
import app.omnivore.omnivore.ui.library.LibraryViewModel
import app.omnivore.omnivore.ui.reader.WebReaderViewModel
import app.omnivore.omnivore.ui.root.RootView
import app.omnivore.omnivore.ui.theme.OmnivoreTheme
import com.pspdfkit.PSPDFKit
@ -32,7 +31,6 @@ class MainActivity : ComponentActivity() {
val loginViewModel: LoginViewModel by viewModels()
val libraryViewModel: LibraryViewModel by viewModels()
val webReaderViewModel: WebReaderViewModel by viewModels()
val context = this
@ -53,7 +51,7 @@ class MainActivity : ComponentActivity() {
.fillMaxSize()
.background(color = Color.Black)
) {
RootView(loginViewModel, libraryViewModel, webReaderViewModel)
RootView(loginViewModel, libraryViewModel)
}
}
}

View File

@ -18,14 +18,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import app.omnivore.omnivore.Routes
import app.omnivore.omnivore.persistence.entities.SavedItemAndSavedItemLabelCrossRef
import app.omnivore.omnivore.persistence.entities.SavedItemCardData
import app.omnivore.omnivore.persistence.entities.SavedItemCardDataWithLabels
import app.omnivore.omnivore.ui.savedItemViews.SavedItemCard
import app.omnivore.omnivore.ui.reader.PDFReaderActivity
import app.omnivore.omnivore.ui.reader.WebReaderLoadingContainerActivity
import kotlinx.coroutines.flow.distinctUntilChanged
@ -48,7 +46,6 @@ fun LibraryView(
) { paddingValues ->
LibraryViewContent(
libraryViewModel,
navController,
modifier = Modifier
.padding(
top = paddingValues.calculateTopPadding(),
@ -60,11 +57,7 @@ fun LibraryView(
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun LibraryViewContent(
libraryViewModel: LibraryViewModel,
navController: NavHostController,
modifier: Modifier
) {
fun LibraryViewContent(libraryViewModel: LibraryViewModel, modifier: Modifier) {
val context = LocalContext.current
val listState = rememberLazyListState()
@ -95,13 +88,10 @@ fun LibraryViewContent(
SavedItemCard(
cardData = cardDataWithLabels.cardData,
onClickHandler = {
if (cardDataWithLabels.cardData.isPDF()) {
val intent = Intent(context, PDFReaderActivity::class.java)
intent.putExtra("SAVED_ITEM_SLUG", cardDataWithLabels.cardData.slug)
context.startActivity(intent)
} else {
navController.navigate("WebReader/${cardDataWithLabels.cardData.slug}")
}
val activityClass = if (cardDataWithLabels.cardData.isPDF()) PDFReaderActivity::class.java else WebReaderLoadingContainerActivity::class.java
val intent = Intent(context, activityClass)
intent.putExtra("SAVED_ITEM_SLUG", cardDataWithLabels.cardData.slug)
context.startActivity(intent)
},
actionHandler = { libraryViewModel.handleSavedItemAction(cardDataWithLabels.cardData.savedItemId, it) }
)

View File

@ -2,6 +2,7 @@ package app.omnivore.omnivore.ui.reader
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
@ -51,7 +52,8 @@ class WebReaderLoadingContainerActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val requestID = intent.getStringExtra("SAVED_ITEM_REQUEST_ID") ?: ""
val requestID = intent.getStringExtra("SAVED_ITEM_REQUEST_ID")
val slug = intent.getStringExtra("SAVED_ITEM_SLUG")
setContent {
val systemUiController = rememberSystemUiController()
@ -69,7 +71,8 @@ class WebReaderLoadingContainerActivity: ComponentActivity() {
} else {
WebReaderLoadingContainer(
requestID = requestID,
onLibraryIconTap = { startMainActivity() },
slug = slug,
onLibraryIconTap = if (requestID != null) { { startMainActivity() } } else null,
webReaderViewModel = viewModel
)
}
@ -104,6 +107,7 @@ class WebReaderLoadingContainerActivity: ComponentActivity() {
@Composable
fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null, onLibraryIconTap: (() -> Unit)? = null, webReaderViewModel: WebReaderViewModel) {
Log.d("reader", "loading web reader")
val onBackPressedDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
var isMenuExpanded by remember { mutableStateOf(false) }
@ -119,6 +123,16 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null, o
if (webReaderParams == null) {
webReaderViewModel.maxToolbarHeightPx = with(LocalDensity.current) { maxToolbarHeight.roundToPx().toFloat() }
webReaderViewModel.loadItem(slug = slug, requestID = requestID)
Column(
verticalArrangement = Arrangement.SpaceAround,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
) {
Text("Loading...", color = Color.White)
}
}
if (webReaderParams != null) {
@ -192,15 +206,5 @@ fun WebReaderLoadingContainer(slug: String? = null, requestID: String? = null, o
onBackPressedDispatcher?.onBackPressed()
}
}
} else {
Column(
verticalArrangement = Arrangement.SpaceAround,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
) {
Text("Loading...", color = Color.White)
}
}
}

View File

@ -70,7 +70,7 @@ class WebReaderViewModel @Inject constructor(
val webReaderParams = loadItemFromServer(slug)
if (webReaderParams != null) {
Log.d("sync", "data loaded from server")
Log.d("reader", "data loaded from server")
webReaderParamsLiveData.postValue(webReaderParams)
} else {
loadItemFromDB(slug)
@ -204,15 +204,6 @@ class WebReaderViewModel @Inject constructor(
}
}
fun reset() {
shouldPopViewLiveData.postValue(false)
webReaderParamsLiveData.value = null
annotationLiveData.value = null
javascriptDispatchQueue = mutableListOf()
hasTappedExistingHighlight = false
lastTapCoordinates = null
}
fun resetJavascriptDispatchQueue() {
lastJavascriptActionLoopUUID = javascriptActionLoopUUIDLiveData.value ?: UUID.randomUUID()
javascriptDispatchQueue = mutableListOf()

View File

@ -18,14 +18,12 @@ import app.omnivore.omnivore.ui.auth.LoginViewModel
import app.omnivore.omnivore.ui.auth.WelcomeScreen
import app.omnivore.omnivore.ui.library.LibraryView
import app.omnivore.omnivore.ui.library.LibraryViewModel
import app.omnivore.omnivore.ui.reader.*
import com.google.accompanist.systemuicontroller.rememberSystemUiController
@Composable
fun RootView(
loginViewModel: LoginViewModel,
libraryViewModel: LibraryViewModel,
webReaderViewModel: WebReaderViewModel
libraryViewModel: LibraryViewModel
) {
val hasAuthToken: Boolean by loginViewModel.hasAuthTokenLiveData.observeAsState(false)
val systemUiController = rememberSystemUiController()
@ -47,8 +45,7 @@ fun RootView(
if (hasAuthToken) {
PrimaryNavigator(
loginViewModel = loginViewModel,
libraryViewModel = libraryViewModel,
webReaderViewModel = webReaderViewModel
libraryViewModel = libraryViewModel
)
} else {
WelcomeScreen(viewModel = loginViewModel)
@ -66,8 +63,7 @@ fun RootView(
@Composable
fun PrimaryNavigator(
loginViewModel: LoginViewModel,
libraryViewModel: LibraryViewModel,
webReaderViewModel: WebReaderViewModel
libraryViewModel: LibraryViewModel
) {
val navController = rememberNavController()
@ -79,15 +75,6 @@ fun PrimaryNavigator(
)
}
composable("WebReader/{slug}") {
webReaderViewModel.reset() // clear previously loaded item
WebReaderLoadingContainer(
it.arguments?.getString("slug") ?: "",
webReaderViewModel = webReaderViewModel
)
}
composable(Routes.Settings.route) {
SettingsView(loginViewModel = loginViewModel, navController = navController)
}