Skip to content

Commit

Permalink
Fix deprecation and extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 authored and nielsvanvelzen committed Aug 20, 2023
1 parent 73157ad commit 85c54fe
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions app/src/main/java/org/jellyfin/mobile/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch
import org.jellyfin.mobile.events.ActivityEventHandler
import org.jellyfin.mobile.player.cast.Chromecast
import org.jellyfin.mobile.player.cast.IChromecast
Expand Down Expand Up @@ -84,25 +87,10 @@ class MainActivity : AppCompatActivity() {
with(activityEventHandler) { subscribe() }

// Load UI
lifecycleScope.launchWhenStarted {
mainViewModel.serverState.collect { state ->
with(supportFragmentManager) {
when (state) {
ServerState.Pending -> {
// TODO add loading indicator
}
is ServerState.Unset -> replaceFragment<ConnectFragment>()
is ServerState.Available -> {
val currentFragment = findFragmentById(R.id.fragment_container)
if (currentFragment !is WebViewFragment || currentFragment.server != state.server) {
replaceFragment<WebViewFragment>(
Bundle().apply {
putParcelable(Constants.FRAGMENT_WEB_VIEW_EXTRA_SERVER, state.server)
},
)
}
}
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
mainViewModel.serverState.collect { state ->
handleServerState(state)
}
}
}
Expand All @@ -116,6 +104,31 @@ class MainActivity : AppCompatActivity() {
orientationListener.enable()
}

private fun handleServerState(state: ServerState) {
with(supportFragmentManager) {
val currentFragment = findFragmentById(R.id.fragment_container)
when (state) {
ServerState.Pending -> {
// TODO add loading indicator
}
is ServerState.Unset -> {
if (currentFragment !is ConnectFragment) {
replaceFragment<ConnectFragment>()
}
}
is ServerState.Available -> {
if (currentFragment !is WebViewFragment || currentFragment.server != state.server) {
replaceFragment<WebViewFragment>(
Bundle().apply {
putParcelable(Constants.FRAGMENT_WEB_VIEW_EXTRA_SERVER, state.server)
},
)
}
}
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
Expand Down

0 comments on commit 85c54fe

Please sign in to comment.