LibreTube/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt

62 lines
2.5 KiB
Kotlin
Raw Normal View History

2022-06-07 16:12:59 +05:30
package com.github.libretube.preferences
import android.content.Intent
import android.os.Bundle
import android.provider.DocumentsContract
2022-06-07 23:46:58 +05:30
import android.widget.TextView
2022-06-07 16:12:59 +05:30
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.github.libretube.R
2022-06-07 20:22:06 +05:30
class AdvancedSettings : PreferenceFragmentCompat() {
val TAG = "AdvancedSettings"
2022-06-07 16:12:59 +05:30
private val directoryRequestCode = 9999
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
2022-06-07 20:22:06 +05:30
setPreferencesFromResource(R.xml.advanced_settings, rootKey)
val topBarTextView = activity?.findViewById<TextView>(R.id.topBar_textView)
topBarTextView?.text = getString(R.string.advanced)
2022-06-07 23:46:58 +05:30
2022-06-07 20:22:06 +05:30
val clearHistory = findPreference<Preference>("clear_history")
clearHistory?.setOnPreferenceClickListener {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
sharedPreferences.edit().remove("search_history").commit()
true
}
2022-06-07 16:12:59 +05:30
// doesn't work yet
val directory = findPreference<Preference>("download_directory")
directory?.setOnPreferenceClickListener {
2022-06-07 16:14:22 +05:30
val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext())
val pickerInitialUri = sharedPreferences
.getString("download_directory_path", "")?.toUri()
2022-06-07 16:12:59 +05:30
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
2022-06-07 16:14:22 +05:30
startActivityForResult(
Intent.createChooser(intent, "Choose directory"),
directoryRequestCode
)
2022-06-07 16:12:59 +05:30
true
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
directoryRequestCode -> {
val directoryUri = data?.data
// save selected download directory to the shared preferences
2022-06-07 16:14:22 +05:30
val sharedPref = PreferenceManager
2022-06-07 16:12:59 +05:30
.getDefaultSharedPreferences(requireContext())
2022-06-07 16:14:22 +05:30
sharedPref.edit().putString("download_directory_path", directoryUri.toString())
2022-06-07 16:12:59 +05:30
.apply()
}
}
}
2022-06-07 16:14:22 +05:30
}