* Organize code, fix "locking" problem Organize `MainActivity.kt`. Open `serverSelectFragment` instead of `loginFragment` which could've lead to app locking if no user has logged in after adding the server. * Fix linting * Optimize imports * fix: update jellyfinApi before navigating to login fragment Also move logic to viewmodel --------- Co-authored-by: Jarne Demeulemeester <jarnedemeulemeester@gmail.com>
This commit is contained in:
parent
dd826fc38d
commit
28231affc8
4 changed files with 65 additions and 29 deletions
|
@ -43,34 +43,17 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private lateinit var navController: NavController
|
||||
|
||||
@OptIn(NavigationUiSaveStateControl::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
scheduleUserDataSync()
|
||||
cleanUpOldDownloads()
|
||||
applyTheme()
|
||||
setupActivity()
|
||||
}
|
||||
|
||||
val syncWorkRequest = OneTimeWorkRequestBuilder<SyncWorker>()
|
||||
.setConstraints(
|
||||
Constraints.Builder()
|
||||
.setRequiredNetworkType(
|
||||
NetworkType.CONNECTED,
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
.build()
|
||||
|
||||
val workManager = WorkManager.getInstance(applicationContext)
|
||||
|
||||
workManager.beginUniqueWork("syncUserData", ExistingWorkPolicy.KEEP, syncWorkRequest).enqueue()
|
||||
|
||||
if (!appPreferences.downloadsMigrated) {
|
||||
cleanUpOldDownloads()
|
||||
}
|
||||
|
||||
if (appPreferences.amoledTheme) {
|
||||
setTheme(CoreR.style.Theme_FindroidAMOLED)
|
||||
}
|
||||
|
||||
@OptIn(NavigationUiSaveStateControl::class)
|
||||
private fun setupActivity() {
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
|
||||
setContentView(binding.root)
|
||||
|
||||
val navHostFragment =
|
||||
|
@ -129,8 +112,8 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private fun checkServersEmpty(graph: NavGraph, onServersEmpty: () -> Unit = {}) {
|
||||
if (!viewModel.startDestinationChanged) {
|
||||
val nServers = database.getServersCount()
|
||||
if (nServers < 1) {
|
||||
val numOfServers = database.getServersCount()
|
||||
if (numOfServers < 1) {
|
||||
graph.setStartDestination(R.id.addServerFragment)
|
||||
viewModel.startDestinationChanged = true
|
||||
onServersEmpty()
|
||||
|
@ -143,7 +126,7 @@ class MainActivity : AppCompatActivity() {
|
|||
appPreferences.currentServer?.let {
|
||||
val currentUser = database.getServerCurrentUser(it)
|
||||
if (currentUser == null) {
|
||||
graph.setStartDestination(R.id.loginFragment)
|
||||
graph.setStartDestination(R.id.serverSelectFragment)
|
||||
viewModel.startDestinationChanged = true
|
||||
onNoUser()
|
||||
}
|
||||
|
@ -155,6 +138,10 @@ class MainActivity : AppCompatActivity() {
|
|||
* Temp to remove old downloads, will be removed in a future version
|
||||
*/
|
||||
private fun cleanUpOldDownloads() {
|
||||
if (appPreferences.downloadsMigrated) {
|
||||
return
|
||||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
val oldDir = applicationContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES)
|
||||
if (oldDir == null) {
|
||||
|
@ -171,4 +158,27 @@ class MainActivity : AppCompatActivity() {
|
|||
appPreferences.downloadsMigrated = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun scheduleUserDataSync() {
|
||||
val syncWorkRequest = OneTimeWorkRequestBuilder<SyncWorker>()
|
||||
.setConstraints(
|
||||
Constraints.Builder()
|
||||
.setRequiredNetworkType(
|
||||
NetworkType.CONNECTED,
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
.build()
|
||||
|
||||
val workManager = WorkManager.getInstance(applicationContext)
|
||||
|
||||
workManager.beginUniqueWork("syncUserData", ExistingWorkPolicy.KEEP, syncWorkRequest)
|
||||
.enqueue()
|
||||
}
|
||||
|
||||
private fun applyTheme() {
|
||||
if (appPreferences.amoledTheme) {
|
||||
setTheme(CoreR.style.Theme_FindroidAMOLED)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ import timber.log.Timber
|
|||
|
||||
@AndroidEntryPoint
|
||||
class ServerSelectFragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentServerSelectBinding
|
||||
private val viewModel: ServerSelectViewModel by viewModels()
|
||||
|
||||
|
@ -66,6 +65,11 @@ class ServerSelectFragment : Fragment() {
|
|||
if (it) navigateToMainActivity()
|
||||
}
|
||||
}
|
||||
launch {
|
||||
viewModel.navigateToLogin.collect {
|
||||
if (it) navigateToLoginFragment()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,4 +91,8 @@ class ServerSelectFragment : Fragment() {
|
|||
private fun navigateToMainActivity() {
|
||||
findNavController().navigate(ServerSelectFragmentDirections.actionServerSelectFragmentToHomeFragment())
|
||||
}
|
||||
|
||||
private fun navigateToLoginFragment() {
|
||||
findNavController().navigate(ServerSelectFragmentDirections.actionServerSelectFragmentToLoginFragment())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -299,6 +299,9 @@
|
|||
app:destination="@id/homeFragment"
|
||||
app:popUpTo="@id/homeFragment"
|
||||
app:popUpToInclusive="true" />
|
||||
<action
|
||||
android:id="@+id/action_serverSelectFragment_to_loginFragment"
|
||||
app:destination="@id/loginFragment" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/loginFragment"
|
||||
|
|
|
@ -29,6 +29,9 @@ constructor(
|
|||
private val _navigateToMain = MutableSharedFlow<Boolean>()
|
||||
val navigateToMain = _navigateToMain.asSharedFlow()
|
||||
|
||||
private val _navigateToLogin = MutableSharedFlow<Boolean>()
|
||||
val navigateToLogin = _navigateToLogin.asSharedFlow()
|
||||
|
||||
sealed class UiState {
|
||||
data class Normal(val servers: List<Server>) : UiState()
|
||||
data object Loading : UiState()
|
||||
|
@ -62,7 +65,19 @@ constructor(
|
|||
viewModelScope.launch {
|
||||
val serverWithAddressesAndUsers = database.getServerWithAddressesAndUsers(server.id) ?: return@launch
|
||||
val serverAddress = serverWithAddressesAndUsers.addresses.firstOrNull { it.id == server.currentServerAddressId } ?: return@launch
|
||||
val user = serverWithAddressesAndUsers.users.firstOrNull { it.id == server.currentUserId } ?: return@launch
|
||||
val user = serverWithAddressesAndUsers.users.firstOrNull { it.id == server.currentUserId }
|
||||
|
||||
// If server has no selected user, navigate to login fragment
|
||||
if (user == null) {
|
||||
jellyfinApi.apply {
|
||||
api.baseUrl = serverAddress.address
|
||||
api.accessToken = null
|
||||
userId = null
|
||||
}
|
||||
appPreferences.currentServer = server.id
|
||||
_navigateToLogin.emit(true)
|
||||
return@launch
|
||||
}
|
||||
|
||||
jellyfinApi.apply {
|
||||
api.baseUrl = serverAddress.address
|
||||
|
|
Loading…
Reference in a new issue