Skip to content

Commit

Permalink
Merge branch 'feature/enable-clear-app-data' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
geckour committed Aug 29, 2017
2 parents cdb7949 + 48f4f11 commit 737823f
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 8 deletions.
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.PreferenceScreen
import com.geckour.egret.App
import com.geckour.egret.R
import com.geckour.egret.view.activity.SettingActivity

class SettingMainFragment: PreferenceFragmentCompat(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback {

Expand All @@ -21,6 +20,7 @@ class SettingMainFragment: PreferenceFragmentCompat(), PreferenceFragmentCompat.
setPreferencesFromResource(R.xml.preferences_main, rootKey)
preferenceScreen.findPreference("manage_accounts").setOnPreferenceClickListener { showAccountManageFragment() }
preferenceScreen.findPreference("manage_restrictions").setOnPreferenceClickListener { showRestrictFragment() }
preferenceScreen.findPreference("manage_held_data").setOnPreferenceClickListener { showSettingAppDataManageFragment() }
}

override fun getCallbackFragment(): Fragment = this
Expand All @@ -44,7 +44,7 @@ class SettingMainFragment: PreferenceFragmentCompat(), PreferenceFragmentCompat.

private fun showAccountManageFragment(): Boolean {
val fragment = AccountManageFragment.newInstance()
(activity as SettingActivity).supportFragmentManager.beginTransaction()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment, AccountManageFragment.TAG)
.addToBackStack(AccountManageFragment.TAG)
.commit()
Expand All @@ -54,11 +54,20 @@ class SettingMainFragment: PreferenceFragmentCompat(), PreferenceFragmentCompat.

private fun showRestrictFragment(): Boolean {
val fragment = SettingRestrictFragment.newInstance()
(activity as SettingActivity).supportFragmentManager.beginTransaction()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment, SettingRestrictFragment.TAG)
.addToBackStack(SettingRestrictFragment.TAG)
.commit()

return true
}

private fun showSettingAppDataManageFragment(): Boolean {
val fragment = SettingAppDataManageFragment.newInstance()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment, SettingAppDataManageFragment.TAG)
.addToBackStack(SettingAppDataManageFragment.TAG)
.commit()
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ class SettingRestrictFragment: PreferenceFragmentCompat(), PreferenceFragmentCom
companion object {
val TAG: String = this::class.java.simpleName

fun newInstance(): SettingRestrictFragment {
val fragment = SettingRestrictFragment()

return fragment
}
fun newInstance(): SettingRestrictFragment = SettingRestrictFragment()
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
</string>

<string name="message_complete_saving_image">Completed to save the image.</string>
<string name="message_complete_clear_db">Data has cleared.</string>
<string name="message_complete_clear_pref">Preference has cleared.</string>

<string name="message_necessity_wifi_grant">To control streaming by such status, grant authority of accessing Wi-Fi status to app.</string>
<string name="message_necessity_read_storage_grant">To pick medias, grant authority of reading storage to app.</string>
Expand Down Expand Up @@ -146,6 +148,7 @@
<string name="pref_switch_to_use_default_browser">Change scheme to open links</string>
<string name="pref_switch_to_show_extreme_toot_bar">Show bar to post new toot on footer of TL</string>
<string name="pref_manage_notify_real_time">Whether notify real time</string>
<string name="pref_manage_held_data">Manage app\'s data</string>

<string name="pref_stream_always">Always</string>
<string name="pref_stream_wifi">When connect with Wi-Fi</string>
Expand All @@ -168,6 +171,11 @@
<string name="pref_manage_notify_real_time_summary_on">Notify real time</string>
<string name="pref_manage_notify_real_time_summary_off">Do not notify real time</string>
<string name="pref_show_licenses">Show open source licenses</string>
<string name="pref_clear_all_preference">Clear all preference data</string>
<string name="pref_clear_all_draft">Clear all drafts data</string>
<string name="pref_clear_all_restriction">Clear all restrictions data</string>
<string name="pref_clear_all_db_except_login_info">Reset whole database except data used to login</string>
<string name="pref_clear_all_db">Reset whole database</string>

<string name="license_name_stetho">Stetho</string>
<string name="license_name_reactive_x_java">RxJava</string>
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/xml/preferences_app_data.xml
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>
3 changes: 3 additions & 0 deletions app/src/main/res/xml/preferences_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
android:switchTextOff="@string/pref_manage_notify_real_time_text_off"
android:summaryOn="@string/pref_manage_notify_real_time_summary_on"
android:summaryOff="@string/pref_manage_notify_real_time_summary_off" />

<PreferenceScreen android:key="manage_held_data"
android:title="@string/pref_manage_held_data" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_category_others">
Expand Down

0 comments on commit 737823f

Please sign in to comment.