feat: auto offline
This commit is contained in:
commit
dedb99b73d
7 changed files with 56 additions and 3 deletions
|
@ -4,6 +4,7 @@ import android.os.Bundle
|
|||
import android.view.View
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavGraph
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
|
@ -22,6 +23,10 @@ import com.nomadics9.ananas.database.ServerDatabaseDao
|
|||
import com.nomadics9.ananas.databinding.ActivityMainBinding
|
||||
import com.nomadics9.ananas.viewmodels.MainViewModel
|
||||
import com.nomadics9.ananas.work.SyncWorker
|
||||
import com.nomadics9.ananas.repository.JellyfinRepository
|
||||
import com.nomadics9.ananas.utils.restart
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
import com.nomadics9.ananas.core.R as CoreR
|
||||
|
||||
|
@ -35,6 +40,9 @@ class MainActivity : AppCompatActivity() {
|
|||
@Inject
|
||||
lateinit var database: ServerDatabaseDao
|
||||
|
||||
@Inject
|
||||
lateinit var jellyfinRepository: JellyfinRepository
|
||||
|
||||
@Inject
|
||||
lateinit var appPreferences: AppPreferences
|
||||
|
||||
|
@ -68,10 +76,18 @@ class MainActivity : AppCompatActivity() {
|
|||
val navView: NavigationBarView = binding.navView as NavigationBarView
|
||||
|
||||
if (appPreferences.offlineMode) {
|
||||
appPreferences.isOffline = true
|
||||
}
|
||||
|
||||
if (appPreferences.isOffline) {
|
||||
navView.menu.clear()
|
||||
navView.inflateMenu(CoreR.menu.bottom_nav_menu_offline)
|
||||
}
|
||||
|
||||
if (!appPreferences.isOffline && appPreferences.autoOffline) {
|
||||
testServerConnection()
|
||||
}
|
||||
|
||||
setSupportActionBar(binding.mainToolbar)
|
||||
|
||||
// Passing each menu ID as a set of Ids because each
|
||||
|
@ -152,4 +168,18 @@ class MainActivity : AppCompatActivity() {
|
|||
setTheme(CoreR.style.ThemeOverlay_Findroid_Amoled)
|
||||
}
|
||||
}
|
||||
|
||||
private fun testServerConnection() {
|
||||
val activity = this
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
jellyfinRepository.getPublicSystemInfo()
|
||||
// Give the UI a chance to load
|
||||
delay(100)
|
||||
} catch (e: Exception) {
|
||||
appPreferences.isOffline = true
|
||||
activity.restart()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ object RepositoryModule {
|
|||
appPreferences: AppPreferences,
|
||||
): JellyfinRepository {
|
||||
println("Creating new JellyfinRepository")
|
||||
return when (appPreferences.offlineMode) {
|
||||
return when (appPreferences.isOffline) {
|
||||
true -> jellyfinRepositoryOfflineImpl
|
||||
false -> jellyfinRepositoryImpl
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ constructor(
|
|||
private fun testServerConnection() {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
if (appPreferences.offlineMode) return@launch
|
||||
if (appPreferences.isOffline) return@launch
|
||||
repository.getPublicSystemInfo()
|
||||
// Give the UI a chance to load
|
||||
delay(100)
|
||||
|
|
|
@ -182,6 +182,7 @@
|
|||
<string name="cancel_download_message">Are you sure you want to cancel the download?</string>
|
||||
<string name="stop_download">Stop download</string>
|
||||
<string name="privacy_policy_notice">By using Ananas you agree with the <a href='https://raw.githubusercontent.com/nomadics9/ananas/main/PRIVACY'>Privacy Policy</a> which states that we do not collect any data</string>
|
||||
<string name="turn_on_offline_mode_automatically">Turn on offline mode automatically</string>
|
||||
<string name="no_servers_found">No servers found</string>
|
||||
<string name="no_users_found">No users found</string>
|
||||
<string name="select_user">Select user</string>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<EditTextPreference
|
||||
app:defaultValue="30000"
|
||||
app:key="pref_network_request_timeout"
|
||||
|
@ -15,4 +16,8 @@
|
|||
app:key="pref_network_socket_timeout"
|
||||
app:title="@string/settings_socket_timeout"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
<SwitchPreferenceCompat
|
||||
android:defaultValue="false"
|
||||
app:key="pref_auto_offline"
|
||||
app:title="@string/turn_on_offline_mode_automatically" />
|
||||
</PreferenceScreen>
|
|
@ -25,11 +25,27 @@ constructor(
|
|||
var offlineMode
|
||||
get() = sharedPreferences.getBoolean(Constants.PREF_OFFLINE_MODE, false)
|
||||
set(value) {
|
||||
isOffline = value
|
||||
sharedPreferences.edit {
|
||||
putBoolean(Constants.PREF_OFFLINE_MODE, value)
|
||||
}
|
||||
}
|
||||
|
||||
var autoOffline
|
||||
get() = sharedPreferences.getBoolean(Constants.PREF_AUTO_OFFLINE, false)
|
||||
set(value) {
|
||||
sharedPreferences.edit {
|
||||
putBoolean(Constants.PREF_AUTO_OFFLINE, value)
|
||||
}
|
||||
}
|
||||
|
||||
private var _isOffline = sharedPreferences.getBoolean(Constants.PREF_OFFLINE_MODE, false)
|
||||
var isOffline
|
||||
get() = _isOffline
|
||||
set(value) {
|
||||
_isOffline = value
|
||||
}
|
||||
|
||||
// Appearance
|
||||
val theme get() = sharedPreferences.getString(Constants.PREF_THEME, null)
|
||||
val dynamicColors get() = sharedPreferences.getBoolean(Constants.PREF_DYNAMIC_COLORS, true)
|
||||
|
|
|
@ -12,6 +12,7 @@ object Constants {
|
|||
// pref
|
||||
const val PREF_CURRENT_SERVER = "pref_current_server"
|
||||
const val PREF_OFFLINE_MODE = "pref_offline_mode"
|
||||
const val PREF_AUTO_OFFLINE = "pref_auto_offline"
|
||||
const val PREF_PLAYER_GESTURES = "pref_player_gestures"
|
||||
const val PREF_PLAYER_GESTURES_VB = "pref_player_gestures_vb"
|
||||
const val PREF_PLAYER_GESTURES_ZOOM = "pref_player_gestures_zoom"
|
||||
|
|
Loading…
Reference in a new issue