use of cn.snowt.diary.util.SimpleResult in project Diary by HibaraAi.
the class KeepDiaryActivity method onOptionsItemSelected.
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
{
if (!"".equals(diaryInputView.getText().toString())) {
askSave(diaryInputView.getText().toString());
} else {
finish();
}
break;
}
case R.id.toolbar_diary_send:
{
String diaryInputStr = diaryInputView.getText().toString();
if ("".equals(diaryInputStr)) {
BaseUtils.shortTipInSnack(diaryInputView, "空白日记有什么好记录的呢? OvO");
} else if (diaryInputStr.length() > 2000) {
BaseUtils.shortTipInSnack(diaryInputView, "你以为写书呢?大于2000字了,禁止保存 ORz");
} else {
Date date = null;
String dateStr = dateView.getText().toString();
if (!"".equals(dateStr)) {
date = BaseUtils.stringToDate(dateStr);
}
SimpleResult result;
if (-1 != updateDiaryId) {
Diary diary = new Diary();
diary.setContent(diaryInputStr);
diary.setId(updateDiaryId);
result = diaryService.updateDiaryContentById(diary);
} else {
result = diaryService.addOneByArgs(diaryInputStr, labelView.getText().toString(), locationView.getText().toString(), weatherView.getText().toString(), imageTempSrcList, date, videoTempSrcList, quoteDiaryId);
}
if (result.getSuccess()) {
clearTempPinInEdit();
// 又判断一次????吃饱了撑???
if (-1 != updateDiaryId) {
BaseUtils.shortTipInCoast(KeepDiaryActivity.this, "日记的文本内容已更新,请手动刷新!");
} else {
BaseUtils.shortTipInCoast(KeepDiaryActivity.this, "新日记已存储,请手动刷新!");
}
if (-1 != tempDiaryId) {
LitePal.delete(TempDiary.class, tempDiaryId);
}
finish();
} else {
BaseUtils.shortTipInSnack(diaryInputView, result.getMsg());
}
}
break;
}
default:
break;
}
return true;
}
use of cn.snowt.diary.util.SimpleResult in project Diary by HibaraAi.
the class DiaryAdapter method onCreateViewHolder.
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.diary_item, parent, false);
if (null == context) {
context = parent.getContext();
}
final ViewHolder viewHolder = new ViewHolder(view);
// 头像及用户名是每个item都一样,且不会有点击事件
if (null == MyConfiguration.getInstance().getHeadImg()) {
viewHolder.headImg.setImageResource(R.drawable.nav_icon);
} else {
Glide.with(viewHolder.diaryView).load(MyConfiguration.getInstance().getHeadImg()).into(viewHolder.headImg);
}
viewHolder.username.setText(MyConfiguration.getInstance().getUsername());
// 展开或收起评论区
viewHolder.comment.setOnClickListener(v -> {
if (viewHolder.visible) {
// 改为不可见
viewHolder.diaryView.findViewById(R.id.item_comment_area_parent).setVisibility(View.GONE);
viewHolder.visible = false;
} else {
viewHolder.diaryView.findViewById(R.id.item_comment_area_parent).setVisibility(View.VISIBLE);
viewHolder.visible = true;
}
});
// 特殊地,如果是日记详情页,自动打开评论区
if (parent.getId() == R.id.detail_recyclerview) {
viewHolder.diaryView.findViewById(R.id.item_comment_area_parent).setVisibility(View.VISIBLE);
viewHolder.visible = true;
}
// 读取输入的评论
viewHolder.submitCommentBtn.setOnClickListener(v -> {
String commentInputStr = viewHolder.commentInput.getText().toString();
if (!"".equals(commentInputStr)) {
SimpleResult result = commentService.addOneByArgs(commentInputStr, viewHolder.diaryId);
if (result.getSuccess()) {
BaseUtils.shortTipInSnack(viewHolder.diaryView, "评论成功,请手动刷新 OvO");
viewHolder.commentInput.setText("");
} else {
BaseUtils.longTipInSnack(viewHolder.diaryView, result.getMsg());
}
}
});
// 设置字体大小
float fontSize = MyConfiguration.getInstance().getFontSize();
if (fontSize != -1) {
viewHolder.content.setTextSize(fontSize);
}
// 长按日记文字
viewHolder.content.setOnLongClickListener(v -> {
AtomicReference<String> select = new AtomicReference<>();
final String[] items = { "复制日记", "置顶日记", "引用追更", "查看详情", "编辑日记", "删除" };
select.set(items[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("日记菜单");
builder.setSingleChoiceItems(items, 0, (dialogInterface, i) -> {
select.set(items[i]);
});
builder.setPositiveButton("确定", (dialog, which) -> {
switch(select.get()) {
case "引用追更":
{
Intent intent = new Intent(context, KeepDiaryActivity.class);
intent.putExtra(KeepDiaryActivity.OPEN_FROM_TYPE, KeepDiaryActivity.OPEN_FROM_QUOTE_ADD);
intent.putExtra("uuid", viewHolder.myUuid);
intent.putExtra("str", viewHolder.content.getText());
context.startActivity(intent);
break;
}
case "复制日记":
{
BaseUtils.copyInClipboard(context, viewHolder.content.getText().toString());
BaseUtils.shortTipInSnack(viewHolder.content, "日记已复制 OvO");
break;
}
case "删除":
{
String s = viewHolder.content.getText().toString().replaceAll("\n", "");
String tip = s.length() > 20 ? s.substring(0, 20) + "..." : s;
new AlertDialog.Builder(context).setTitle("确定要删除这条日记吗?").setMessage("\"" + tip + "\"").setPositiveButton("确认删除", (dialog1, which1) -> {
SimpleResult result = diaryService.deleteById(viewHolder.diaryId);
if (result.getSuccess()) {
BaseUtils.shortTipInSnack(viewHolder.diaryView, "删除成功,刷新后将正常展示 OvO");
} else {
BaseUtils.shortTipInSnack(viewHolder.diaryView, result.getMsg());
}
}).setNegativeButton("刚刚点错了", null).show();
break;
}
case "查看详情":
{
Intent intent = new Intent(context, DiaryDetailActivity.class);
intent.putExtra("id", viewHolder.diaryId);
context.startActivity(intent);
break;
}
case "编辑日记":
{
Intent intent = new Intent(context, KeepDiaryActivity.class);
intent.putExtra(KeepDiaryActivity.OPEN_FROM_TYPE, KeepDiaryActivity.OPEN_FROM_UPDATE_DIARY);
intent.putExtra("id", viewHolder.diaryId);
context.startActivity(intent);
break;
}
case "置顶日记":
{
new AlertDialog.Builder(context).setTitle("说明").setMessage("将此条日记置顶。(已有则覆盖)\n特殊地,如果对置顶日记本身执行此操作则视为取消置顶\n另外,置顶只是提前展示,你仍会在浏览时看到该日记真身。如果删除置顶日记,则视为真正删除该日记。").setPositiveButton("继续", (dialog1, which1) -> {
int topDiaryId = BaseUtils.getSharedPreference().getInt("topDiary", -1);
SharedPreferences.Editor edit = BaseUtils.getSharedPreference().edit();
if (viewHolder.diaryId == topDiaryId) {
// 就是这一条,取消置顶
edit.putInt("topDiary", -1);
} else {
// 设置为置顶
edit.putInt("topDiary", viewHolder.diaryId);
}
edit.apply();
BaseUtils.shortTipInSnack(viewHolder.diaryView, "已更新置顶日记,刷新后即生效。OvO");
}).setNegativeButton("取消", null).show();
break;
}
default:
break;
}
});
builder.setNegativeButton("取消", null);
builder.setCancelable(false);
builder.show();
return true;
});
// 点击标签
viewHolder.label.setOnClickListener(v -> {
String allLabel = viewHolder.label.getText().toString();
AtomicReference<String> selectLabel = new AtomicReference<>();
final String[] items = allLabel.split("##");
if (items.length > 1) {
items[0] = items[0] + "#";
items[items.length - 1] = "#" + items[items.length - 1];
}
if (items.length >= 3) {
for (int i = 1; i <= items.length - 2; i++) {
items[i] = "#" + items[i] + "#";
}
}
selectLabel.set(items[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("按标签查看日记");
builder.setTitle("请选择一个标签,将展示同标签的所有日记");
builder.setSingleChoiceItems(items, 0, (dialogInterface, i) -> selectLabel.set(items[i]));
builder.setPositiveButton("查看", (dialog, which) -> {
Intent intent = new Intent(context, DiaryListActivity.class);
intent.putExtra(DiaryListActivity.OPEN_FROM_TYPE, DiaryListActivity.OPEN_FROM_SEARCH_LABEL);
intent.putExtra("label", selectLabel.get());
context.startActivity(intent);
});
builder.setNegativeButton("取消", null);
builder.setCancelable(false);
builder.show();
});
viewHolder.label.setOnLongClickListener(v -> {
String allLabel = viewHolder.label.getText().toString();
BaseUtils.copyInClipboard(context, allLabel);
BaseUtils.shortTipInCoast(context, "已复制: " + allLabel);
return true;
});
// 长按引用日记
viewHolder.quoteDiaryArea.setOnLongClickListener(v -> {
Intent intent = new Intent(context, DiaryDetailActivity.class);
intent.putExtra("uuid", viewHolder.quoteDiaryUuid);
context.startActivity(intent);
return true;
});
return viewHolder;
}
use of cn.snowt.diary.util.SimpleResult in project Diary by HibaraAi.
the class DiaryServiceImpl method updateDiaryContentById.
@Override
public SimpleResult updateDiaryContentById(Diary diary) {
// 此方法目前仅用于更新日记文本,修改错别字,仅此而已
SimpleResult result = new SimpleResult();
Diary diaryInDb = LitePal.find(Diary.class, diary.getId());
if (null == diaryInDb) {
result.setSuccess(false);
result.setMsg("没有找到需要更新的日记");
} else {
if (MyConfiguration.getInstance().isRequiredAndAbleToEncode()) {
diaryInDb.setContent(RSAUtils.encode(diary.getContent(), MyConfiguration.getInstance().getPublicKey()));
} else {
diaryInDb.setContent(diary.getContent());
}
int update = diaryInDb.update(diaryInDb.getId());
if (1 == update) {
result.setSuccess(true);
result.setMsg("更新成功,请刷新");
} else {
result.setSuccess(false);
result.setMsg("数据库更新失败,请重试");
}
}
return result;
}
use of cn.snowt.diary.util.SimpleResult in project Diary by HibaraAi.
the class DiaryServiceImpl method backupDiary.
@Override
public SimpleResult backupDiary(String publicKey, String pinKey) {
SimpleResult result = new SimpleResult();
// List<Diary> diaryList = LitePal.findAll(Diary.class);
// 按时间排序是希望恢复日记时能按照日记顺序恢复,方便操作日记引用
List<Diary> diaryList = LitePal.order("modifiedDate asc").find(Diary.class);
if (diaryList.isEmpty()) {
result.setSuccess(false);
result.setMsg("没有查询到有日记,备份啥呢?");
} else {
// 1.读取日记
List<DiaryVoForBackup> vos = new ArrayList<>(diaryList.size());
diaryList.forEach(diary -> {
DiaryVoForBackup vo = new DiaryVoForBackup();
vo.setEncryption(diary.getEncryption());
vo.setContent(diary.getContent());
vo.setLabelStr(diary.getLabel());
vo.setModifiedDate(diary.getModifiedDate());
if (null != diary.getLocationId()) {
vo.setLocation(LitePal.find(Location.class, diary.getLocationId()));
}
if (null != diary.getWeatherId()) {
vo.setWeather(LitePal.find(Weather.class, diary.getWeatherId()));
}
vo.setCommentList(LitePal.where("diaryId = ?", diary.getId() + "").find(Comment.class));
vo.setDrawingList(LitePal.where("diaryId = ?", diary.getId() + "").find(Drawing.class));
vo.setVideoList(LitePal.where("diaryId = ?", diary.getId() + "").find(Video.class));
vo.setQuoteDiaryUuid(diary.getQuoteDiaryUuid());
vo.setMyUuid(diary.getMyUuid());
vos.add(vo);
});
// 读取纪念日
SpecialDayService specialDayService = new SpecialDayServiceImpl();
List<SpecialDay> specialDays = specialDayService.getAll();
// 读取同名标签设置
String sameLabel = BaseUtils.getSharedPreference().getString("sameLabel", "");
// 2.封装数据
Map<String, Object> map = new HashMap<>();
String uuid = UUID.randomUUID().toString();
map.put(Constant.BACKUP_ARGS_NAME_UUID, uuid);
map.put(Constant.BACKUP_ARGS_NAME_DATA_NAME, vos);
map.put(Constant.BACKUP_ARGS_NAME_PUBLIC_KEY, publicKey);
map.put(Constant.BACKUP_ARGS_NAME_PIN_KEY, MD5Utils.encrypt(Constant.PASSWORD_PREFIX + pinKey));
map.put(Constant.BACKUP_ARGS_NAME_VERSION, Constant.INTERNAL_VERSION);
map.put(Constant.BACKUP_ARGS_NAME_ENCODE_UUID, MD5Utils.encrypt(Constant.PASSWORD_PREFIX + uuid));
map.put("SpecialDay", specialDays);
map.put("sameLabel", sameLabel);
String mapJson = JSON.toJSONString(map);
// 3.输出文件
String fileName = "XiaoXiaoLe_Backup_" + BaseUtils.dateToString(new Date()) + ".dll";
boolean saveFlag = FileUtils.saveAsFileWriter(mapJson, fileName);
if (saveFlag) {
String absolutePath = Environment.getExternalStoragePublicDirectory(Constant.EXTERNAL_STORAGE_LOCATION + "output/").getAbsolutePath();
result.setMsg("备份文件保存至[" + absolutePath + "/" + fileName + "],建议马上移动文件到其他文件夹,用完之后应及时删除.");
result.setSuccess(true);
} else {
result.setSuccess(false);
result.setMsg("备份文件保存失败,请重试");
}
}
return result;
}
use of cn.snowt.diary.util.SimpleResult in project Diary by HibaraAi.
the class SetSameLabelActivity method showAlreadyAddSameLabel.
/**
* 展示已经添加的同名标签
*/
private void showAlreadyAddSameLabel() {
Map<Integer, String> sameLabel = labelService.getAllSameLabel();
if (sameLabel.isEmpty()) {
return;
}
// 将map转成list展示
List<String> list = new ArrayList<>(sameLabel.size());
sameLabel.forEach((integer, s) -> list.add(integer + ": " + s));
Collections.reverse(list);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
ListView listView = findViewById(R.id.label_list);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener((parent, view, position, id) -> {
// 解析id和原标签
TextView view1 = (TextView) view;
String[] split = view1.getText().toString().split(": ");
System.out.println("id:" + split[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("修改");
builder.setMessage("修改同名标签,空输入视为删除");
EditText editText = new EditText(this);
editText.setHint("#AA##BB#");
editText.setText(split[1]);
builder.setView(editText);
builder.setPositiveButton("修改", (dialog, which) -> {
String s = editText.getText().toString();
SimpleResult result = labelService.updateSameLabel(Integer.valueOf(split[0]), s);
if (!result.getSuccess()) {
BaseUtils.alertDialogToShow(this, "修改失败", result.getMsg());
} else {
BaseUtils.alertDialogToShow(this, "修改成功", result.getMsg());
showAlreadyAddSameLabel();
}
});
builder.setNegativeButton("取消", null);
builder.show();
return true;
});
}
Aggregations