Skip to content

Commit

Permalink
Add backup file cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Haleydu committed Aug 9, 2020
1 parent 0706bdb commit bed93f3
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/com/hiroshi/cimoc/core/Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public static Observable<String[]> loadSettings(DocumentFile root) {
return load(root, SUFFIX_CSBF);
}

public static Observable<String[]> loadClearBackup(DocumentFile root) {
return load(root, SUFFIX_CIMOC, SUFFIX_CFBF, SUFFIX_CTBF, SUFFIX_CSBF);
}

private static Observable<String[]> load(final DocumentFile root, final String... suffix) {
return Observable.create(new Observable.OnSubscribe<String[]>() {
@Override
Expand Down Expand Up @@ -268,6 +272,18 @@ public void call(Subscriber<? super List<Comic>> subscriber) {
}).subscribeOn(Schedulers.io());
}

public static Observable<Integer> clearBackup(final DocumentFile root) {
return Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
DocumentFile dir = DocumentUtils.getOrCreateSubDirectory(root, BACKUP);
if (dir != null) dir.delete();
subscriber.onNext(1);
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}

public static Observable<Map<String, ?>> restoreSetting(final ContentResolver resolver, final DocumentFile root, final String filename) {
return Observable.create(new Observable.OnSubscribe<Map<String, ?>>() {
@Override
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/hiroshi/cimoc/presenter/BackupPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public void call(Throwable throwable) {
}));
}

public void loadClearBackupFile() {
mCompositeSubscription.add(Backup.loadClearBackup(mBaseView.getAppInstance().getDocumentFile())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String[]>() {
@Override
public void call(String[] file) {
mBaseView.onClearFileLoadSuccess(file);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mBaseView.onFileLoadFail();
}
}));
}

public void saveComic() {
mCompositeSubscription.add(mComicManager.listFavoriteOrHistoryInRx()
.map(new Func1<List<Comic>, Integer>() {
Expand Down Expand Up @@ -237,6 +253,22 @@ public void call(Throwable throwable) {
}));
}

public void clearBackup() {
mCompositeSubscription.add(Backup.clearBackup(mBaseView.getAppInstance().getDocumentFile())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer pair) {
mBaseView.onClearBackupSuccess();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mBaseView.onClearBackupFail();
}
}));
}

private List<Tag> setTagsId(final List<Pair<Tag, List<Comic>>> list) {
final List<Tag> tags = new LinkedList<>();
mTagRefManager.runInTx(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.hiroshi.cimoc.presenter.BackupPresenter;
import com.hiroshi.cimoc.presenter.BasePresenter;
import com.hiroshi.cimoc.ui.fragment.dialog.ChoiceDialogFragment;
import com.hiroshi.cimoc.ui.fragment.dialog.MessageDialogFragment;
import com.hiroshi.cimoc.ui.view.BackupView;
import com.hiroshi.cimoc.ui.widget.preference.CheckBoxPreference;
import com.hiroshi.cimoc.utils.PermissionUtils;
Expand All @@ -25,6 +26,7 @@ public class BackupActivity extends BackActivity implements BackupView {
private static final int DIALOG_REQUEST_RESTORE_COMIC = 0;
private static final int DIALOG_REQUEST_RESTORE_TAG = 1;
private static final int DIALOG_REQUEST_RESTORE_SETTINGS = 2;
private static final int DIALOG_REQUEST_RESTORE_CLEAR = 3;

@BindView(R.id.backup_layout)
View mLayoutView;
Expand Down Expand Up @@ -103,6 +105,15 @@ void onRestoreTagClick() {
}
}

@OnClick(R.id.backup_clear_record) void onClearRecordClick() {
showProgressDialog();
if (PermissionUtils.hasStoragePermission(this)) {
mPresenter.loadClearBackupFile();
} else {
onFileLoadFail();
}
}

@Override
public void onDialogResult(int requestCode, Bundle bundle) {
switch (requestCode) {
Expand All @@ -118,6 +129,10 @@ public void onDialogResult(int requestCode, Bundle bundle) {
showProgressDialog();
mPresenter.restoreSetting(bundle.getString(EXTRA_DIALOG_RESULT_VALUE));
break;
case DIALOG_REQUEST_RESTORE_CLEAR:
showProgressDialog();
mPresenter.clearBackup();
break;
}
}

Expand All @@ -142,6 +157,14 @@ private void showChoiceDialog(int title, String[] item, int request) {
fragment.show(getFragmentManager(), null);
}

@Override
public void onClearFileLoadSuccess(String[] file) {
hideProgressDialog();
MessageDialogFragment fragment = MessageDialogFragment.newInstance(R.string.backup_clear_record,
R.string.backup_clear_record_notice_summary, false, DIALOG_REQUEST_RESTORE_CLEAR);
fragment.show(getFragmentManager(), null);
}

@Override
public void onFileLoadFail() {
hideProgressDialog();
Expand All @@ -154,6 +177,18 @@ public void onBackupRestoreSuccess() {
showSnackbar(R.string.common_execute_success);
}

@Override
public void onClearBackupSuccess() {
hideProgressDialog();
showSnackbar(R.string.common_execute_clear_success);
}

@Override
public void onClearBackupFail() {
hideProgressDialog();
showSnackbar(R.string.common_execute_clear_fail);
}

@Override
public void onBackupRestoreFail() {
hideProgressDialog();
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/hiroshi/cimoc/ui/view/BackupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public interface BackupView extends BaseView, DialogCaller {

void onSettingsFileLoadSuccess(String[] file);

void onClearFileLoadSuccess(String[] file);

void onFileLoadFail();

void onClearBackupSuccess();

void onClearBackupFail();

}
28 changes: 28 additions & 0 deletions app/src/main/res/layout/activity_backup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
app:title="@string/backup_save_comic" />

<com.hiroshi.cimoc.ui.widget.Option
android:visibility="gone"
android:id="@+id/backup_save_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -90,6 +91,7 @@
app:title="@string/backup_restore_comic" />

<com.hiroshi.cimoc.ui.widget.Option
android:visibility="gone"
android:id="@+id/backup_restore_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -103,6 +105,32 @@
app:summary="@string/backup_restore_settings_summary"/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="10dp"
android:text="@string/backup_clear"
android:textColor="?attr/colorPrimary"
android:textSize="14sp" />

<com.hiroshi.cimoc.ui.widget.Option
android:id="@+id/backup_clear_record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:summary="@string/backup_clear_record_summary"
app:title="@string/backup_clear_record" />

</LinearLayout>

<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
<string name="backup_restore_comic_summary">恢復收藏和歷史漫畫</string>
<string name="backup_restore_tag">恢復標籤</string>
<string name="backup_restore_tag_summary">恢復標籤及相應漫畫</string>
<string name="backup_restore_settings">恢复设置</string>
<string name="backup_restore_settings_summary">恢复所有个人配置</string>
<string name="backup_cloud">雲端</string>
<string name="backup_cloud_restore_tag">從雲端恢復</string>
<string name="backup_cloud_restore_tag_summary">從雲端恢復收藏的漫畫</string>
Expand All @@ -215,6 +217,10 @@
<string name="backup_cloud_backup_tag_summary">備份收藏漫畫到雲端</string>
<string name="backup_cloud_sync_tag">自動同步</string>
<string name="backup_cloud_sync_tag_summary">自動同步收藏到雲端</string>
<string name="backup_clear">清除</string>
<string name="backup_clear_record">清除备份记录</string>
<string name="backup_clear_record_summary">清除所有备份记录,清除后请尽快备份以免数据丢失</string>


<string name="home_page">首頁</string>
<string name="home_page_cimqus">Cimqus</string>
Expand Down Expand Up @@ -281,6 +287,8 @@
<string name="common_keyword_empty">關鍵字為空</string>
<string name="common_parse_error">解析錯誤</string>
<string name="common_execute_success">操作成功</string>
<string name="common_execute_clear_success">清理成功</string>
<string name="common_execute_clear_fail">清理失败</string>
<string name="common_execute_fail">操作失敗</string>
<string name="common_operation_select">操作選擇</string>
<string name="common_null">null</string>
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
<string name="backup_restore_comic_summary">恢复收藏和历史漫画</string>
<string name="backup_restore_tag">恢复标签</string>
<string name="backup_restore_tag_summary">恢复标签及相应漫画</string>
<string name="backup_restore_settings">恢复设置</string>
<string name="backup_restore_settings_summary">恢复所有个人配置</string>
<string name="backup_cloud">云端</string>
<string name="backup_cloud_restore_tag">从云端恢复</string>
<string name="backup_cloud_restore_tag_summary">从云端恢复收藏的漫画</string>
Expand All @@ -219,8 +221,10 @@
<string name="backup_cloud_backup_tag_summary">备份收藏漫画到云端</string>
<string name="backup_cloud_sync_tag">自动同步</string>
<string name="backup_cloud_sync_tag_summary">自动同步收藏到云</string>
<string name="backup_restore_settings">恢复设置</string>
<string name="backup_restore_settings_summary">恢复所有个人配置</string>
<string name="backup_clear">清除</string>
<string name="backup_clear_record">清除备份记录</string>
<string name="backup_clear_record_summary">清除收藏、历史、个人配置的所有备份记录</string>
<string name="backup_clear_record_notice_summary">清除后请尽快备份以免备份数据丢失,无法恢复备份内容</string>

<string name="home_page">主页</string>
<string name="home_page_url">https://github.com/Haleydu/Cimoc</string>
Expand Down Expand Up @@ -297,6 +301,8 @@
<string name="common_keyword_empty">关键字为空</string>
<string name="common_parse_error">解析错误</string>
<string name="common_execute_success">操作成功</string>
<string name="common_execute_clear_success">清理成功</string>
<string name="common_execute_clear_fail">清理失败</string>
<string name="common_execute_fail">操作失败</string>
<string name="common_operation_select">操作选择</string>
<string name="common_null">null</string>
Expand Down

0 comments on commit bed93f3

Please sign in to comment.