parent
4cb8898793
commit
94e5065677
@ -0,0 +1,10 @@ |
||||
package io.legado.app.ui.config |
||||
|
||||
object ConfigTag { |
||||
|
||||
const val OTHER_CONFIG = "otherConfig" |
||||
const val THEME_CONFIG = "themeConfig" |
||||
const val BACKUP_CONFIG = "backupConfig" |
||||
const val COVER_CONFIG = "coverConfig" |
||||
|
||||
} |
@ -1,15 +0,0 @@ |
||||
package io.legado.app.ui.config |
||||
|
||||
import android.app.Application |
||||
import androidx.lifecycle.AndroidViewModel |
||||
|
||||
class ConfigViewModel(application: Application) : AndroidViewModel(application) { |
||||
companion object { |
||||
const val TYPE_CONFIG = 0 |
||||
const val TYPE_THEME_CONFIG = 1 |
||||
const val TYPE_WEB_DAV_CONFIG = 2 |
||||
} |
||||
|
||||
var configType: Int = TYPE_CONFIG |
||||
|
||||
} |
@ -0,0 +1,119 @@ |
||||
package io.legado.app.ui.config |
||||
|
||||
import android.annotation.SuppressLint |
||||
import android.content.SharedPreferences |
||||
import android.net.Uri |
||||
import android.os.Bundle |
||||
import android.view.View |
||||
import androidx.preference.Preference |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BasePreferenceFragment |
||||
import io.legado.app.constant.PreferKey |
||||
import io.legado.app.lib.dialogs.selector |
||||
import io.legado.app.lib.theme.ATH |
||||
import io.legado.app.ui.widget.image.CoverImageView |
||||
import io.legado.app.utils.* |
||||
|
||||
class CoverConfigFragment : BasePreferenceFragment(), |
||||
SharedPreferences.OnSharedPreferenceChangeListener { |
||||
|
||||
private val requestCodeCover = 111 |
||||
private val requestCodeCoverDark = 112 |
||||
private val selectImage = registerForActivityResult(SelectImageContract()) { |
||||
val uri = it?.second ?: return@registerForActivityResult |
||||
when (it.first) { |
||||
requestCodeCover -> setCoverFromUri(PreferKey.defaultCover, uri) |
||||
requestCodeCoverDark -> setCoverFromUri(PreferKey.defaultCoverDark, uri) |
||||
} |
||||
} |
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { |
||||
addPreferencesFromResource(R.xml.pref_config_cover) |
||||
upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) |
||||
upPreferenceSummary(PreferKey.defaultCoverDark, getPrefString(PreferKey.defaultCoverDark)) |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
activity?.setTitle(R.string.cover_config) |
||||
ATH.applyEdgeEffectColor(listView) |
||||
setHasOptionsMenu(true) |
||||
} |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) |
||||
} |
||||
|
||||
override fun onDestroy() { |
||||
super.onDestroy() |
||||
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this) |
||||
} |
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { |
||||
sharedPreferences ?: return |
||||
when (key) { |
||||
PreferKey.defaultCover, |
||||
PreferKey.defaultCoverDark -> { |
||||
upPreferenceSummary(key, getPrefString(key)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
@SuppressLint("PrivateResource") |
||||
override fun onPreferenceTreeClick(preference: Preference?): Boolean { |
||||
when (val key = preference?.key) { |
||||
PreferKey.defaultCover -> |
||||
if (getPrefString(PreferKey.defaultCover).isNullOrEmpty()) { |
||||
selectImage.launch(requestCodeCover) |
||||
} else { |
||||
context?.selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> |
||||
if (i == 0) { |
||||
removePref(PreferKey.defaultCover) |
||||
CoverImageView.upDefaultCover() |
||||
} else { |
||||
selectImage.launch(requestCodeCover) |
||||
} |
||||
} |
||||
} |
||||
PreferKey.defaultCoverDark -> |
||||
if (getPrefString(PreferKey.defaultCoverDark).isNullOrEmpty()) { |
||||
selectImage.launch(requestCodeCoverDark) |
||||
} else { |
||||
context?.selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> |
||||
if (i == 0) { |
||||
removePref(PreferKey.defaultCoverDark) |
||||
CoverImageView.upDefaultCover() |
||||
} else { |
||||
selectImage.launch(requestCodeCoverDark) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return super.onPreferenceTreeClick(preference) |
||||
} |
||||
|
||||
private fun upPreferenceSummary(preferenceKey: String, value: String?) { |
||||
val preference = findPreference<Preference>(preferenceKey) ?: return |
||||
when (preferenceKey) { |
||||
PreferKey.defaultCover, |
||||
PreferKey.defaultCoverDark -> preference.summary = if (value.isNullOrBlank()) { |
||||
getString(R.string.select_image) |
||||
} else { |
||||
value |
||||
} |
||||
else -> preference.summary = value |
||||
} |
||||
} |
||||
|
||||
private fun setCoverFromUri(preferenceKey: String, uri: Uri) { |
||||
readUri(uri) { name, bytes -> |
||||
var file = requireContext().externalFiles |
||||
file = FileUtils.createFileIfNotExist(file, "covers", name) |
||||
file.writeBytes(bytes) |
||||
putPrefString(preferenceKey, file.absolutePath) |
||||
CoverImageView.upDefaultCover() |
||||
} |
||||
} |
||||
|
||||
} |
Before Width: | Height: | Size: 260 KiB After Width: | Height: | Size: 5.5 KiB |
@ -0,0 +1,49 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto"> |
||||
|
||||
<io.legado.app.ui.widget.prefs.SwitchPreference |
||||
android:defaultValue="false" |
||||
android:key="useDefaultCover" |
||||
android:summary="@string/use_default_cover_s" |
||||
android:title="@string/use_default_cover" |
||||
app:allowDividerAbove="false" |
||||
app:allowDividerBelow="false" |
||||
app:iconSpaceReserved="false" /> |
||||
|
||||
<io.legado.app.ui.widget.prefs.PreferenceCategory |
||||
android:key="dayThemeCategory" |
||||
android:title="@string/day" |
||||
app:allowDividerAbove="true" |
||||
app:allowDividerBelow="false" |
||||
app:iconSpaceReserved="false" |
||||
app:layout="@layout/view_preference_category"> |
||||
|
||||
<io.legado.app.ui.widget.prefs.Preference |
||||
android:key="defaultCover" |
||||
android:title="@string/default_cover" |
||||
app:allowDividerAbove="false" |
||||
app:allowDividerBelow="false" |
||||
app:iconSpaceReserved="false" /> |
||||
|
||||
</io.legado.app.ui.widget.prefs.PreferenceCategory> |
||||
|
||||
<io.legado.app.ui.widget.prefs.PreferenceCategory |
||||
android:key="nightThemeCategory" |
||||
android:title="@string/night" |
||||
app:allowDividerAbove="true" |
||||
app:allowDividerBelow="false" |
||||
app:iconSpaceReserved="false" |
||||
app:layout="@layout/view_preference_category"> |
||||
|
||||
<io.legado.app.ui.widget.prefs.Preference |
||||
android:key="defaultCoverDark" |
||||
android:title="@string/default_cover" |
||||
app:allowDividerAbove="false" |
||||
app:allowDividerBelow="false" |
||||
app:iconSpaceReserved="false" /> |
||||
|
||||
|
||||
</io.legado.app.ui.widget.prefs.PreferenceCategory> |
||||
|
||||
</androidx.preference.PreferenceScreen> |
Loading…
Reference in new issue