-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/org/zotero/android/pdf/reader/plainreader/PdfPlainReaderPspdfKitView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.zotero.android.pdf.reader.plainreader | ||
|
||
import android.view.ViewGroup | ||
import android.widget.FrameLayout | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import com.pspdfkit.ui.PdfReaderView | ||
|
||
@Composable | ||
fun PdfPlainReaderPspdfKitView( | ||
viewModel: PdfPlainReaderViewModel, | ||
) { | ||
val activity = LocalContext.current as? AppCompatActivity ?: return | ||
AndroidView( | ||
modifier = Modifier.fillMaxSize(), | ||
factory = { context -> | ||
val frameLayout = FrameLayout(context) | ||
frameLayout.layoutParams = FrameLayout.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
val pdfReaderView = PdfReaderView(activity) | ||
pdfReaderView.layoutParams = FrameLayout.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
frameLayout.addView(pdfReaderView) | ||
viewModel.init( | ||
pdfReaderView = pdfReaderView | ||
) | ||
frameLayout | ||
}, | ||
update = { _ -> | ||
} | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/zotero/android/pdf/reader/plainreader/PdfPlainReaderTopBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.zotero.android.pdf.reader.plainreader | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.stringResource | ||
import org.zotero.android.uicomponents.Strings | ||
import org.zotero.android.uicomponents.theme.CustomTheme | ||
import org.zotero.android.uicomponents.topbar.NewCustomTopBar | ||
import org.zotero.android.uicomponents.topbar.NewHeadingTextButton | ||
|
||
@Composable | ||
internal fun PdfPlainReaderTopBar( | ||
onBack: () -> Unit, | ||
title: String, | ||
) { | ||
NewCustomTopBar( | ||
title = title, | ||
backgroundColor = CustomTheme.colors.surface, | ||
leftGuidelineStartPercentage = 0.2f, | ||
rightGuidelineStartPercentage = 0.05f, | ||
leftContainerContent = listOf( | ||
{ | ||
NewHeadingTextButton( | ||
onClick = onBack, | ||
text = stringResource(Strings.back_button), | ||
) | ||
}, | ||
) | ||
) | ||
} |
86 changes: 86 additions & 0 deletions
86
app/src/main/java/org/zotero/android/pdf/reader/plainreader/PdfPlainReaderViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.zotero.android.pdf.reader.plainreader | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.pspdfkit.configuration.PdfConfiguration | ||
import com.pspdfkit.configuration.theming.ThemeMode | ||
import com.pspdfkit.ui.PdfReaderView | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import org.zotero.android.architecture.BaseViewModel2 | ||
import org.zotero.android.architecture.Defaults | ||
import org.zotero.android.architecture.ScreenArguments | ||
import org.zotero.android.architecture.ViewEffect | ||
import org.zotero.android.architecture.ViewState | ||
import org.zotero.android.pdf.data.PdfReaderCurrentThemeEventStream | ||
import org.zotero.android.pdf.data.PdfReaderThemeDecider | ||
import org.zotero.android.pdf.reader.plainreader.data.PdfPlainReaderArgs | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class PdfPlainReaderViewModel @Inject constructor( | ||
private val pdfReaderCurrentThemeEventStream: PdfReaderCurrentThemeEventStream, | ||
private val pdfReaderThemeDecider: PdfReaderThemeDecider, | ||
private val defaults: Defaults, | ||
) : BaseViewModel2<PdfPlainReaderViewState, PdfPlainReaderViewEffect>(PdfPlainReaderViewState()) { | ||
|
||
private val screenArgs: PdfPlainReaderArgs by lazy { | ||
ScreenArguments.pdfPlainReaderArgs | ||
} | ||
|
||
fun init( | ||
pdfReaderView: PdfReaderView | ||
) { | ||
startObservingTheme() | ||
|
||
val pdfSettings = defaults.getPDFSettings() | ||
pdfReaderThemeDecider.setPdfPageAppearanceMode(pdfSettings.appearanceMode) | ||
|
||
val isCalculatedThemeDark = pdfReaderCurrentThemeEventStream.currentValue()!!.isDark | ||
val themeMode = when (isCalculatedThemeDark) { | ||
true -> ThemeMode.NIGHT | ||
false -> ThemeMode.DEFAULT | ||
} | ||
|
||
val configuration = PdfConfiguration.Builder() | ||
.invertColors(isCalculatedThemeDark) | ||
.themeMode(themeMode) | ||
.build() | ||
pdfReaderView.setDocument(screenArgs.pdfDocument, configuration) | ||
pdfReaderView.show() | ||
|
||
updateState { | ||
copy(title = screenArgs.pdfDocument.title ?: "") | ||
} | ||
} | ||
|
||
private var pdfReaderThemeCancellable: Job? = null | ||
|
||
private fun startObservingTheme() { | ||
this.pdfReaderThemeCancellable = pdfReaderCurrentThemeEventStream.flow() | ||
.onEach { data -> | ||
val isDark = data!!.isDark | ||
updateState { | ||
copy(isDark = isDark) | ||
} | ||
triggerEffect(PdfPlainReaderViewEffect.ScreenRefresh) | ||
} | ||
.launchIn(viewModelScope) | ||
} | ||
|
||
fun setOsTheme(isDark: Boolean) { | ||
pdfReaderThemeDecider.setCurrentOsTheme(isOsThemeDark = isDark) | ||
} | ||
|
||
} | ||
|
||
data class PdfPlainReaderViewState( | ||
val isDark: Boolean = false, | ||
val title: String = "", | ||
) : ViewState | ||
|
||
sealed class PdfPlainReaderViewEffect : ViewEffect { | ||
object NavigateBack : PdfPlainReaderViewEffect() | ||
object ScreenRefresh : PdfPlainReaderViewEffect() | ||
} |
Oops, something went wrong.