|
|
package net.micode.notes.model;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 模板管理类,用于管理笔记模板的存储、加载、添加、删除等操作
|
|
|
*/
|
|
|
public class TemplateManager {
|
|
|
private static final String PREF_TEMPLATES = "note_templates";
|
|
|
private static final String PREF_TEMPLATE_ID_COUNTER = "template_id_counter";
|
|
|
private static TemplateManager sInstance;
|
|
|
private Context mContext;
|
|
|
private List<NoteTemplate> mTemplates;
|
|
|
|
|
|
private TemplateManager(Context context) {
|
|
|
mContext = context.getApplicationContext();
|
|
|
loadTemplates();
|
|
|
}
|
|
|
|
|
|
public static synchronized TemplateManager getInstance(Context context) {
|
|
|
if (sInstance == null) {
|
|
|
sInstance = new TemplateManager(context);
|
|
|
}
|
|
|
return sInstance;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载所有模板,包括系统模板和自定义模板
|
|
|
*/
|
|
|
private void loadTemplates() {
|
|
|
mTemplates = new ArrayList<>();
|
|
|
|
|
|
// 先添加系统模板
|
|
|
addSystemTemplates();
|
|
|
|
|
|
// 然后加载自定义模板
|
|
|
loadCustomTemplates();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加系统模板
|
|
|
*/
|
|
|
private void addSystemTemplates() {
|
|
|
// 会议记录模板
|
|
|
NoteTemplate meetingTemplate = new NoteTemplate();
|
|
|
meetingTemplate.setId(1);
|
|
|
meetingTemplate.setName("会议记录");
|
|
|
meetingTemplate.setContent("<strong>会议记录</strong><br><br>" +
|
|
|
"<strong>会议主题</strong>:<br>" +
|
|
|
"<strong>会议时间</strong>:<br>" +
|
|
|
"<strong>会议地点</strong>:<br>" +
|
|
|
"<strong>参会人员</strong>:<br>" +
|
|
|
"<strong>主持人</strong>:<br><br>" +
|
|
|
"<strong>会议议程</strong><br>" +
|
|
|
"1. <br>" +
|
|
|
"2. <br>" +
|
|
|
"3. <br><br>" +
|
|
|
"<strong>会议内容</strong><br><br>" +
|
|
|
"<strong>决议事项</strong><br>" +
|
|
|
"1. <br>" +
|
|
|
"2. <br>" +
|
|
|
"3. <br><br>" +
|
|
|
"<strong>行动项</strong><br>" +
|
|
|
"<strong>任务</strong>:<strong>负责人</strong>:<strong>截止日期</strong>:<strong>状态</strong>:<br><br>" +
|
|
|
"<br><br>" +
|
|
|
"<strong>下次会议</strong><br>" +
|
|
|
"<strong>时间</strong>:<br>" +
|
|
|
"<strong>主题</strong>:<br>");
|
|
|
meetingTemplate.setType(NoteTemplate.TYPE_SYSTEM);
|
|
|
mTemplates.add(meetingTemplate);
|
|
|
|
|
|
// 待办事项模板
|
|
|
NoteTemplate todoTemplate = new NoteTemplate();
|
|
|
todoTemplate.setId(2);
|
|
|
todoTemplate.setName("待办事项");
|
|
|
todoTemplate.setContent("<strong>待办事项</strong><br><br>" +
|
|
|
"<strong>日期</strong>:<br><br>" +
|
|
|
"<strong>今日待办</strong><br>" +
|
|
|
"☐ <br>" +
|
|
|
"☐ <br>" +
|
|
|
"☐ <br>" +
|
|
|
"☐ <br>" +
|
|
|
"☐ <br><br>" +
|
|
|
"<strong>重要事项</strong><br>" +
|
|
|
"⭐ <br>" +
|
|
|
"⭐ <br><br>" +
|
|
|
"<strong>已完成</strong><br>" +
|
|
|
"✓ <br>" +
|
|
|
"✓ <br>" +
|
|
|
"✓ <br>");
|
|
|
todoTemplate.setType(NoteTemplate.TYPE_SYSTEM);
|
|
|
mTemplates.add(todoTemplate);
|
|
|
|
|
|
// 购物清单模板
|
|
|
NoteTemplate shoppingTemplate = new NoteTemplate();
|
|
|
shoppingTemplate.setId(3);
|
|
|
shoppingTemplate.setName("购物清单");
|
|
|
shoppingTemplate.setContent("<strong>购物清单</strong><br><br>" +
|
|
|
"<strong>日期</strong>:<br>" +
|
|
|
"<strong>商店</strong>:<br>" +
|
|
|
"<strong>预算</strong>:<br><br>" +
|
|
|
"<strong>清单</strong><br>" +
|
|
|
"<strong>物品</strong>:<strong>数量</strong>:<strong>单价</strong>:<strong>备注</strong>:<strong>状态</strong>:<br>" +
|
|
|
" ☐ <br>" +
|
|
|
" ☐ <br>" +
|
|
|
" ☐ <br>" +
|
|
|
" ☐ <br>" +
|
|
|
" ☐ <br><br>" +
|
|
|
"<strong>总计</strong><br>" +
|
|
|
"<strong>实际花费</strong>:<br>" +
|
|
|
"<strong>节省/超支</strong>:<br>");
|
|
|
shoppingTemplate.setType(NoteTemplate.TYPE_SYSTEM);
|
|
|
mTemplates.add(shoppingTemplate);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载自定义模板
|
|
|
*/
|
|
|
private void loadCustomTemplates() {
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
|
|
String templatesJson = prefs.getString(PREF_TEMPLATES, "[]");
|
|
|
|
|
|
try {
|
|
|
JSONArray jsonArray = new JSONArray(templatesJson);
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
JSONObject json = jsonArray.getJSONObject(i);
|
|
|
NoteTemplate template = NoteTemplate.fromJson(json);
|
|
|
mTemplates.add(template);
|
|
|
}
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存自定义模板到SharedPreferences
|
|
|
*/
|
|
|
private void saveCustomTemplates() {
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (NoteTemplate template : mTemplates) {
|
|
|
if (NoteTemplate.TYPE_CUSTOM.equals(template.getType())) {
|
|
|
try {
|
|
|
jsonArray.put(template.toJson());
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
editor.putString(PREF_TEMPLATES, jsonArray.toString());
|
|
|
editor.apply();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所有模板
|
|
|
*/
|
|
|
public List<NoteTemplate> getAllTemplates() {
|
|
|
return new ArrayList<>(mTemplates);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取系统模板
|
|
|
*/
|
|
|
public List<NoteTemplate> getSystemTemplates() {
|
|
|
List<NoteTemplate> systemTemplates = new ArrayList<>();
|
|
|
for (NoteTemplate template : mTemplates) {
|
|
|
if (NoteTemplate.TYPE_SYSTEM.equals(template.getType())) {
|
|
|
systemTemplates.add(template);
|
|
|
}
|
|
|
}
|
|
|
return systemTemplates;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取自定义模板
|
|
|
*/
|
|
|
public List<NoteTemplate> getCustomTemplates() {
|
|
|
List<NoteTemplate> customTemplates = new ArrayList<>();
|
|
|
for (NoteTemplate template : mTemplates) {
|
|
|
if (NoteTemplate.TYPE_CUSTOM.equals(template.getType())) {
|
|
|
customTemplates.add(template);
|
|
|
}
|
|
|
}
|
|
|
return customTemplates;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据ID获取模板
|
|
|
*/
|
|
|
public NoteTemplate getTemplateById(long id) {
|
|
|
for (NoteTemplate template : mTemplates) {
|
|
|
if (template.getId() == id) {
|
|
|
return template;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加自定义模板
|
|
|
*/
|
|
|
public void addCustomTemplate(NoteTemplate template) {
|
|
|
// 生成唯一ID
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
|
|
long id = prefs.getLong(PREF_TEMPLATE_ID_COUNTER, 1000);
|
|
|
|
|
|
template.setId(id);
|
|
|
template.setType(NoteTemplate.TYPE_CUSTOM);
|
|
|
template.setCreateTime(System.currentTimeMillis());
|
|
|
template.setUpdateTime(System.currentTimeMillis());
|
|
|
|
|
|
mTemplates.add(template);
|
|
|
|
|
|
// 更新ID计数器
|
|
|
prefs.edit().putLong(PREF_TEMPLATE_ID_COUNTER, id + 1).apply();
|
|
|
|
|
|
// 保存自定义模板
|
|
|
saveCustomTemplates();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除自定义模板
|
|
|
*/
|
|
|
public boolean deleteCustomTemplate(long id) {
|
|
|
NoteTemplate templateToRemove = null;
|
|
|
for (NoteTemplate template : mTemplates) {
|
|
|
if (template.getId() == id && NoteTemplate.TYPE_CUSTOM.equals(template.getType())) {
|
|
|
templateToRemove = template;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (templateToRemove != null) {
|
|
|
mTemplates.remove(templateToRemove);
|
|
|
saveCustomTemplates();
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新自定义模板
|
|
|
*/
|
|
|
public boolean updateCustomTemplate(NoteTemplate updatedTemplate) {
|
|
|
for (int i = 0; i < mTemplates.size(); i++) {
|
|
|
NoteTemplate template = mTemplates.get(i);
|
|
|
if (template.getId() == updatedTemplate.getId() &&
|
|
|
NoteTemplate.TYPE_CUSTOM.equals(template.getType())) {
|
|
|
|
|
|
updatedTemplate.setUpdateTime(System.currentTimeMillis());
|
|
|
mTemplates.set(i, updatedTemplate);
|
|
|
saveCustomTemplates();
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
}
|