基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器

中原工学院王舒宇
发布于 2022-12-17 18:40
浏览
0收藏

 界面仿照华为自带的备忘录完成。

功能介绍

1.基本的备忘录功能。

  标题、内容的存储。

  自动记录创建时间。

  删除、添加记录。

2.分布式数据库,可以在多个设备协同查看和编辑。

3.DDL报警

  DDL接近颜色高亮。开始-着急-迫近-死线。

  缺省设置为1/2开始着急1/4开始迫近,1/10为死线。

  DDL关键时间节点提醒。着急-迫近-死线。

4.关键词搜索功能

创建项目

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

登录华为账号 选择p50虚拟机

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

 配置“build.gradle”文件

    compileOptions {
        annotationEnabled true
    }
  • 1.
  • 2.
  • 3.

构造数据库,即创建数据库类并配置对应的属性

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);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.


界面跳转

ClickToEdit(x) {
        console.info("ClickToEdit(" + x.toString() + ")");
        router.push({
            uri: 'pages/edit/edit', 
            params: {
                index: this.todoList[x].index,
                content: this.todoList[x]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.


登录页面

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

主要代码

<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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

编辑界面

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

基于JS和Java依托harmonyOS提供的能力完成的一个DDL管理器-鸿蒙开发者社区

代码

<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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.


接口层

                } 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");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.







主要代码

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);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.

分类
标签
已于2022-12-17 18:48:36修改
2
收藏
回复
举报
2
回复
    相关推荐