mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-13 17:38:42 -04:00
Merge pull request #2373 from s-ayush2903/chore/s-ayush2903/#2200-SharedPrefUtils-to-Kotlin
feat: Converted SharedPrefUtil to Kotlin and Set the OnlineLib Working
This commit is contained in:
commit
8c032fcbc9
@ -24,7 +24,7 @@ import androidx.preference.Preference
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.Companion.PREF_STORAGE
|
||||
|
||||
class KiwixPrefsFragment : CorePrefsFragment() {
|
||||
|
||||
|
@ -1,216 +0,0 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.kiwix.kiwixmobile.core.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import org.kiwix.kiwixmobile.core.CoreApp;
|
||||
import org.kiwix.kiwixmobile.core.NightModeConfig;
|
||||
|
||||
/**
|
||||
* Manager for the Default Shared Preferences of the application.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
public class SharedPreferenceUtil {
|
||||
// Prefs
|
||||
public static final String PREF_LANG = "pref_language_chooser";
|
||||
public static final String PREF_STORAGE = "pref_select_folder";
|
||||
public static final String PREF_WIFI_ONLY = "pref_wifi_only";
|
||||
public static final String PREF_KIWIX_MOBILE = "kiwix-mobile";
|
||||
public static final String PREF_SHOW_INTRO = "showIntro";
|
||||
private static final String PREF_BACK_TO_TOP = "pref_backtotop";
|
||||
private static final String PREF_FULLSCREEN = "pref_fullscreen";
|
||||
private static final String PREF_NEW_TAB_BACKGROUND = "pref_newtab_background";
|
||||
private static final String PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup";
|
||||
private static final String PREF_IS_FIRST_RUN = "isFirstRun";
|
||||
private static final String PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book";
|
||||
private static final String PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book";
|
||||
private static final String PREF_HOSTED_BOOKS = "hosted_books";
|
||||
public static final String PREF_NIGHT_MODE = "pref_night_mode";
|
||||
private static final String TEXT_ZOOM = "true_text_zoom";
|
||||
private SharedPreferences sharedPreferences;
|
||||
private final PublishProcessor<String> prefStorages = PublishProcessor.create();
|
||||
private final PublishProcessor<Integer> textZooms = PublishProcessor.create();
|
||||
private final PublishProcessor<NightModeConfig.Mode> nightModes = PublishProcessor.create();
|
||||
|
||||
@Inject
|
||||
public SharedPreferenceUtil(Context context) {
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
public boolean getPrefWifiOnly() {
|
||||
return sharedPreferences.getBoolean(PREF_WIFI_ONLY, true);
|
||||
}
|
||||
|
||||
public boolean getPrefIsFirstRun() {
|
||||
return sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true);
|
||||
}
|
||||
|
||||
public boolean getPrefFullScreen() {
|
||||
return sharedPreferences.getBoolean(PREF_FULLSCREEN, false);
|
||||
}
|
||||
|
||||
public boolean getPrefBackToTop() {
|
||||
return sharedPreferences.getBoolean(PREF_BACK_TO_TOP, false);
|
||||
}
|
||||
|
||||
public boolean getPrefNewTabBackground() {
|
||||
return sharedPreferences.getBoolean(PREF_NEW_TAB_BACKGROUND, false);
|
||||
}
|
||||
|
||||
public boolean getPrefExternalLinkPopup() {
|
||||
return sharedPreferences.getBoolean(PREF_EXTERNAL_LINK_POPUP, true);
|
||||
}
|
||||
|
||||
public String getPrefLanguage() {
|
||||
return sharedPreferences.getString(PREF_LANG, Locale.ROOT.toString());
|
||||
}
|
||||
|
||||
public String getPrefStorage() {
|
||||
final String storage = sharedPreferences.getString(PREF_STORAGE, null);
|
||||
if (storage == null) {
|
||||
final String defaultStorage = defaultStorage();
|
||||
putPrefStorage(defaultStorage);
|
||||
return defaultStorage;
|
||||
} else if (!new File(storage).exists()) {
|
||||
return defaultStorage();
|
||||
}
|
||||
return storage;
|
||||
}
|
||||
|
||||
private String defaultStorage() {
|
||||
final File externalFilesDir =
|
||||
ContextCompat.getExternalFilesDirs(CoreApp.getInstance(), null)[0];
|
||||
return externalFilesDir != null ? externalFilesDir.getPath()
|
||||
: CoreApp.getInstance().getFilesDir().getPath(); // workaround for emulators
|
||||
}
|
||||
|
||||
public void putPrefLanguage(String language) {
|
||||
sharedPreferences.edit().putString(PREF_LANG, language).apply();
|
||||
}
|
||||
|
||||
public void putPrefIsFirstRun(boolean isFirstRun) {
|
||||
sharedPreferences.edit().putBoolean(PREF_IS_FIRST_RUN, isFirstRun).apply();
|
||||
}
|
||||
|
||||
public void putPrefWifiOnly(boolean wifiOnly) {
|
||||
sharedPreferences.edit().putBoolean(PREF_WIFI_ONLY, wifiOnly).apply();
|
||||
}
|
||||
|
||||
public void putPrefStorage(String storage) {
|
||||
sharedPreferences.edit().putString(PREF_STORAGE, storage).apply();
|
||||
prefStorages.onNext(storage);
|
||||
}
|
||||
|
||||
public Flowable<String> getPrefStorages() {
|
||||
return prefStorages.startWith(getPrefStorage());
|
||||
}
|
||||
|
||||
public void putPrefFullScreen(boolean fullScreen) {
|
||||
sharedPreferences.edit().putBoolean(PREF_FULLSCREEN, fullScreen).apply();
|
||||
}
|
||||
|
||||
public void putPrefExternalLinkPopup(boolean externalLinkPopup) {
|
||||
sharedPreferences.edit().putBoolean(PREF_EXTERNAL_LINK_POPUP, externalLinkPopup).apply();
|
||||
}
|
||||
|
||||
public boolean showIntro() {
|
||||
return sharedPreferences.getBoolean(PREF_SHOW_INTRO, true);
|
||||
}
|
||||
|
||||
public void setIntroShown() {
|
||||
sharedPreferences.edit().putBoolean(PREF_SHOW_INTRO, false).apply();
|
||||
}
|
||||
|
||||
public boolean getShowHistoryAllBooks() {
|
||||
return sharedPreferences.getBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, true);
|
||||
}
|
||||
|
||||
public void setShowHistoryAllBooks(boolean prefShowHistoryAllBooks) {
|
||||
sharedPreferences.edit()
|
||||
.putBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, prefShowHistoryAllBooks)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public boolean getShowBookmarksAllBooks() {
|
||||
return sharedPreferences.getBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, true);
|
||||
}
|
||||
|
||||
public void setShowBookmarksAllBooks(boolean prefShowBookmarksFromCurrentBook) {
|
||||
sharedPreferences.edit()
|
||||
.putBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, prefShowBookmarksFromCurrentBook)
|
||||
.apply();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public NightModeConfig.Mode getNightMode() {
|
||||
return NightModeConfig.Mode.from(
|
||||
Integer.parseInt(
|
||||
sharedPreferences.getString(
|
||||
PREF_NIGHT_MODE,
|
||||
"" + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Flowable<NightModeConfig.Mode> nightModes() {
|
||||
return nightModes.startWith(getNightMode());
|
||||
}
|
||||
|
||||
public void updateNightMode() {
|
||||
nightModes.offer(getNightMode());
|
||||
}
|
||||
|
||||
public Set<String> getHostedBooks() {
|
||||
return sharedPreferences.getStringSet(PREF_HOSTED_BOOKS, new HashSet<>());
|
||||
}
|
||||
|
||||
public void setHostedBooks(Set<String> hostedBooks) {
|
||||
sharedPreferences.edit()
|
||||
.putStringSet(PREF_HOSTED_BOOKS, hostedBooks)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public void setTextZoom(int textZoom) {
|
||||
sharedPreferences.edit().putInt(TEXT_ZOOM, textZoom).apply();
|
||||
textZooms.offer(textZoom);
|
||||
}
|
||||
|
||||
public int getTextZoom() {
|
||||
return sharedPreferences.getInt(TEXT_ZOOM, 100);
|
||||
}
|
||||
|
||||
public Flowable<Integer> getTextZooms() {
|
||||
return textZooms.startWith(getTextZoom());
|
||||
}
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.core.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.preference.PreferenceManager
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.content.ContextCompat.getExternalFilesDirs
|
||||
import androidx.core.content.edit
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.processors.PublishProcessor
|
||||
import org.kiwix.kiwixmobile.core.NightModeConfig
|
||||
import org.kiwix.kiwixmobile.core.NightModeConfig.Mode.Companion.from
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Manager for the Default Shared Preferences of the application.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
class SharedPreferenceUtil @Inject constructor(val context: Context) {
|
||||
private val sharedPreferences: SharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val _prefStorages = PublishProcessor.create<String>()
|
||||
val prefStorages
|
||||
get() = _prefStorages.startWith(prefStorage)
|
||||
private val _textZooms = PublishProcessor.create<Int>()
|
||||
val textZooms
|
||||
get() = _textZooms.startWith(textZoom)
|
||||
private val nightModes = PublishProcessor.create<NightModeConfig.Mode>()
|
||||
|
||||
val prefWifiOnly: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_WIFI_ONLY, true)
|
||||
|
||||
val prefIsFirstRun: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true)
|
||||
|
||||
val prefFullScreen: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_FULLSCREEN, false)
|
||||
|
||||
val prefBackToTop: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_BACK_TO_TOP, false)
|
||||
|
||||
val prefNewTabBackground: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_NEW_TAB_BACKGROUND, false)
|
||||
|
||||
val prefExternalLinkPopup: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_EXTERNAL_LINK_POPUP, true)
|
||||
|
||||
val prefLanguage: String
|
||||
get() = sharedPreferences.getString(PREF_LANG, "") ?: Locale.ROOT.toString()
|
||||
|
||||
val prefStorage: String
|
||||
get() {
|
||||
val storage = sharedPreferences.getString(PREF_STORAGE, null)
|
||||
return when {
|
||||
storage == null -> defaultStorage().also(::putPrefStorage)
|
||||
!File(storage).exists() -> defaultStorage()
|
||||
else -> storage
|
||||
}
|
||||
}
|
||||
|
||||
private fun defaultStorage(): String =
|
||||
getExternalFilesDirs(context, null)[0]?.path
|
||||
?: context.filesDir.path // a workaround for emulators
|
||||
|
||||
fun getPrefStorageTitle(defaultTitle: String): String =
|
||||
sharedPreferences.getString(PREF_STORAGE_TITLE, defaultTitle) ?: defaultTitle
|
||||
|
||||
fun putPrefLanguage(language: String) =
|
||||
sharedPreferences.edit { putString(PREF_LANG, language) }
|
||||
|
||||
fun putPrefIsFirstRun(isFirstRun: Boolean) =
|
||||
sharedPreferences.edit { putBoolean(PREF_IS_FIRST_RUN, isFirstRun) }
|
||||
|
||||
fun putPrefWifiOnly(wifiOnly: Boolean) =
|
||||
sharedPreferences.edit { putBoolean(PREF_WIFI_ONLY, wifiOnly) }
|
||||
|
||||
fun putPrefStorageTitle(storageTitle: String) =
|
||||
sharedPreferences.edit { putString(PREF_STORAGE_TITLE, storageTitle) }
|
||||
|
||||
fun putPrefStorage(storage: String) {
|
||||
sharedPreferences.edit { putString(PREF_STORAGE, storage) }
|
||||
_prefStorages.onNext(storage)
|
||||
}
|
||||
|
||||
fun putPrefFullScreen(fullScreen: Boolean) =
|
||||
sharedPreferences.edit { putBoolean(PREF_FULLSCREEN, fullScreen) }
|
||||
|
||||
fun putPrefExternalLinkPopup(externalLinkPopup: Boolean) =
|
||||
sharedPreferences.edit { putBoolean(PREF_EXTERNAL_LINK_POPUP, externalLinkPopup) }
|
||||
|
||||
fun showIntro(): Boolean = sharedPreferences.getBoolean(PREF_SHOW_INTRO, true)
|
||||
|
||||
fun setIntroShown() = sharedPreferences.edit { putBoolean(PREF_SHOW_INTRO, false) }
|
||||
|
||||
var showHistoryAllBooks: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, true)
|
||||
set(prefShowHistoryAllBooks) {
|
||||
sharedPreferences.edit { putBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, prefShowHistoryAllBooks) }
|
||||
}
|
||||
|
||||
var showBookmarksAllBooks: Boolean
|
||||
get() = sharedPreferences.getBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, true)
|
||||
set(prefShowBookmarksFromCurrentBook) = sharedPreferences.edit {
|
||||
putBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, prefShowBookmarksFromCurrentBook)
|
||||
}
|
||||
|
||||
val nightMode: NightModeConfig.Mode
|
||||
get() = from(
|
||||
sharedPreferences.getString(PREF_NIGHT_MODE, null)?.toInt()
|
||||
?: AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||
)
|
||||
|
||||
fun nightModes(): Flowable<NightModeConfig.Mode> = nightModes.startWith(nightMode)
|
||||
|
||||
fun updateNightMode() = nightModes.offer(nightMode)
|
||||
|
||||
var hostedBooks: Set<String>
|
||||
get() = sharedPreferences.getStringSet(PREF_HOSTED_BOOKS, null)?.toHashSet() ?: HashSet()
|
||||
set(hostedBooks) {
|
||||
sharedPreferences.edit { putStringSet(PREF_HOSTED_BOOKS, hostedBooks) }
|
||||
}
|
||||
|
||||
var textZoom: Int
|
||||
get() = sharedPreferences.getInt(TEXT_ZOOM, DEFAULT_ZOOM)
|
||||
set(textZoom) {
|
||||
sharedPreferences.edit { putInt(TEXT_ZOOM, textZoom) }
|
||||
_textZooms.offer(textZoom)
|
||||
}
|
||||
|
||||
companion object {
|
||||
// Prefs
|
||||
const val PREF_LANG = "pref_language_chooser"
|
||||
const val PREF_STORAGE = "pref_select_folder"
|
||||
const val PREF_WIFI_ONLY = "pref_wifi_only"
|
||||
const val PREF_KIWIX_MOBILE = "kiwix-mobile"
|
||||
const val PREF_SHOW_INTRO = "showIntro"
|
||||
private const val PREF_BACK_TO_TOP = "pref_backtotop"
|
||||
private const val PREF_FULLSCREEN = "pref_fullscreen"
|
||||
private const val PREF_NEW_TAB_BACKGROUND = "pref_newtab_background"
|
||||
private const val PREF_STORAGE_TITLE = "pref_selected_title"
|
||||
private const val PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup"
|
||||
private const val PREF_IS_FIRST_RUN = "isFirstRun"
|
||||
private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book"
|
||||
private const val PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book"
|
||||
private const val PREF_HOSTED_BOOKS = "hosted_books"
|
||||
const val PREF_NIGHT_MODE = "pref_night_mode"
|
||||
private const val TEXT_ZOOM = "true_text_zoom"
|
||||
private const val DEFAULT_ZOOM = 100
|
||||
}
|
||||
}
|
@ -20,8 +20,8 @@ package org.kiwix.kiwixmobile.custom.settings
|
||||
|
||||
import android.os.Bundle
|
||||
import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_LANG
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_WIFI_ONLY
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.Companion.PREF_LANG
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.Companion.PREF_WIFI_ONLY
|
||||
import org.kiwix.kiwixmobile.custom.BuildConfig
|
||||
|
||||
class CustomPrefsFragment : CorePrefsFragment() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user