Skip to content

Commit

Permalink
Merge pull request #345 from leonlatsch/release/1.7.3
Browse files Browse the repository at this point in the history
Release: 1.7.3
  • Loading branch information
leonlatsch authored Jul 9, 2024
2 parents 50e447f + 28caef3 commit 34ce8ee
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Also available at **[IzzyOnDroid (F-Droid)](https://apt.izzysoft.de/fdroid/index
![Portuguese (Brazil)](https://img.shields.io/badge/Portuguese%20(Brazil)-81%25-yellow)
![Russian](https://img.shields.io/badge/Russian-84%25-yellow)
![Spanish](https://img.shields.io/badge/Spanish-100%25-brightgreen)
![Turkish](https://img.shields.io/badge/Turkish-100%25-brightgreen)
<!-- END-TRANSLATIONS -->

LICENSE
Expand Down
44 changes: 18 additions & 26 deletions app/src/main/java/dev/leonlatsch/photok/BaseApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import android.content.Intent
import androidx.lifecycle.*
import dagger.hilt.android.HiltAndroidApp
import dev.leonlatsch.photok.main.ui.MainActivity
import dev.leonlatsch.photok.model.io.EncryptedStorageManager
import dev.leonlatsch.photok.model.repositories.PhotoRepository
import dev.leonlatsch.photok.other.setAppDesign
import dev.leonlatsch.photok.security.EncryptionManager
import dev.leonlatsch.photok.settings.data.Config
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -36,44 +36,38 @@ import javax.inject.Inject
* @author Leon Latsch
*/
@HiltAndroidApp
class BaseApplication : Application(), LifecycleObserver {
class BaseApplication : Application(), DefaultLifecycleObserver {

@Inject
lateinit var config: Config

@Inject
lateinit var encryptionManager: EncryptionManager

@Inject
lateinit var encryptedStorageManager: EncryptedStorageManager
val state = MutableStateFlow(ApplicationState.LOCKED)

private var wentToBackgroundAt = 0L

val rawApplicationState = MutableLiveData(ApplicationState.LOCKED)

private var ignoreNextTimeout = false

var applicationState: ApplicationState
get() = rawApplicationState.value!!
set(value) = rawApplicationState.postValue(value)

override fun onCreate() {
super.onCreate()
super<Application>.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}

ProcessLifecycleOwner.get().lifecycle.addObserver(this)

setAppDesign(config.systemDesign)
}

/**
* Call [lockApp] when app was ON_STOP for at least the configured time.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onAppForeground() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)

if (ignoreNextTimeout) {
ignoreNextTimeout = false
return
Expand All @@ -87,11 +81,9 @@ class BaseApplication : Application(), LifecycleObserver {
}
}

/**
* Saves the ON_STOP timestamp
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackground() {
override fun onStop(owner: LifecycleOwner) {
super.onStop(owner)

wentToBackgroundAt = System.currentTimeMillis()
}

Expand All @@ -107,7 +99,7 @@ class BaseApplication : Application(), LifecycleObserver {
*/
fun lockApp() {
encryptionManager.reset()
applicationState = ApplicationState.LOCKED
state.update { ApplicationState.LOCKED }
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import dev.leonlatsch.photok.gallery.ui.importing.ImportBottomSheetDialogFragmen
import dev.leonlatsch.photok.main.ui.navigation.MainMenu
import dev.leonlatsch.photok.other.REQ_PERM_SHARED_IMPORT
import dev.leonlatsch.photok.other.extensions.getBaseApplication
import dev.leonlatsch.photok.other.extensions.launchLifecycleAwareJob
import dev.leonlatsch.photok.permissions.getReadImagesPermission
import dev.leonlatsch.photok.permissions.getReadVideosPermission
import dev.leonlatsch.photok.settings.data.Config
Expand Down Expand Up @@ -86,9 +87,11 @@ class MainActivity : BindableActivity<ActivityMainBinding>(R.layout.activity_mai
}
}

getBaseApplication().rawApplicationState.observe(this) {
if (it == ApplicationState.UNLOCKED) {
viewModel.consumeSharedUris()
launchLifecycleAwareJob {
getBaseApplication().state.collect {
if (it == ApplicationState.UNLOCKED) {
viewModel.consumeSharedUris()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import android.os.Build
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import dev.leonlatsch.photok.BaseApplication
import kotlinx.coroutines.launch

/**
* Get the "application" as [BaseApplication] from any activity.
Expand Down Expand Up @@ -62,4 +67,8 @@ fun Activity.showSystemUI() {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
}
}
inline fun AppCompatActivity.launchLifecycleAwareJob(
state: Lifecycle.State = Lifecycle.State.CREATED,
crossinline block: suspend () -> Unit
) = lifecycleScope.launch { repeatOnLifecycle(state) { block() } }
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ fun Fragment.startActivityForResultAndIgnoreTimer(intent: Intent, reqCode: Int)
inline fun Fragment.launchLifecycleAwareJob(
state: Lifecycle.State = Lifecycle.State.CREATED,
crossinline block: suspend () -> Unit
) =
viewLifecycleOwner.lifecycleScope.launch { repeatOnLifecycle(state) { block() } }
) = viewLifecycleOwner.lifecycleScope.launch { repeatOnLifecycle(state) { block() } }


/**
* Create a view model with assisted injection. This is a workaround for the missing support of assisted injection in Hilt.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import dev.leonlatsch.photok.other.extensions.getBaseApplication
import dev.leonlatsch.photok.other.extensions.hide
import dev.leonlatsch.photok.other.extensions.requireActivityAs
import dev.leonlatsch.photok.other.extensions.show
import dev.leonlatsch.photok.other.statusBarPadding
import dev.leonlatsch.photok.other.systemBarsPadding
import dev.leonlatsch.photok.uicomponnets.Dialogs
import dev.leonlatsch.photok.uicomponnets.base.BaseActivity
import dev.leonlatsch.photok.uicomponnets.bindings.BindableFragment
import kotlinx.coroutines.flow.update

/**
* Fragment for the setup.
Expand Down Expand Up @@ -101,7 +101,7 @@ class SetupFragment : BindableFragment<FragmentSetupBinding>(R.layout.fragment_s
binding.loadingOverlay.hide()

if (viewModel.encryptionManager.isReady) {
requireActivity().getBaseApplication().applicationState = ApplicationState.UNLOCKED
requireActivity().getBaseApplication().state.update { ApplicationState.UNLOCKED }
findNavController().navigate(R.id.action_setupFragment_to_galleryFragment)
} else {
Dialogs.showLongToast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import dev.leonlatsch.photok.other.extensions.hide
import dev.leonlatsch.photok.other.extensions.requireActivityAs
import dev.leonlatsch.photok.other.extensions.show
import dev.leonlatsch.photok.other.extensions.vanish
import dev.leonlatsch.photok.other.statusBarPadding
import dev.leonlatsch.photok.other.systemBarsPadding
import dev.leonlatsch.photok.uicomponnets.Dialogs
import dev.leonlatsch.photok.uicomponnets.base.BaseActivity
import dev.leonlatsch.photok.uicomponnets.bindings.BindableFragment
import kotlinx.coroutines.flow.update

/**
* Unlock fragment.
Expand Down Expand Up @@ -77,7 +77,7 @@ class UnlockFragment : BindableFragment<FragmentUnlockBinding>(R.layout.fragment
binding.loadingOverlay.hide()

if (viewModel.encryptionManager.isReady) {
requireActivity().getBaseApplication().applicationState = ApplicationState.UNLOCKED
requireActivity().getBaseApplication().state.update { ApplicationState.UNLOCKED }
findNavController().navigate(R.id.action_unlockFragment_to_galleryFragment)
} else {
Dialogs.showLongToast(requireContext(), getString(R.string.common_error))
Expand Down
Loading

0 comments on commit 34ce8ee

Please sign in to comment.