-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/enable-clear-app-data' into develop
- Loading branch information
Showing
6 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
app/src/main/java/com/geckour/egret/view/fragment/SettingAppDataManageFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.geckour.egret.view.fragment | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.support.design.widget.Snackbar | ||
import android.support.v4.app.Fragment | ||
import android.support.v7.preference.PreferenceFragmentCompat | ||
import android.support.v7.preference.PreferenceManager | ||
import android.support.v7.preference.PreferenceScreen | ||
import com.geckour.egret.R | ||
import com.geckour.egret.util.OrmaProvider | ||
import com.geckour.egret.view.activity.SettingActivity | ||
import io.reactivex.Observable | ||
import io.reactivex.Single | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.schedulers.Schedulers | ||
|
||
class SettingAppDataManageFragment: PreferenceFragmentCompat(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback { | ||
|
||
companion object { | ||
val TAG: String = this::class.java.simpleName | ||
|
||
fun newInstance(): SettingAppDataManageFragment = SettingAppDataManageFragment() | ||
} | ||
|
||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.preferences_app_data, rootKey) | ||
|
||
preferenceScreen.findPreference("clear_all_preference").setOnPreferenceClickListener { clearAllPreference() } | ||
preferenceScreen.findPreference("clear_all_draft").setOnPreferenceClickListener { clearAllDraft() } | ||
preferenceScreen.findPreference("clear_all_restriction").setOnPreferenceClickListener { clearAllRestriction() } | ||
preferenceScreen.findPreference("clear_all_db_except_login_info").setOnPreferenceClickListener { clearAllDBExceptLoginInfo() } | ||
preferenceScreen.findPreference("clear_all_db").setOnPreferenceClickListener { clearAllDB() } | ||
} | ||
|
||
override fun getCallbackFragment(): Fragment = this | ||
|
||
override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat?, pref: PreferenceScreen?): Boolean { | ||
caller?.preferenceScreen = pref | ||
return true | ||
} | ||
|
||
private fun clearAllPreference(): Boolean { | ||
PreferenceManager.getDefaultSharedPreferences(activity).edit().clear().apply() | ||
Snackbar.make((activity as SettingActivity).binding.root, R.string.message_complete_clear_pref, Snackbar.LENGTH_SHORT).show() | ||
return true | ||
} | ||
|
||
private fun clearAllDraft(): Boolean { | ||
OrmaProvider.db.deleteFromDraft().executeAsSingle() | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe({ | ||
Snackbar.make((activity as SettingActivity).binding.root, R.string.message_complete_clear_db, Snackbar.LENGTH_SHORT).show() | ||
}, Throwable::printStackTrace) | ||
return true | ||
} | ||
|
||
private fun clearAllRestriction(): Boolean { | ||
Observable.merge( | ||
listOf( | ||
OrmaProvider.db.deleteFromMuteClient().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteHashTag().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteInstance().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteKeyword().executeAsSingle().toObservable() | ||
) | ||
) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe({}, Throwable::printStackTrace, { | ||
Snackbar.make((activity as SettingActivity).binding.root, R.string.message_complete_clear_db, Snackbar.LENGTH_SHORT).show() | ||
}) | ||
return true | ||
} | ||
|
||
private fun clearAllDBExceptLoginInfo(): Boolean { | ||
Observable.merge( | ||
listOf( | ||
OrmaProvider.db.deleteFromDraft().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteClient().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteHashTag().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteInstance().executeAsSingle().toObservable(), | ||
OrmaProvider.db.deleteFromMuteKeyword().executeAsSingle().toObservable() | ||
) | ||
) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe({}, Throwable::printStackTrace, { | ||
Snackbar.make((activity as SettingActivity).binding.root, R.string.message_complete_clear_db, Snackbar.LENGTH_SHORT).show() | ||
}) | ||
return true | ||
} | ||
|
||
private fun clearAllDB(): Boolean { | ||
Single.just(OrmaProvider.db) | ||
.map { it.deleteAll() } | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe({ | ||
Snackbar.make((activity as SettingActivity).binding.root, R.string.message_complete_clear_db, Snackbar.LENGTH_SHORT).show() | ||
}, Throwable::printStackTrace) | ||
|
||
val intent = activity.baseContext.packageManager.getLaunchIntentForPackage(activity.baseContext.packageName) | ||
.apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) } | ||
startActivity(intent) | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<PreferenceCategory android:title="@string/pref_manage_held_data"> | ||
|
||
<PreferenceScreen android:key="clear_all_preference" | ||
android:title="@string/pref_clear_all_preference" /> | ||
|
||
<PreferenceScreen android:key="clear_all_draft" | ||
android:title="@string/pref_clear_all_draft" /> | ||
|
||
<PreferenceScreen android:key="clear_all_restriction" | ||
android:title="@string/pref_clear_all_restriction" /> | ||
|
||
<PreferenceScreen android:key="clear_all_db_except_login_info" | ||
android:title="@string/pref_clear_all_db_except_login_info" /> | ||
|
||
<PreferenceScreen android:key="clear_all_db" | ||
android:title="@string/pref_clear_all_db" /> | ||
</PreferenceCategory> | ||
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters