Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation and extract method #1161

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ->
nielsvanvelzen marked this conversation as resolved.
Show resolved Hide resolved
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