mirror of
https://github.com/NiciDieNase/chaosflix
synced 2024-11-23 04:43:07 +00:00
Touch: add import and export for Favorites in Settings
This commit is contained in:
parent
d3d97c9c5c
commit
69bb3697af
5 changed files with 145 additions and 17 deletions
|
@ -1,13 +1,89 @@
|
|||
package de.nicidienase.chaosflix.common.viewmodel
|
||||
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import de.nicidienase.chaosflix.common.mediadata.sync.Downloader
|
||||
import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem
|
||||
import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItemDao
|
||||
import de.nicidienase.chaosflix.common.util.LiveEvent
|
||||
import de.nicidienase.chaosflix.common.util.ThreadHandler
|
||||
import java.io.BufferedReader
|
||||
import java.io.BufferedWriter
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.FileWriter
|
||||
import java.lang.Exception
|
||||
|
||||
class PreferencesViewModel(val downloader: Downloader,
|
||||
val watchlistItemDao: WatchlistItemDao,
|
||||
val exportDir: File) : ViewModel() {
|
||||
val gson = Gson()
|
||||
|
||||
private val threadHandler = ThreadHandler()
|
||||
|
||||
class PreferencesViewModel(val downloader: Downloader) : ViewModel() {
|
||||
fun cleanNonUserData() {
|
||||
ThreadHandler().runOnBackgroundThread {
|
||||
threadHandler.runOnBackgroundThread {
|
||||
downloader.deleteNonUserData()
|
||||
}
|
||||
}
|
||||
|
||||
fun exportFavorites() {
|
||||
threadHandler.runOnBackgroundThread {
|
||||
val favorites = watchlistItemDao.getAllSync()
|
||||
val json = gson.toJson(favorites)
|
||||
Log.d(TAG, json)
|
||||
if (exportDir.isDirectory) {
|
||||
val file = File("${exportDir.path}${File.separator}$FAVORITES_FILENAME")
|
||||
if (file.exists()) {
|
||||
file.delete()
|
||||
file.createNewFile()
|
||||
}
|
||||
try {
|
||||
val fileWriter = FileWriter(file)
|
||||
val bufferedWriter = BufferedWriter(fileWriter)
|
||||
bufferedWriter.write(json)
|
||||
bufferedWriter.close()
|
||||
fileWriter.close()
|
||||
Log.d(TAG, file.path)
|
||||
} catch (ex: Exception){
|
||||
ex.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importFavorites(): MutableLiveData<LiveEvent<State, List<WatchlistItem>, Exception>> {
|
||||
val mutableLiveData = MutableLiveData<LiveEvent<State,List<WatchlistItem>,Exception>>()
|
||||
mutableLiveData.postValue(LiveEvent(State.Loading,null,null))
|
||||
threadHandler.runOnBackgroundThread {
|
||||
val file = File("${exportDir.path}${File.separator}$FAVORITES_FILENAME")
|
||||
try {
|
||||
if(file.exists()){
|
||||
val fileReader = FileReader(file)
|
||||
val bufferedReader = BufferedReader(fileReader)
|
||||
val json: String = bufferedReader.readText()
|
||||
val fromJson = gson.fromJson(json, Array<WatchlistItem>::class.java)
|
||||
fromJson.map { watchlistItemDao.saveItem(WatchlistItem(eventGuid = it.eventGuid)) }
|
||||
mutableLiveData.postValue(LiveEvent(State.Done,fromJson.asList(),null))
|
||||
} else {
|
||||
mutableLiveData.postValue(LiveEvent(State.Done,null,null))
|
||||
}
|
||||
} catch (ex: Exception){
|
||||
mutableLiveData.postValue(LiveEvent(State.Done,null,ex))
|
||||
}
|
||||
}
|
||||
return mutableLiveData
|
||||
}
|
||||
|
||||
enum class State {
|
||||
Loading, Done
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TAG = PreferencesViewModel::class.java.simpleName
|
||||
val FAVORITES_FILENAME = "chaosflix_favorites.json"
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package de.nicidienase.chaosflix.common.viewmodel
|
|||
import android.arch.lifecycle.ViewModel
|
||||
import android.arch.lifecycle.ViewModelProvider
|
||||
import android.content.Context
|
||||
import android.os.Environment
|
||||
import android.preference.PreferenceManager
|
||||
import de.nicidienase.chaosflix.common.DatabaseFactory
|
||||
import de.nicidienase.chaosflix.common.OfflineItemManager
|
||||
|
@ -22,6 +23,7 @@ class ViewModelFactory(context: Context) : ViewModelProvider.Factory {
|
|||
val offlineItemManager =
|
||||
OfflineItemManager(context.applicationContext, database.offlineEventDao(),preferencesManager)
|
||||
val downloader by lazy { Downloader(recordingApi, database) }
|
||||
val externalFilesDir = Environment.getExternalStorageDirectory()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
|
@ -32,7 +34,7 @@ class ViewModelFactory(context: Context) : ViewModelProvider.Factory {
|
|||
} else if (modelClass.isAssignableFrom(DetailsViewModel::class.java)) {
|
||||
return DetailsViewModel(database, offlineItemManager, preferencesManager, downloader) as T
|
||||
} else if (modelClass.isAssignableFrom(PreferencesViewModel::class.java)){
|
||||
return PreferencesViewModel(downloader) as T
|
||||
return PreferencesViewModel(downloader, database.watchlistItemDao(), externalFilesDir) as T
|
||||
} else {
|
||||
throw UnsupportedOperationException("The requested ViewModel is currently unsupported. " +
|
||||
"Please make sure to implement are correct creation of it. " +
|
||||
|
|
|
@ -45,4 +45,6 @@
|
|||
<string name="setting_choose_stream">Automatically choose stream</string>
|
||||
<string name="setting_metered_networks">Allow downloads over metered networks</string>
|
||||
<string name="settings_choose_recording">Automatically choose recording</string>
|
||||
<string name="export_favorites">Export Favorites</string>
|
||||
<string name="import_favorites">Import Favorites</string>
|
||||
</resources>
|
||||
|
|
|
@ -2,31 +2,45 @@
|
|||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:key="allow_metered_networks"
|
||||
android:defaultValue="false"
|
||||
android:title="@string/setting_metered_networks"
|
||||
android:summary="@string/pref_mobile_downloads"/>
|
||||
android:summary="@string/pref_mobile_downloads"
|
||||
android:title="@string/setting_metered_networks"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:key="auto_select_stream"
|
||||
android:defaultValue="false"
|
||||
android:title="@string/setting_choose_stream"
|
||||
android:summary="@string/pref_autoselect_stream"/>
|
||||
android:summary="@string/pref_autoselect_stream"
|
||||
android:title="@string/setting_choose_stream"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="auto_select_recording"
|
||||
android:defaultValue="false"
|
||||
android:title="@string/settings_choose_recording"
|
||||
android:summary="@string/pref_autoselect_recording"/>
|
||||
android:key="auto_select_recording"
|
||||
android:summary="@string/pref_autoselect_recording"
|
||||
android:title="@string/settings_choose_recording"/>
|
||||
|
||||
<Preference
|
||||
android:id="@+id/download_folder"
|
||||
android:key="download_folder"
|
||||
android:title="@string/download_folder"/>
|
||||
|
||||
<Preference
|
||||
android:id="@+id/deleteData"
|
||||
android:key="delete_data"
|
||||
android:title="@string/clean_cache"/>
|
||||
<PreferenceCategory
|
||||
android:title="Debug">
|
||||
|
||||
<Preference
|
||||
android:id="@+id/deleteData"
|
||||
android:key="delete_data"
|
||||
android:title="@string/clean_cache"/>
|
||||
<Preference
|
||||
android:id="@+id/export_favorites"
|
||||
android:key="export_favorites"
|
||||
android:title="@string/export_favorites"/>
|
||||
<Preference
|
||||
android:id="@+id/import_favorites"
|
||||
android:key="import_favorites"
|
||||
android:title="@string/import_favorites"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
|
||||
</PreferenceScreen>
|
|
@ -1,10 +1,12 @@
|
|||
package de.nicidienase.chaosflix.touch.settings
|
||||
|
||||
import android.arch.lifecycle.Observer
|
||||
import android.arch.lifecycle.ViewModelProviders
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.preference.PreferenceManager
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v7.preference.PreferenceFragmentCompat
|
||||
import de.nicidienase.chaosflix.R
|
||||
import de.nicidienase.chaosflix.common.viewmodel.PreferencesViewModel
|
||||
|
@ -30,7 +32,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
|
||||
if (requestCode == REQUEST_DIRECTORY) {
|
||||
if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {
|
||||
val dir = data!!.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)
|
||||
val dir = data?.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)
|
||||
val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext().applicationContext)
|
||||
val edit = sharedPref.edit()
|
||||
edit.putString("download_folder", dir)
|
||||
|
@ -52,6 +54,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
updateSummary()
|
||||
val downloadFolderPref = this.findPreference("download_folder")
|
||||
val cleanCachePref = this.findPreference("delete_data")
|
||||
val exportFavorites = this.findPreference("export_favorites")
|
||||
val importFavorites = this.findPreference("import_favorites")
|
||||
|
||||
downloadFolderPref?.setOnPreferenceClickListener {
|
||||
val chooserIntent = Intent(context, DirectoryChooserActivity::class.java)
|
||||
|
@ -72,6 +76,36 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
viewModel.cleanNonUserData()
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
|
||||
exportFavorites?.setOnPreferenceClickListener {
|
||||
viewModel.exportFavorites()
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
|
||||
importFavorites?.setOnPreferenceClickListener {
|
||||
var snackbar: Snackbar? = null
|
||||
viewModel.importFavorites().observe(this, Observer { event ->
|
||||
when {
|
||||
event?.state == PreferencesViewModel.State.Loading -> {
|
||||
snackbar?.dismiss()
|
||||
snackbar = Snackbar.make(listView,"Importing",Snackbar.LENGTH_INDEFINITE)
|
||||
snackbar?.show()
|
||||
}
|
||||
event?.error != null -> {
|
||||
snackbar?.dismiss()
|
||||
val message: String = event.error?.message ?: event.error.toString()
|
||||
snackbar = Snackbar.make(listView, message, Snackbar.LENGTH_SHORT)
|
||||
snackbar?.show()
|
||||
}
|
||||
event?.state == PreferencesViewModel.State.Done -> {
|
||||
snackbar?.dismiss()
|
||||
snackbar = Snackbar.make(listView,"Import Done",Snackbar.LENGTH_SHORT)
|
||||
snackbar?.show()
|
||||
}
|
||||
}
|
||||
})
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
Loading…
Reference in a new issue