Skip to content

Commit

Permalink
Disable device sleep in reader Closes #171.
Browse files Browse the repository at this point in the history
Upping versionCode to 109
  • Loading branch information
Dima-Android committed Oct 18, 2024
1 parent 6dc9f41 commit ac454c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zotero.android.pdf.reader

import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.isSystemInDarkTheme
Expand All @@ -8,8 +9,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.hilt.navigation.compose.hiltViewModel
Expand Down Expand Up @@ -59,11 +63,18 @@ internal fun PdfReaderScreen(
val thumbnailsLazyListState = rememberLazyListState()
val layoutType = CustomLayoutSize.calculateLayoutType()
val focusManager = LocalFocusManager.current
val currentView = LocalView.current
LaunchedEffect(key1 = viewEffect) {
when (val consumedEffect = viewEffect?.consume()) {
is PdfReaderViewEffect.NavigateBack -> {
onBack()
}
is PdfReaderViewEffect.DisableForceScreenOn -> {
currentView.keepScreenOn = false
}
is PdfReaderViewEffect.EnableForceScreenOn -> {
currentView.keepScreenOn = true
}

is PdfReaderViewEffect.ShowPdfFilters -> {
navigateToPdfFilter()
Expand Down Expand Up @@ -124,9 +135,18 @@ internal fun PdfReaderScreen(
}

CustomScaffold(
modifier = Modifier.pointerInteropFilter {
when (it.action) {
MotionEvent.ACTION_DOWN -> viewModel.restartDisableForceScreenOnTimer()
}
false
},
backgroundColor = CustomTheme.colors.pdfAnnotationsTopbarBackground,
topBar = {
AnimatedContent(targetState = viewState.isTopBarVisible, label = "") { isTopBarVisible ->
AnimatedContent(
targetState = viewState.isTopBarVisible,
label = ""
) { isTopBarVisible ->
if (isTopBarVisible) {
PdfReaderTopBar(
onBack = onBack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ import org.zotero.android.sync.SessionDataEventStream
import org.zotero.android.sync.Tag
import timber.log.Timber
import java.util.EnumSet
import java.util.Timer
import javax.inject.Inject
import kotlin.concurrent.timerTask
import kotlin.random.Random


@HiltViewModel
class PdfReaderViewModel @Inject constructor(
private val defaults: Defaults,
Expand Down Expand Up @@ -227,6 +228,8 @@ class PdfReaderViewModel @Inject constructor(

private lateinit var searchResultHighlighter: SearchResultHighlighter

private var disableForceScreenOnTimer: Timer? = null

val screenArgs: PdfReaderArgs by lazy {
val argsEncoded = stateHandle.get<String>(ARG_PDF_SCREEN).require()
navigationParamsMarshaller.decodeObjectFromBase64(argsEncoded)
Expand Down Expand Up @@ -391,6 +394,7 @@ class PdfReaderViewModel @Inject constructor(
fragmentManager: FragmentManager,
isTablet: Boolean,
) {
restartDisableForceScreenOnTimer()
this.uri = uri
this.isTablet = isTablet
this.fragmentManager = fragmentManager
Expand Down Expand Up @@ -3207,6 +3211,20 @@ class PdfReaderViewModel @Inject constructor(
)
}
}

fun restartDisableForceScreenOnTimer() {
viewModelScope.launch {
triggerEffect(PdfReaderViewEffect.EnableForceScreenOn)
}
disableForceScreenOnTimer?.cancel()
disableForceScreenOnTimer = Timer()
disableForceScreenOnTimer?.schedule(timerTask {
viewModelScope.launch {
triggerEffect(PdfReaderViewEffect.DisableForceScreenOn)
}
}, 25 * 60 * 1000L)
}

}

data class PdfReaderViewState(
Expand Down Expand Up @@ -3266,6 +3284,8 @@ data class PdfReaderViewState(

sealed class PdfReaderViewEffect : ViewEffect {
object NavigateBack : PdfReaderViewEffect()
object DisableForceScreenOn : PdfReaderViewEffect()
object EnableForceScreenOn : PdfReaderViewEffect()
object ShowPdfFilters : PdfReaderViewEffect()
object ShowPdfSettings : PdfReaderViewEffect()
object ShowPdfPlainReader: PdfReaderViewEffect()
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 34

val versionCode = 108 // Must be updated on every build
val versionCode = 109 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit ac454c8

Please sign in to comment.