Inline validateAuthentication extension into MainActivity

This commit is contained in:
Niels van Velzen 2023-05-12 22:27:52 +02:00 committed by Niels van Velzen
parent 2e4aa21db2
commit e1066911cb
3 changed files with 17 additions and 37 deletions

View File

@ -1,31 +0,0 @@
package org.jellyfin.androidtv.auth.ui
import android.content.Intent
import androidx.fragment.app.FragmentActivity
import org.jellyfin.androidtv.auth.repository.SessionRepository
import org.jellyfin.androidtv.auth.repository.UserRepository
import org.jellyfin.androidtv.ui.startup.StartupActivity
import org.koin.android.ext.android.inject
import timber.log.Timber
/**
* Extension function to check authentication. Should be called in [FragmentActivity.onCreate] and
* [FragmentActivity.onResume]. It validates the current session and opens the authentication screen
* when no session is found.
*
* @return whether to proceed creating the activity or not. When `false` is returned the
* [FragmentActivity.finish] function is automatically called.
*/
fun FragmentActivity.validateAuthentication(): Boolean {
val sessionRepository by inject<SessionRepository>()
val userRepository by inject<UserRepository>()
if (sessionRepository.currentSession.value == null || userRepository.currentUser.value == null) {
Timber.w("Activity ${this::class.qualifiedName} started without a session, bouncing to StartupActivity")
startActivity(Intent(this, StartupActivity::class.java))
finish()
return false
}
return true
}

View File

@ -15,15 +15,18 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.auth.ui.validateAuthentication
import org.jellyfin.androidtv.auth.repository.SessionRepository
import org.jellyfin.androidtv.auth.repository.UserRepository
import org.jellyfin.androidtv.ui.ScreensaverViewModel
import org.jellyfin.androidtv.ui.background.AppBackground
import org.jellyfin.androidtv.ui.navigation.NavigationAction
import org.jellyfin.androidtv.ui.navigation.NavigationRepository
import org.jellyfin.androidtv.ui.screensaver.InAppScreensaver
import org.jellyfin.androidtv.ui.startup.StartupActivity
import org.jellyfin.androidtv.util.applyTheme
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import timber.log.Timber
class MainActivity : FragmentActivity(R.layout.activity_main) {
companion object {
@ -31,6 +34,8 @@ class MainActivity : FragmentActivity(R.layout.activity_main) {
}
private val navigationRepository by inject<NavigationRepository>()
private val sessionRepository by inject<SessionRepository>()
private val userRepository by inject<UserRepository>()
private val screensaverViewModel by viewModel<ScreensaverViewModel>()
private val backPressedCallback = object : OnBackPressedCallback(false) {
@ -87,6 +92,17 @@ class MainActivity : FragmentActivity(R.layout.activity_main) {
else window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
private fun validateAuthentication(): Boolean {
if (sessionRepository.currentSession.value == null || userRepository.currentUser.value == null) {
Timber.w("Activity ${this::class.qualifiedName} started without a session, bouncing to StartupActivity")
startActivity(Intent(this, StartupActivity::class.java))
finish()
return false
}
return true
}
override fun onPause() {
super.onPause()

View File

@ -17,7 +17,6 @@ import androidx.lifecycle.Lifecycle;
import org.jellyfin.androidtv.R;
import org.jellyfin.androidtv.auth.repository.UserRepository;
import org.jellyfin.androidtv.auth.ui.ActivityAuthenticationExtensionsKt;
import org.jellyfin.androidtv.data.compat.PlaybackException;
import org.jellyfin.androidtv.data.compat.StreamInfo;
import org.jellyfin.androidtv.data.compat.SubtitleStreamInfo;
@ -98,8 +97,6 @@ public class ExternalPlayerActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!ActivityAuthenticationExtensionsKt.validateAuthentication(this)) return;
mItemsToPlay = videoQueueManager.getValue().getCurrentVideoQueue();
if (mItemsToPlay == null || mItemsToPlay.size() == 0) {
@ -118,8 +115,6 @@ public class ExternalPlayerActivity extends FragmentActivity {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (!ActivityAuthenticationExtensionsKt.validateAuthentication(this)) return;
long playerFinishedTime = System.currentTimeMillis();
Timber.d("Returned from player, result <%d>, extra data <%s>", resultCode, data);
org.jellyfin.sdk.model.api.BaseItemDto item = mItemsToPlay.get(mCurrentNdx);