Browse Source

feat: 🚪 关门

master
niushuai233 1 year ago
parent
commit
db59b77be2
  1. 3
      app/src/main/java/cc/niushuai/dididone/biz/dao/ProjectDao.java
  2. 51
      app/src/main/java/cc/niushuai/dididone/ui/setting/SettingFragment.java
  3. 9
      app/src/main/java/cc/niushuai/dididone/util/GsonUtil.java

3
app/src/main/java/cc/niushuai/dididone/biz/dao/ProjectDao.java

@ -23,6 +23,9 @@ public interface ProjectDao { @@ -23,6 +23,9 @@ public interface ProjectDao {
@Query("SELECT count(*) FROM t_project where deleted = 0 and name = :name")
Flowable<Integer> countByName(String name);
@Insert
Completable insertAll(List<Project> projects);
@Insert
Completable insertAll(Project... projects);

51
app/src/main/java/cc/niushuai/dididone/ui/setting/SettingFragment.java

@ -15,6 +15,8 @@ import androidx.annotation.Nullable; @@ -15,6 +15,8 @@ import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
@ -188,6 +190,11 @@ public class SettingFragment extends BaseFragment { @@ -188,6 +190,11 @@ public class SettingFragment extends BaseFragment {
InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
String readStr = IoUtil.read(inputStream, StandardCharsets.UTF_8);
if (!GsonUtil.isJson(readStr)) {
XLog.d("非json: {}", readStr);
Toasts.shortShow(getContext(), "文件内容非JSON格式");
return;
}
List<Map<String, Object>> list = GsonUtil.toBean(readStr, List.class);
if (CollUtil.isEmpty(list)) {
@ -213,6 +220,16 @@ public class SettingFragment extends BaseFragment { @@ -213,6 +220,16 @@ public class SettingFragment extends BaseFragment {
private void restoreProjectRecord(List<ProjectRecord> projectRecordList) {
if (CollUtil.isNotEmpty(projectRecordList)) {
insertProject(projectRecordList);
insertRecord(projectRecordList);
}
}
private void insertRecord(List<ProjectRecord> projectRecordList) {
for (ProjectRecord projectRecord : projectRecordList) {
List<Record> recordList = projectRecord.getRecordList();
@ -224,51 +241,59 @@ public class SettingFragment extends BaseFragment { @@ -224,51 +241,59 @@ public class SettingFragment extends BaseFragment {
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
XLog.d("restore onSubscribe");
XLog.d("restore insertRecord onSubscribe");
}
@Override
public void onComplete() {
XLog.d("restore onComplete");
XLog.d("restore insertRecord onComplete");
BizGlobal.buildCache();
Toasts.shortShow(getContext(), "恢复打卡记录成功");
}
@Override
public void onError(Throwable e) {
XLog.d("restore onError: {}", e.getMessage(), e);
XLog.d("restore insertRecord onError: {}", e.getMessage(), e);
Toasts.shortShow(getContext(),"恢复打卡记录出错: {}", e.getMessage());
}
});
}
// 插入project
}
}
private void insertProject(List<ProjectRecord> projectRecordList) {
List<Project> projectList = projectRecordList.stream().map(item -> {
Project project = new Project();
BeanUtil.copyProperties(projectRecord, project);
Completable completable = DBManager.INSTANCE.projectDao().insertAll(project);
BeanUtil.copyProperties(item, project);
return project;
}).collect(Collectors.toList());
// 插入project
Completable completable = DBManager.INSTANCE.projectDao().insertAll(projectList);
completable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
XLog.d("restore insertProject onComplete");
}
@Override
public void onComplete() {
XLog.d("restore insertProject onComplete");
BizGlobal.buildCache();
Toasts.shortShow(getContext(), "恢复打卡项成功");
}
@Override
public void onError(Throwable e) {
XLog.d("restore insertProject onError: {}", e.getMessage(), e);
Toasts.shortShow(getContext(),"恢复打卡项出错: {}", e.getMessage());
}
});
}
}
}
private void backupResult(@NonNull Intent data) {
Uri fileUri = data.getData();

9
app/src/main/java/cc/niushuai/dididone/util/GsonUtil.java

@ -2,6 +2,7 @@ package cc.niushuai.dididone.util; @@ -2,6 +2,7 @@ package cc.niushuai.dididone.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
@ -113,4 +114,12 @@ public class GsonUtil { @@ -113,4 +114,12 @@ public class GsonUtil {
}.getType());
}
public static boolean isJson(String json) {
try {
JsonParser.parseString(json);
return true;
}catch (Exception e) {
return false;
}
}
}
Loading…
Cancel
Save