回复
基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器
中原工学院王舒宇
发布于 2022-12-17 18:40
浏览
0收藏
界面仿照华为自带的备忘录完成。
功能介绍
1.基本的备忘录功能。
标题、内容的存储。
自动记录创建时间。
删除、添加记录。
2.分布式数据库,可以在多个设备协同查看和编辑。
3.DDL报警
DDL接近颜色高亮。开始-着急-迫近-死线。
缺省设置为1/2开始着急1/4开始迫近,1/10为死线。
DDL关键时间节点提醒。着急-迫近-死线。
4.关键词搜索功能
创建项目
登录华为账号 选择p50虚拟机
配置“build.gradle”文件
compileOptions {
annotationEnabled true
}
构造数据库,即创建数据库类并配置对应的属性
TodoServiceAbility.register(this);
KvManagerConfig config = new KvManagerConfig(context);
kvManager = KvManagerFactory.getInstance().createKvManager(config);
Options CREATE = new Options();
CREATE.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
singleKvStore = kvManager.getKvStore(CREATE, storeID);
KvStoreObserver kvStoreObserverClient = new KvStoreObserverClient();
singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, kvStoreObserverClient);
界面跳转
ClickToEdit(x) {
console.info("ClickToEdit(" + x.toString() + ")");
router.push({
uri: 'pages/edit/edit',
params: {
index: this.todoList[x].index,
content: this.todoList[x]
登录页面
主要代码
<div class="container">
<div class="title-box">
<text class="title" onclick="onShow"> {{ title }}</text>
<text class="number"> {{ number }} </text>
</div>
<div class="search-box">
<search onchange="getTodos()" onblur="getTodos()">
</search>
</div>
<div class="text-box">
<list>
<list-item for="{{todoList}}" class="todo-item" onlongpress="LongPressToChoose" onclick="ClickToEdit($idx)">
<div style="width: 10px;">
<button if="{{$item.color==3}}" style="height: 100%; width: 100%; background-color: red;"></button>
<button if="{{$item.color==2}}" style="height: 100%; width: 100%; background-color: indianred;"></button>
<button if="{{$item.color==1}}" style="height: 100%; width: 100%; background-color: blue;"></button>
<button if="{{$item.color==0}}" style="height: 100%; width: 100%; background-color: skyblue;"></button>
</div>
<div class="div-item">
<text class="title-text" > {{$item.title}} </text>
<text if="{{$item.ddl}}" class="date-text" > DDL:{{$item.ddl}} </text>
<text else class="date-text"> {{$item.date}} </text>
<div class="delete-choose">
<input type="checkbox" value="{{$idx}}" disabled="{{!choose_delete}}" show="{{choose_delete}}" onchange="ChooseToDelete($idx)">
</input>
</div>
</div>
</list-item>
</list>
</div>
<button class="addButton" type="circle" onclick="AddOrDelete">
{{add_delete}}
</button>
</div>
编辑界面
代码
<div class="container">
<div class="nav-bar">
<button style="right: 0px;" onclick="ClickToBack"> {{ BACK }} </button>
<button style="left: 0px;" onclick="InsertTodos"> {{ SAVE }}</button>
</div>
<div class="content">
<textarea class="title" onchange="PassTitle">
{{title}}
</textarea>
<textarea class="text" onchange="PassText">
{{text}}
</textarea>
</div>
<button class="addButton" type="circle" onclick="AddDdl">
+
</button>
<dialog id="ddl_dialog" class="dialog-main">
<div class="dialog-div">
<div class="inner-txt">
<text class="txt">{{ddl_text}}</text>
<picker type="date" value="{{ddl}}" onchange="PassDdl"> </picker>
</div>
<div class="inner-txt">
<text class="txt" >{{ddl1_text}}</text>
<slider class="slide" value="{{ddls[0]}}" onchange="PassDdls(0)"></slider>
</div>
<div class="inner-txt">
<text class="txt">{{ddl2_text}}</text>
<slider class="slide" value="{{ddls[1]}}" onchange="PassDdls(1)"></slider>
</div>
<div class="inner-txt">
<text class="txt">{{ddl3_text}}</text>
<slider class="slide" value="{{ddls[2]}}" onchange="PassDdls(2)"></slider>
</div>
<div class="inner-btn">
<button type="capsule" value="{{ CANCEL }}" onclick="CancelDdl" class="btn-txt"></button>
<button type="capsule" value="{{ CONFIRM }}" onclick="SaveDdl" class="btn-txt"></button>
</div>
</div>
</dialog>
</div>
接口层
} catch (RuntimeException e) {
HiLog.error(LABEL, "convert failed.");
}
if (param.title!=null) db.putString(param.id+" title",param.title);
if (param.date!=null) db.putString(param.id+" date",param.date);
if (param.text!=null) db.putString(param.id+" text",param.text);
if (param.ddl!=null) db.putString(param.id+" ddl",param.ddl);
HiLog.debug(LABEL, "insert or update success");
break;
}
case SELECT_TODOS:{
// 返回结果当前仅支持String,对于复杂结构可以序列化为ZSON字符串上报
HiLog.debug(LABEL, "Database Return All Result");
String condition = data.readString();
HiLog.debug(LABEL, "condition="+condition);
Set<String> indexes = new HashSet<>();
for (Entry i: db.getEntries("")){
indexes.add(i.getKey().split(" ")[0]);
}
for (String i: indexes){
String title,date,ddl;
try {
title = db.getString(i+" title");
if (title.contains(condition))
result.put("title",title);
else
continue;
}
catch (KvStoreException k){
title = "";
};
try {
date = db.getString(i+" date");
result.put("date",date);
}
catch (KvStoreException k){
date = "";
};
try {
ddl = db.getString(i+" ddl");
result.put("ddl",ddl);
}
catch (KvStoreException k){
ddl = "";
};
// HiLog.debug(LABEL, String.valueOf(result));
String rString = ZSONObject.toZSONString(result);
ret.put(i,rString);
}
reply.writeString(ZSONObject.toZSONString(ret));
HiLog.debug(LABEL, "select all success");
break;
}
case SELECT_TODO: {
String dataStr = data.readString();
TodoRequestParam param = new TodoRequestParam();
try {
param = ZSONObject.stringToClass(dataStr, TodoRequestParam.class);
} catch (RuntimeException e) {
HiLog.error(LABEL, "convert failed.");
}
String r;
try {
r = db.getString(param.id+" text");
} catch (KvStoreException k) {
r = "";
}
reply.writeString(r);
HiLog.debug(LABEL, "select one text success");
break;
}
default: {
HiLog.debug(LABEL, "unknown code");
return false;
}
};
// 分布式数据库的同步
abilityContext.syncContact();
return true;
}
/**
* Internal ability 注册接口
*/
public static void register(MainAbility abilityContext) {
instance = new TodoServiceAbility();
instance.onRegister(abilityContext);
}
private void onRegister(MainAbility abilityContext) {
this.abilityContext = abilityContext;
this.setInternalAbilityHandler(this::onRemoteRequest);
HiLog.info(LABEL, "jgq TodoServiceAbility onRegister");
}
/**
* Internal ability 注销接口。
*/
public static void unregister() {
instance.onUnregister();
}
private void onUnregister() {
abilityContext = null;
this.setInternalAbilityHandler(null);
HiLog.info(LABEL, "jgq TodoServiceAbility onUnregister");
}
}
主要代码
public class MainAbility extends AceAbility implements IAbilityContinuation {
// static public Preferences preferences;
static public String storeID = "TodoDistributeDatabase";
private SingleKvStore singleKvStore;
private KvManager kvManager;
private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "MainAbility");
@Override
public void onStart(Intent intent) {
super.onStart(intent);
HiLog.info(LABEL,"jgq onStart");
// 开发者显示声明需要使用的权限
requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"}, 0);
Context context = this.getContext();
// DatabaseHelper databaseHelper = new DatabaseHelper(context);
// String filename = "todoDB";
// preferences = databaseHelper.getPreferences(filename);
// 需要注册
TodoServiceAbility.register(this);
// 分布式数据库,不知道是不是写在这里,注册
KvManagerConfig config = new KvManagerConfig(context);
kvManager = KvManagerFactory.getInstance().createKvManager(config);
// 创建数据库
Options CREATE = new Options();
CREATE.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
singleKvStore = kvManager.getKvStore(CREATE, storeID);
// 订阅
KvStoreObserver kvStoreObserverClient = new KvStoreObserverClient();
singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, kvStoreObserverClient);
}
public SingleKvStore getSingleKvStore() {
return singleKvStore;
}
@Override
public void onStop() {
// 取消注册
HiLog.info(LABEL,"jgq onStop");
TodoServiceAbility.unregister();
super.onStop();
}
public void syncContact() {
List<DeviceInfo> deviceInfoList = kvManager.getConnectedDevicesInfo(DeviceFilterStrategy.NO_FILTER);
List<String> deviceIdList = new ArrayList<>();
for (DeviceInfo deviceInfo : deviceInfoList) {
deviceIdList.add(deviceInfo.getId());
}
if (deviceIdList.size() == 0) {
// showTip("组网失败");
HiLog.info(LABEL,"同步失败");
return;
}
singleKvStore.registerSyncCallback(new SyncCallback() {
@Override
public void syncCompleted(Map<String, Integer> map) {
getUITaskDispatcher().asyncDispatch(new Runnable() {
@Override
public void run() {
// queryContact();
// showTip("同步成功");
HiLog.info(LABEL,"同步成功");
}
});
singleKvStore.unRegisterSyncCallback();
}
});
singleKvStore.sync(deviceIdList, SyncMode.PUSH_PULL);
}
}
分类
标签
已于2022-12-17 18:48:36修改
赞
2
收藏
回复
相关推荐