From efca73bc412d6ad7c0e69f8755864842d0446c7e Mon Sep 17 00:00:00 2001 From: genglingxia Date: Wed, 8 Mar 2023 19:04:03 +0800 Subject: [PATCH 1/4] manual demo fun add --- .../main/ets/Application/MyAbilityStage.ts | 2 +- .../src/main/ets/MainAbility/MainAbility.ts | 339 ++++++++++++++---- .../entry/src/main/ets/pages/index/index.ets | 314 ++++++++++------ 3 files changed, 480 insertions(+), 175 deletions(-) diff --git a/kvdb_stage_demo/entry/src/main/ets/Application/MyAbilityStage.ts b/kvdb_stage_demo/entry/src/main/ets/Application/MyAbilityStage.ts index 7334fe6..b2e09bd 100644 --- a/kvdb_stage_demo/entry/src/main/ets/Application/MyAbilityStage.ts +++ b/kvdb_stage_demo/entry/src/main/ets/Application/MyAbilityStage.ts @@ -1,4 +1,4 @@ -import AbilityStage from "@ohos.application.AbilityStage" +import AbilityStage from '@ohos.app.ability.UIAbility'; export default class MyAbilityStage extends AbilityStage { onCreate() { diff --git a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts index f57e1b9..7fe8c6e 100644 --- a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts @@ -1,31 +1,51 @@ -import Ability from '@ohos.application.Ability'; +import Ability from '@ohos.app.ability.UIAbility'; import deviceManager from '@ohos.distributedHardware.deviceManager'; import distributedData from '@ohos.data.distributedKVStore'; import accessControl from '@ohos.abilityAccessCtrl'; import bundle from '@ohos.bundle'; +import dataSharePredicates from '@ohos.data.dataSharePredicates'; + //import {AreaMode} from "application/Context" +var nameList = ["Leila", "Ursa", "Crystal", "Rebecca", "Taylor", "Elizabeth", "Ayn", "Rosa", "Beatrice", "Tracy", + "Emily", "Glenn", "Iris", "Sara", "Freddie", "Cindy", "Devin", "Karen", "Zillah", "Ivy", "Xena", "Aimee", "Olivia", + "Elisabeth", "Ulrica", "Chris", "Leslie", "Felicity", "Alessandra", "Cherie", "Sonia", "Elsa", "Natasha", "Candy", + "Kim", "Darleen", "Lillian", "Abigail", "Del", "Serena", "Candice", "Gemma", "Irene", "Rita", "Genevieve", "Nora", + "Ivory", "Niki", "Jacky", "Betty", "Isabelle", "Ray", "Patty", "Cathy", "Tanya", "Alison", "Rachel", "Julia", + "Julie", "Yasmine", "Susan", "Odin", "Emma", "Jessie", "Zoie", "Paula", "Carrie", "Harriet", "Christian", "Hana", + "Charlotte", "Terry", "Leah", "Ximena", "Frederica", "Wing", "Qara", "Elaine", "Silvia", "Ella", "Francesca", + "Janet", "Phoebe", "Holly", "Xochitl", "Urania", "Qatar", "Yolanda", "Athena", "Georgia", "Beth", "Barbie", + "Della", "Isabella", "Wendi", "Ophelia", "Hanna", "Sarah", "Hayden", "Teresa"]; + +function random(min, max) { + return Math.floor(Math.random() * (max - min)) + min; +} + export default class MainAbility extends Ability { devManager; deviceList; kvManager; kvStore; - syncMode; logText = ''; storeId = "device_sync_storeId_10"; appId = "com.example.kvdb_stage_demo"; syncListenerStarted = false; dataListenerStarted = false; - subscribeType = 1; // Զ̶ localDeviceId; - demoKvStoreType = 0; onCreate(want,launchParam){ // Ability is creating, initialize resources for this ability console.info("[Demo] MainAbility onCreate") globalThis.abilityWant = want; this.logText = "--------------Stage module----------------\r\n\n" + globalThis.encrypt = false; + globalThis.autoSync = false; + globalThis.kvStoreType = distributedData.KVStoreType.DEVICE_COLLABORATION; + globalThis.securityLevel = distributedData.SecurityLevel.S1; + globalThis.subscribeType = distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; + globalThis.syncMode = distributedData.SyncMode.PUSH_PULL; + globalThis.needDelLst = []; } onDestroy() { @@ -35,6 +55,7 @@ export default class MainAbility extends Ability { globalThis.DeleteKvStore(); globalThis.stopDataListener(); globalThis.stopSyncListener(); + this.kvManager = null; } } @@ -135,12 +156,9 @@ export default class MainAbility extends Ability { try { const kvManagerConfig = { bundleName: this.appId, - userInfo: { - userId: '0', - userType: 0 - }, context : globalThis.abilityContext, } + /* API8 distributedData.createKVManager(kvManagerConfig).then((manager) => { console.log ("[JSDemo]: CreateKVManager success"); this.logText += "CreateKVManager success\r\n"; @@ -149,6 +167,16 @@ export default class MainAbility extends Ability { console.log("[JSDemo]: createKVManager err: " + JSON.stringify(err)); this.logText += "createKVManager err: " + JSON.stringify(err) + "\r\n"; }); + */ + try { + this.kvManager = distributedData.createKVManager(kvManagerConfig); + console.log("[JSDemo]: Succeeded in creating KVManager"); + this.logText += "CreateKVManager success\r\n"; + } catch (err) { + console.log("[JSDemo]: createKVManager err: " + JSON.stringify(err)); + this.logText += "createKVManager err: " + JSON.stringify(err) + "\r\n"; + } + } catch (e) { console.log("[JSDemo]: An unexpected error occurred. Error:" + e); this.logText += "An unexpected error occurred. Error: " + e + "\r\n"; @@ -171,18 +199,19 @@ export default class MainAbility extends Ability { try { const options = { createIfMissing: true, - encrypt: false , + encrypt: globalThis.encrypt, backup: false, - autoSync: false, - kvStoreType: this.demoKvStoreType, // DEVICE_COLLABORATION--0, SINGLE_VERSION = 1 - securityLevel: distributedData.SecurityLevel.S1, + autoSync: globalThis.autoSync, + kvStoreType: globalThis.kvStoreType, // DEVICE_COLLABORATION--0, SINGLE_VERSION = 1 + securityLevel: globalThis.securityLevel, }; let self = this; this.kvManager.getKVStore(this.storeId, options).then((store) => { - console.log("[JSDemo]: getKVStore success"); - this.logText += "getKVStore success\r\n"; - this.logText += "kvStore options : " + JSON.stringify(options) +"\r\n"; + console.log("[JSDemo]: getKVStore success, options :" + JSON.stringify(options)); + + self.logText += "kvStore options : " + JSON.stringify(options) +"\r\n"; + self.logText += "getKVStore success\r\n"; self.kvStore = store; }).catch((err) => { console.log("[JSDemo]: getKVStore err: " + JSON.stringify(err)); @@ -193,20 +222,46 @@ export default class MainAbility extends Ability { this.logText += "GetKvStore: An unexpected error occurred. Error: " + e +"\r\n"; } }) + globalThis.PutRandom = (() => { + if (this.kvStore == undefined) { + console.log('[JSDemo]: Put: kvStore not initialized'); + this.logText += "Put: kvStore not initialized\r\n"; + return; + } + try { + let key = "TEST_" + nameList[Math.floor(Math.random() * nameList.length)]; + let value = random(1, 100); + let self = this; + this.kvStore.put(key, value).then((data) => { + console.log("[JSDemo]: put success. "); + self.logText += "put success. \r\n"; + globalThis.needDelLst.push(key); + }).catch((err) => { + console.log("[JSDemo]: put err: " + JSON.stringify(err)); + self.logText += "put err: " + JSON.stringify(err) + "\r\n"; + }); + } catch (e) { + console.log("[JSDemo]: An unexpected error occurred. Error:" + e); + this.logText += "PUT: An unexpected error occurred. Error: " + e +"\r\n"; + } + }) - globalThis.Put = (() => { + globalThis.Put = (() => { if (this.kvStore == undefined) { console.log('[JSDemo]: Put: kvStore not initialized'); this.logText += "Put: kvStore not initialized\r\n"; return; } try { - this.kvStore.put(JSON.stringify('key_test_string'), JSON.stringify('value_test_string')).then((data) => { + let key = JSON.stringify('key_test_string'); + let value = random(1, 100); + let self = this; + this.kvStore.put(key, value).then((data) => { console.log("[JSDemo]: put success. "); - this.logText += "put success. \r\n"; + self.logText += "put success. \r\n"; }).catch((err) => { console.log("[JSDemo]: put err: " + JSON.stringify(err)); - this.logText += "put err: " + JSON.stringify(err) + "\r\n"; + self.logText += "put err: " + JSON.stringify(err) + "\r\n"; }); } catch (e) { console.log("[JSDemo]: An unexpected error occurred. Error:" + e); @@ -221,31 +276,32 @@ export default class MainAbility extends Ability { return; } try { - if (this.demoKvStoreType == 0) { //deviceKvStore + if (globalThis.kvStoreType == distributedData.KVStoreType.DEVICE_COLLABORATION) { //deviceKvStore var localDeviceId = this.devManager.getLocalDeviceInfoSync().deviceId; + let self = this; const promise1 = this.kvStore.get(localDeviceId, JSON.stringify('key_test_string')); promise1.then((data) => { console.log("[JSDemo] Get with local deviceId success " + data); - this.logText += ("Get with local deviceId success. data:" + data + "\r\n"); + self.logText += ("Get with local deviceId success. data:" + data + "\r\n"); }).catch((err) => { var remoteDeviceId = this.devManager.getTrustedDeviceListSync()[0].deviceId; const promise2 = this.kvStore.get(remoteDeviceId, JSON.stringify('key_test_string')); promise2.then((data) => { console.log("[JSDemo] Get with remote deviceId success " + data); - this.logText += ("Get with remote deviceId success. data:" + data + "\r\n"); + self.logText += ("Get with remote deviceId success. data:" + data + "\r\n"); }).catch((err) => { console.log("[JSDemo] Get fail " + err); - this.logText += "Get fail " + err + ".\r\n"; + self.logText += "Get fail " + err + ".\r\n"; }); }); - } else if (this.demoKvStoreType == 1) { // singleKvStore - const promise1 = this.kvStore.get(JSON.stringify('key_test_string')); - promise1.then((data) => { - console.log("[JSDemo] Get success " + data); - this.logText += ("Get success. data : " + data + "\r\n"); + } else if (globalThis.kvStoreType == distributedData.KVStoreType.SINGLE_VERSION) { // singleKvStore + let self = this; + this.kvStore.getEntries('TEST').then((entries) => { + console.log("[JSDemo] Get singleKvStore success " + entries); + self.logText += ("Get singleKvStore success. data : " + entries + "\r\n"); }).catch((err) => { - console.log("[JSDemo] Get fail " + err); - this.logText += "Get fail " + err + ".\r\n"; + console.error(`[JSDemo] Fail to get singleKvStore Entries.code is ${err.code},message is ${err.message}`); + self.logText += ("Get singleKvStore fail is " + err.code + "message is" + err.message + ".\r\n"); }); } } catch (e) { @@ -254,6 +310,46 @@ export default class MainAbility extends Ability { } }) + globalThis.GetAllData = (() => { + if (this.kvStore == undefined) { + console.log('[JSDemo]: GetAllData: kvStore not initialized'); + this.logText += " GetAllData: kvStore not initialized\r\n"; + return; + } + try { + let self = this; + if (globalThis.kvStoreType == distributedData.KVStoreType.DEVICE_COLLABORATION) { //deviceKvStore + var localDeviceId = this.devManager.getLocalDeviceInfoSync().deviceId; + this.kvStore.getEntries(localDeviceId, 'TEST').then((entries) => { + console.log("[JSDemo] Get All localDevice data success, entries.length: " + entries.length + ", entries:" + JSON.stringify(entries)); + self.logText += ("Get All local localDevice success. data : " + JSON.stringify(entries) + "\r\n"); + }).catch((err) => { + var remoteDeviceId = this.devManager.getTrustedDeviceListSync()[0].deviceId; + this.kvStore.getEntries(remoteDeviceId, 'TEST').then((entries) => { + console.log("[JSDemo] Get All remoteDevice data success, entries.length: " + entries.length + ", entries:" + JSON.stringify(entries)); + self.logText += ("Get All remoteDevice data success. data : " + JSON.stringify(entries) + "\r\n"); + }).catch((err) => { + console.error(`[JSDemo] Fail to Get All remoteDevice entries.code is ${err.code},message is ${err.message}`); + self.logText += (`Fail to Get All remoteDevice entries.code is ${err.code},message is ${err.message}` + ".\r\n"); + }); + }); + } else if (globalThis.kvStoreType == distributedData.KVStoreType.SINGLE_VERSION) { // singleKvStore + this.kvStore.getEntries('TEST', function (err, entries) { + if (err != undefined) { + console.error(`[JSDemo] Fail to get Entries.code is ${err.code},message is ${err.message}`); + self.logText += (`Fail to get Entries.code is ${err.code},message is ${err.message}`+ "\r\n"); + return; + } + console.log("[JSDemo] GetAllData success, entries.length: " + entries.length + ", entries:" + JSON.stringify(entries)); + self.logText += ("GetAllData success. data : " + JSON.stringify(entries) + "\r\n"); + }); + } + } catch (e) { + console.log("[JSDemo]:GetAllData An unexpected error occurred. Error:" + e); + this.logText += "GetAllData: An unexpected error occurred. Error: " + e +"\r\n"; + } + }) + globalThis.Sync = (() =>{ console.info("[JSDemo] Sync begin"); this.logText += "Sync begin\r\n"; @@ -271,9 +367,10 @@ export default class MainAbility extends Ability { var devices = this.deviceList; console.info('[JSDemo] Sync start'); this.logText += "Sync start\r\n"; - this.kvStore.sync(devices, distributedData.SyncMode.PUSH_PULL); + this.kvStore.sync(devices, globalThis.syncMode); }) + // delete one data globalThis.Delete = (() =>{ console.info("[JSDemo] Delete begin"); this.logText += "Delete begin\r\n"; @@ -283,10 +380,10 @@ export default class MainAbility extends Ability { return; } try { - const promise1 = this.kvStore.delete(JSON.stringify('key_test_string')); + const promise1 = this.kvStore.delete(JSON.stringify("key_test_string")); promise1.then((data) => { - console.log("[JSDemo] Delete success "); - this.logText += "Delete success\r\n"; + console.log("[JSDemo] Delete success, key_test_string"); + this.logText += "Delete success, key_test_string"+"\r\n"; }).catch((err) => { console.log("[JSDemo] Delete fail " + err); this.logText += "Delete fail" +JSON.stringify(err)+ "\r\n"; @@ -297,6 +394,65 @@ export default class MainAbility extends Ability { } }) + globalThis.DeleteBasePrefix = (() =>{ + console.info("[JSDemo] DeleteBasePrefix begin"); + this.logText += "DeleteBasePrefix begin\r\n"; + if(this.kvStore == null) { + console.info('[JSDemo] DeleteBasePrefix kvStore is null'); + this.logText += "DeleteBasePrefix, kvStore is null\r\n"; + return; + } + try { + let self = this; + let predicates = new dataSharePredicates.DataSharePredicates(); + let arr = ["TEST"]; + predicates.inKeys(arr); + const promise = this.kvStore.delete(predicates); + promise.then((data) => { + console.log("[JSDemo] DeleteBasePrefix success "); + this.logText += "DeleteBasePrefix success\r\n"; + }).catch((err) => { + console.error(`[JSDemo]Fail to DeleteBasePrefix.code is ${err.code},message is ${err.message}`); + self.logText += (`DeleteBasePrefix, Fail to delete.code is ${err.code},message is ${err.message}` + "\r\n"); + }); + + } catch(e) { + console.log("[JSDemo] DeleteBasePrefix err " + e); + this.logText += "DeleteBasePrefix err" +JSON.stringify(e)+ "\r\n"; + } + }) + + globalThis.DeleteAllData = (() =>{ + console.info("[JSDemo] DeleteAllData begin"); + this.logText += "DeleteAllData begin\r\n"; + if(this.kvStore == null) { + console.info('[JSDemo] DeleteAllData kvStore is null'); + this.logText += "DeleteAllData, kvStore is null\r\n"; + return; + } + if(globalThis.needDelLst.length<1){ + console.log("[JSDemo] DeleteAllData: no data need del"); + this.logText += "DeleteAllData: no data need del \r\n"; + return; + } + try { + let self = this; + const promise = this.kvStore.deleteBatch(globalThis.needDelLst); + promise.then((data) => { + console.log("[JSDemo] DeleteAllData success,"+ globalThis.needDelLst); + self.logText += "DeleteAllData success,"+ globalThis.needDelLst +" \r\n"; + }).catch((err) => { + console.error(`[JSDemo]Fail to DeleteAllData.code is ${err.code},message is ${err.message}`); + self.logText += (`DeleteAllData, Fail to delete.code is ${err.code},message is ${err.message}` + "\r\n"); + }); + + } catch(e) { + console.log("[JSDemo] DeleteAllData err " + e); + this.logText += "DeleteAllData err" +JSON.stringify(e)+ "\r\n"; + } + }) + + globalThis.DeleteKvStore = (() =>{ console.info("[JSDemo] DeleteKvStore begin"); this.logText += "DeleteKvStore begin\r\n"; @@ -311,30 +467,6 @@ export default class MainAbility extends Ability { return; } try { -// this.kvManager.closeKVStore(this.context.applicationInfo.name, this.storeId, this.kvStore).then(() => { -// }) -// this.kvManager = null; -// -// this.context.switchArea(AreaMode.EL1); -// let context = this.context; -// const kvManagerConfig = { -// bundleName: this.appId, -// userInfo: { -// userId: '0', -// userType: distributedData.UserType.SAME_USER_ID -// }, -// context : context, -// } -// distributedData.createKVManager(kvManagerConfig).then((manager) => { -// console.log ("[JSDemo]: CreateKVManager success"); -// this.logText += "CreateKVManager success AreaMode::EL1\r\n"; -//// this.kvManager = manager; -// }).catch((err) => { -// console.log("[JSDemo]: createKVManager err: " + JSON.stringify(err)); -// this.logText += "createKVManager err: " + JSON.stringify(err) + "\r\n"; -// }); - - this.kvManager.closeKVStore(this.appId, this.storeId).then(() => { console.log('closeKVStore success'); this.logText += "closeKVStore success.\r\n"; @@ -346,7 +478,7 @@ export default class MainAbility extends Ability { console.log('deleteKVStore success'); this.logText += "deleteKVStore success.\r\n"; this.kvStore = null; - this.kvManager = null; + //this.kvManager = null; }).catch((err) => { console.log('deleteKVStore err ' ); this.logText += "deleteKVStore fail." + JSON.stringify(err) + "\r\n"; @@ -419,8 +551,8 @@ export default class MainAbility extends Ability { return; } try { - this.logText += "register dataChange SubscribeType:" + this.subscribeType + ".\r\n"; - this.kvStore.on('dataChange', this.subscribeType, function (data) { + this.logText += "register dataChange SubscribeType:" + globalThis.subscribeType + ".\r\n"; + this.kvStore.on('dataChange', globalThis.subscribeType, function (data) { console.info('[JSDemo] dataChange callback call' + data); that.logText += "dataChange callback call.\r\n"; if(data) { @@ -478,10 +610,91 @@ export default class MainAbility extends Ability { this.logText = "" return this.logText; }) + + globalThis.SetEncrypt = ((encrypt) =>{ + if(encrypt==0){ + globalThis.encrypt=false; + }else{ + globalThis.encrypt=true; + } + console.info("[JSDemo] SetEncrypt:" + globalThis.encrypt); + this.logText+="SetEncrypt:" + globalThis.encrypt + "\r\n"; + }) + + globalThis.SetAutoSync = ((autoSync) =>{ + if(autoSync==0){ + globalThis.autoSync=false; + }else{ + globalThis.autoSync=true; + } + console.info("[JSDemo] SetAutoSync:" + globalThis.autoSync); + this.logText+="SetAutoSync:" + globalThis.autoSync + "\r\n"; + }) + + globalThis.SetKvStoreType = ((kvStoreType) =>{ + if(kvStoreType==0){ + globalThis.kvStoreType=distributedData.KVStoreType.DEVICE_COLLABORATION; + } else { + globalThis.kvStoreType=distributedData.KVStoreType.SINGLE_VERSION; + } + console.info("[JSDemo] SetKvStoreType:" + globalThis.kvStoreType); + this.logText+="SetKvStoreType:" + globalThis.kvStoreType + "\r\n"; + }) + + globalThis.SetSecurityLevel = ((securityLevel) =>{ + let tmp = "NA"; + if(securityLevel==0){ + globalThis.securityLevel=distributedData.SecurityLevel.S1; + tmp="S1"; + } else if(securityLevel==1) { + globalThis.securityLevel=distributedData.SecurityLevel.S2; + tmp="S2"; + } else if(securityLevel==2) { + globalThis.securityLevel=distributedData.SecurityLevel.S3; + tmp="S3"; + } else if(securityLevel==3) { + globalThis.securityLevel=distributedData.SecurityLevel.S4; + tmp="S4"; + } else { // para range error + globalThis.securityLevel=distributedData.SecurityLevel.S1; + tmp="S1, para range error"; + } + console.info("[JSDemo] SetSecurityLevel:" + tmp); + this.logText+="SetSecurityLevel:" + tmp + "\r\n"; + }) + + globalThis.SetSyncMode = ((syncMode) =>{ + if(syncMode==0){ + globalThis.syncMode=distributedData.SyncMode.PULL_ONLY; + } else if(syncMode==1){ + globalThis.syncMode=distributedData.SyncMode.PUSH_ONLY; + } else if(syncMode==2){ + globalThis.syncMode=distributedData.SyncMode.PUSH_PULL; + } else { // para range error + globalThis.syncMode=distributedData.SyncMode.PUSH_PULL; + } + console.info("[JSDemo] SetSyncMode:" + globalThis.syncMode); + this.logText+="SetSyncMode:" + globalThis.syncMode + "\r\n"; + }) + + globalThis.SetSubscribeType = ((subscribeType) =>{ + if(subscribeType==0){ + globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL; + } else if(subscribeType==1){ + globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; + } else if(subscribeType==2){ + globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL; + } else { // para range error + globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; + } + console.info("[JSDemo] SetSubscribeType:" + globalThis.subscribeType); + this.logText+="SetSubscribeType:" + globalThis.subscribeType + "\r\n"; + }) + } onWindowStageDestroy() { - //Main window is destroyed, release UI related resources + // Main window is destroyed, release UI related resources console.info("[Demo] MainAbility onWindowStageDestroy") } @@ -494,4 +707,4 @@ export default class MainAbility extends Ability { // Ability has back to background console.info("[Demo] MainAbility onBackground") } -}; \ No newline at end of file +}; diff --git a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets index bee9840..f23b099 100644 --- a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets +++ b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets @@ -12,20 +12,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -//import file from '@system.file'; -// -////import {Core, ExpectExtend, InstrumentLog, ReportExtend} from "deccjsunit/index.ets" + import {Core, ExpectExtend} from "hypium/index" //import testsuite from "../../test/List.test" -import router from '@system.router'; +import router from '@ohos.router'; async function routePage() { let options = { - uri: 'pages/second' + url: 'pages/second/second' } try { - await router.push(options) + await router.pushUrl({url: 'pages/second/second'}) // API9 } catch (err) { console.error(`fail callback, code: ${err.code}, msg: ${err.msg}`) } @@ -57,148 +55,242 @@ struct Index { } @State text: string = '' + + @State encryptSelect:number = 0 + private encryptLst:string[] = ['false','true'] + + @State autoSyncSelect:number = 0 + private autoSyncLst:string[] = ['false','true'] + + @State kvStoreTypeSelect:number = 0 + private kvStoreTypeLst:string[] = ['DEVICE_COLLABORATION','SINGLE_VERSION'] + + @State securityLevelSelect:number = 0 + private securityLevelLst:string[] = ['S1','S2','S3','S4'] + + @State subscribeTypeSelect:number = 1 + private subscribeTypeLst:string[] = ['LOCAL','REMOTE','ALL'] + + @State syncModSelect:number = 2 + private syncModLst:string[] = ['PULL_ONLY','PUSH_ONLY','PUSH_PULL'] build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { - Button('CreateKvManager').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') + Column({space:2}) { + Row({space:2}) { + Button('CreateKvManager').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.CreateKvManager(); this.text = globalThis.printLogs(); }) + Button('SetEncrypt').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.encryptLst, + selected:this.encryptSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetEncrypt(value.index) + this.encryptSelect=value.index; + console.info("[JSDemo]encrypt:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]encrypt:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]encrypt:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } + }) + }) + Button('SetAutoSync').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.autoSyncLst, + selected:this.autoSyncSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetAutoSync(value.index) + this.autoSyncSelect=value.index; + console.info("[JSDemo]autoSync:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]autoSync:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]autoSync:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } + }) + }) } .width('100%') - .height('5%') - .border({width:1}) + .height('6%') - Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { - Column() { - Button('GetKvStore').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.GetKvStore(); - this.text = globalThis.printLogs(); + Row({space:2}) { + Button('SetKvStoreType').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.kvStoreTypeLst, + selected:this.kvStoreTypeSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetKvStoreType(value.index) + this.kvStoreTypeSelect=value.index; + console.info("[JSDemo]kvStoreType:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]kvStoreType:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]kvStoreType:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } }) - Button('DeleteKvStore').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.DeleteKvStore(); - this.text = globalThis.printLogs(); + }) + Button('SetSecurityLevel').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.securityLevelLst, + selected:this.securityLevelSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetSecurityLevel(value.index) + this.securityLevelSelect=value.index; + console.info("[JSDemo]securityLevel:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]securityLevel:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]securityLevel:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } + }) }) - } - -// Column() { -// Button('SetSecurityLevel').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') -// .backgroundColor('#f32dc4aa').onClick(() => { -// this.text = globalThis.printLogs(); -// }) -// Button('GetSecurityLevel').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') -// .backgroundColor('#f32dc4aa').onClick(() => { -// this.text = globalThis.printLogs(); -// }) -// } + Button('GetKvStore').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.GetKvStore(); + this.text = globalThis.printLogs(); + }) + } .width('100%') - .height('10%') - .border({width:1}) - - Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { - Column() { -// Button('SubscribeType').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') -// .backgroundColor('#f32dc4aa').onClick(() => { -// this.text = globalThis.printLogs(); -// }) - Button('RegDataChange').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.RegDataChange(); - this.text = globalThis.printLogs(); - }) - } + .height('6%') - Column() { - Button('RegSyncComplete').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('230') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.RegSyncComplete(); - this.text = globalThis.printLogs(); + Row({space:2}) { + Button('SetSubscribeType').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.subscribeTypeLst, + selected:this.subscribeTypeSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetSubscribeType(value.index) + this.subscribeTypeSelect=value.index; + console.info("[JSDemo]SetSubscribeType:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]SetSubscribeType:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]SetSubscribeType:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } }) - } + }) + Button('RegDataChange').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.RegDataChange(); + this.text = globalThis.printLogs(); + }) + Button('RegSyncComplete').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.RegSyncComplete(); + this.text = globalThis.printLogs(); + }) } .width('100%') - .height('10%') - .border({width:1}) + .height('6%') - Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { - Column() { - Button('Put').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('120') + Row({space:2}) { + Button('Put').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') .backgroundColor('#f32dc400').onClick(() => { - globalThis.Put(); - this.text = globalThis.printLogs(); - }) - Button('Get').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('120') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.Get(); - this.text = globalThis.printLogs(); - }) - } + globalThis.PutRandom(); + this.text = globalThis.printLogs(); + }) + Button('Get').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.GetAllData(); + this.text = globalThis.printLogs(); + }) - Column() { - Button('Delete').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('120') - .backgroundColor('#f32dc400').onClick(() => { - globalThis.Delete(); - this.text = globalThis.printLogs(); + Button('Delete').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.DeleteAllData(); + this.text = globalThis.printLogs(); + }) + } + .width('100%') + .height('6%') + + Row({space:2}) { + Button('SetSyncMode').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .onClick(() => { + TextPickerDialog.show({ + range:this.syncModLst, + selected:this.syncModSelect, + onAccept:(value:TextPickerResult)=>{ + globalThis.SetSyncMode(value.index) + this.syncModSelect=value.index; + console.info("[JSDemo]syncMod:onAccept()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + }, + onCancel:()=>{ + console.info("[JSDemo]syncMod:onCancel()"); + }, + onChange:(value:TextPickerResult)=>{ + console.info("[JSDemo]syncMod:onChange()" + JSON.stringify(value)); + this.text=globalThis.printLogs(); + } + }) }) - Button('Sync').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('120') - .backgroundColor('#f32dc400').onClick(() => { + Button('Sync').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { globalThis.Sync(); this.text = globalThis.printLogs(); }) - } - - Column() { - Button('SyncMode').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150') - .backgroundColor('#f32dc4aa').onClick(() => { + Button('DeleteKvStore').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + globalThis.DeleteKvStore(); this.text = globalThis.printLogs(); }) - Button('ClearLog').fontSize(20).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150') - .backgroundColor('#f32dc400').onClick(() => { - this.text = globalThis.clearLog(); + } + .width('100%') + .height('6%') + + Row({space:2}){ + Button('PrintLogs').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + this.text = globalThis.printLogs(); }) - } + Button('ClearLog').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + .backgroundColor('#f32dc400').onClick(() => { + this.text = globalThis.clearLog(); + }) } .width('100%') - .height('10%') - .border({width:1}) + .height('6%') Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { Text(this.text) { - }.baselineOffset(10) + }.baselineOffset(10).fontSize(9) } .width('100%') - .height('55%') + .height('60%') .border({width:1}) } - - .width('100%') + .width('99%') .height('100%') } -// build() { -// Flex({ direction:FlexDirection.Column, alignItems:ItemAlign.Center, justifyContent: FlexAlign.Center }) { -// Text('Hello World') -// .fontSize(50) -// .fontWeight(FontWeight.Bold) -// Button() { -// Text('next page') -// .fontSize(25) -// .fontWeight(FontWeight.Bold) -// }.type(ButtonType.Capsule) -// .margin({ -// top: 20 -// }) -// .backgroundColor('#0D9FFB') -// .onClick(() => { -// -// }) -// } -// .width('100%') -// .height('100%') -// } } \ No newline at end of file -- Gitee From 1b8a4a4bcfc752ad453b49f4cddb184d33b71d9f Mon Sep 17 00:00:00 2001 From: genglingxia Date: Thu, 9 Mar 2023 17:23:47 +0800 Subject: [PATCH 2/4] Review comments modification. --- .../src/main/ets/MainAbility/MainAbility.ts | 183 ++++-------------- .../entry/src/main/ets/pages/index/index.ets | 10 +- 2 files changed, 38 insertions(+), 155 deletions(-) diff --git a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts index 7fe8c6e..b81bf07 100644 --- a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts @@ -3,10 +3,8 @@ import deviceManager from '@ohos.distributedHardware.deviceManager'; import distributedData from '@ohos.data.distributedKVStore'; import accessControl from '@ohos.abilityAccessCtrl'; import bundle from '@ohos.bundle'; -import dataSharePredicates from '@ohos.data.dataSharePredicates'; - -//import {AreaMode} from "application/Context" +const DEMO_KEY_PREFIX = '#TEST#' var nameList = ["Leila", "Ursa", "Crystal", "Rebecca", "Taylor", "Elizabeth", "Ayn", "Rosa", "Beatrice", "Tracy", "Emily", "Glenn", "Iris", "Sara", "Freddie", "Cindy", "Devin", "Karen", "Zillah", "Ivy", "Xena", "Aimee", "Olivia", @@ -41,7 +39,7 @@ export default class MainAbility extends Ability { this.logText = "--------------Stage module----------------\r\n\n" globalThis.encrypt = false; globalThis.autoSync = false; - globalThis.kvStoreType = distributedData.KVStoreType.DEVICE_COLLABORATION; + globalThis.kvStoreType = distributedData.KVStoreType.SINGLE_VERSION; globalThis.securityLevel = distributedData.SecurityLevel.S1; globalThis.subscribeType = distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; globalThis.syncMode = distributedData.SyncMode.PUSH_PULL; @@ -152,22 +150,11 @@ export default class MainAbility extends Ability { this.logText += "CreateKvManager Enter\r\n"; globalThis.CreateDevManager(); if (typeof (this.kvManager) === 'undefined') { - let self = this; try { const kvManagerConfig = { bundleName: this.appId, context : globalThis.abilityContext, } - /* API8 - distributedData.createKVManager(kvManagerConfig).then((manager) => { - console.log ("[JSDemo]: CreateKVManager success"); - this.logText += "CreateKVManager success\r\n"; - self.kvManager = manager; - }).catch((err) => { - console.log("[JSDemo]: createKVManager err: " + JSON.stringify(err)); - this.logText += "createKVManager err: " + JSON.stringify(err) + "\r\n"; - }); - */ try { this.kvManager = distributedData.createKVManager(kvManagerConfig); console.log("[JSDemo]: Succeeded in creating KVManager"); @@ -229,7 +216,7 @@ export default class MainAbility extends Ability { return; } try { - let key = "TEST_" + nameList[Math.floor(Math.random() * nameList.length)]; + let key = DEMO_KEY_PREFIX + nameList[Math.floor(Math.random() * nameList.length)]; let value = random(1, 100); let self = this; this.kvStore.put(key, value).then((data) => { @@ -246,70 +233,6 @@ export default class MainAbility extends Ability { } }) - globalThis.Put = (() => { - if (this.kvStore == undefined) { - console.log('[JSDemo]: Put: kvStore not initialized'); - this.logText += "Put: kvStore not initialized\r\n"; - return; - } - try { - let key = JSON.stringify('key_test_string'); - let value = random(1, 100); - let self = this; - this.kvStore.put(key, value).then((data) => { - console.log("[JSDemo]: put success. "); - self.logText += "put success. \r\n"; - }).catch((err) => { - console.log("[JSDemo]: put err: " + JSON.stringify(err)); - self.logText += "put err: " + JSON.stringify(err) + "\r\n"; - }); - } catch (e) { - console.log("[JSDemo]: An unexpected error occurred. Error:" + e); - this.logText += "PUT: An unexpected error occurred. Error: " + e +"\r\n"; - } - }) - - globalThis.Get = (() => { - if (this.kvStore == undefined) { - console.log('[JSDemo]: Get: kvStore not initialized'); - this.logText += " Get: kvStore not initialized\r\n"; - return; - } - try { - if (globalThis.kvStoreType == distributedData.KVStoreType.DEVICE_COLLABORATION) { //deviceKvStore - var localDeviceId = this.devManager.getLocalDeviceInfoSync().deviceId; - let self = this; - const promise1 = this.kvStore.get(localDeviceId, JSON.stringify('key_test_string')); - promise1.then((data) => { - console.log("[JSDemo] Get with local deviceId success " + data); - self.logText += ("Get with local deviceId success. data:" + data + "\r\n"); - }).catch((err) => { - var remoteDeviceId = this.devManager.getTrustedDeviceListSync()[0].deviceId; - const promise2 = this.kvStore.get(remoteDeviceId, JSON.stringify('key_test_string')); - promise2.then((data) => { - console.log("[JSDemo] Get with remote deviceId success " + data); - self.logText += ("Get with remote deviceId success. data:" + data + "\r\n"); - }).catch((err) => { - console.log("[JSDemo] Get fail " + err); - self.logText += "Get fail " + err + ".\r\n"; - }); - }); - } else if (globalThis.kvStoreType == distributedData.KVStoreType.SINGLE_VERSION) { // singleKvStore - let self = this; - this.kvStore.getEntries('TEST').then((entries) => { - console.log("[JSDemo] Get singleKvStore success " + entries); - self.logText += ("Get singleKvStore success. data : " + entries + "\r\n"); - }).catch((err) => { - console.error(`[JSDemo] Fail to get singleKvStore Entries.code is ${err.code},message is ${err.message}`); - self.logText += ("Get singleKvStore fail is " + err.code + "message is" + err.message + ".\r\n"); - }); - } - } catch (e) { - console.log("[JSDemo]: An unexpected error occurred. Error:" + e); - this.logText += "Get: An unexpected error occurred. Error: " + e +"\r\n"; - } - }) - globalThis.GetAllData = (() => { if (this.kvStore == undefined) { console.log('[JSDemo]: GetAllData: kvStore not initialized'); @@ -320,12 +243,12 @@ export default class MainAbility extends Ability { let self = this; if (globalThis.kvStoreType == distributedData.KVStoreType.DEVICE_COLLABORATION) { //deviceKvStore var localDeviceId = this.devManager.getLocalDeviceInfoSync().deviceId; - this.kvStore.getEntries(localDeviceId, 'TEST').then((entries) => { + this.kvStore.getEntries(localDeviceId, DEMO_KEY_PREFIX).then((entries) => { console.log("[JSDemo] Get All localDevice data success, entries.length: " + entries.length + ", entries:" + JSON.stringify(entries)); self.logText += ("Get All local localDevice success. data : " + JSON.stringify(entries) + "\r\n"); }).catch((err) => { var remoteDeviceId = this.devManager.getTrustedDeviceListSync()[0].deviceId; - this.kvStore.getEntries(remoteDeviceId, 'TEST').then((entries) => { + this.kvStore.getEntries(remoteDeviceId, DEMO_KEY_PREFIX).then((entries) => { console.log("[JSDemo] Get All remoteDevice data success, entries.length: " + entries.length + ", entries:" + JSON.stringify(entries)); self.logText += ("Get All remoteDevice data success. data : " + JSON.stringify(entries) + "\r\n"); }).catch((err) => { @@ -334,7 +257,7 @@ export default class MainAbility extends Ability { }); }); } else if (globalThis.kvStoreType == distributedData.KVStoreType.SINGLE_VERSION) { // singleKvStore - this.kvStore.getEntries('TEST', function (err, entries) { + this.kvStore.getEntries(DEMO_KEY_PREFIX, function (err, entries) { if (err != undefined) { console.error(`[JSDemo] Fail to get Entries.code is ${err.code},message is ${err.message}`); self.logText += (`Fail to get Entries.code is ${err.code},message is ${err.message}`+ "\r\n"); @@ -370,58 +293,6 @@ export default class MainAbility extends Ability { this.kvStore.sync(devices, globalThis.syncMode); }) - // delete one data - globalThis.Delete = (() =>{ - console.info("[JSDemo] Delete begin"); - this.logText += "Delete begin\r\n"; - if(this.kvStore == null) { - console.info('[JSDemo] Delete kvStore is null'); - this.logText += "Delete kvStore is null\r\n"; - return; - } - try { - const promise1 = this.kvStore.delete(JSON.stringify("key_test_string")); - promise1.then((data) => { - console.log("[JSDemo] Delete success, key_test_string"); - this.logText += "Delete success, key_test_string"+"\r\n"; - }).catch((err) => { - console.log("[JSDemo] Delete fail " + err); - this.logText += "Delete fail" +JSON.stringify(err)+ "\r\n"; - }); - } catch(e) { - console.log("[JSDemo] Delete err " + e); - this.logText += "Delete err" +JSON.stringify(e)+ "\r\n"; - } - }) - - globalThis.DeleteBasePrefix = (() =>{ - console.info("[JSDemo] DeleteBasePrefix begin"); - this.logText += "DeleteBasePrefix begin\r\n"; - if(this.kvStore == null) { - console.info('[JSDemo] DeleteBasePrefix kvStore is null'); - this.logText += "DeleteBasePrefix, kvStore is null\r\n"; - return; - } - try { - let self = this; - let predicates = new dataSharePredicates.DataSharePredicates(); - let arr = ["TEST"]; - predicates.inKeys(arr); - const promise = this.kvStore.delete(predicates); - promise.then((data) => { - console.log("[JSDemo] DeleteBasePrefix success "); - this.logText += "DeleteBasePrefix success\r\n"; - }).catch((err) => { - console.error(`[JSDemo]Fail to DeleteBasePrefix.code is ${err.code},message is ${err.message}`); - self.logText += (`DeleteBasePrefix, Fail to delete.code is ${err.code},message is ${err.message}` + "\r\n"); - }); - - } catch(e) { - console.log("[JSDemo] DeleteBasePrefix err " + e); - this.logText += "DeleteBasePrefix err" +JSON.stringify(e)+ "\r\n"; - } - }) - globalThis.DeleteAllData = (() =>{ console.info("[JSDemo] DeleteAllData begin"); this.logText += "DeleteAllData begin\r\n"; @@ -478,7 +349,6 @@ export default class MainAbility extends Ability { console.log('deleteKVStore success'); this.logText += "deleteKVStore success.\r\n"; this.kvStore = null; - //this.kvManager = null; }).catch((err) => { console.log('deleteKVStore err ' ); this.logText += "deleteKVStore fail." + JSON.stringify(err) + "\r\n"; @@ -607,38 +477,47 @@ export default class MainAbility extends Ability { }) globalThis.clearLog = (() =>{ - this.logText = "" + this.logText = ""; return this.logText; }) globalThis.SetEncrypt = ((encrypt) =>{ + let tmp = "NA"; if(encrypt==0){ globalThis.encrypt=false; + tmp = "false"; }else{ globalThis.encrypt=true; + tmp = "true"; } - console.info("[JSDemo] SetEncrypt:" + globalThis.encrypt); - this.logText+="SetEncrypt:" + globalThis.encrypt + "\r\n"; + console.info("[JSDemo] SetEncrypt:" + tmp); + this.logText+="SetEncrypt:" + tmp + "\r\n"; }) globalThis.SetAutoSync = ((autoSync) =>{ + let tmp = "NA"; if(autoSync==0){ globalThis.autoSync=false; + tmp = "false"; }else{ globalThis.autoSync=true; + tmp = "true"; } - console.info("[JSDemo] SetAutoSync:" + globalThis.autoSync); - this.logText+="SetAutoSync:" + globalThis.autoSync + "\r\n"; + console.info("[JSDemo] SetAutoSync:" + tmp); + this.logText+="SetAutoSync:" + tmp + "\r\n"; }) globalThis.SetKvStoreType = ((kvStoreType) =>{ + let tmp = "NA"; if(kvStoreType==0){ globalThis.kvStoreType=distributedData.KVStoreType.DEVICE_COLLABORATION; + tmp = "DEVICE_COLLABORATION"; } else { globalThis.kvStoreType=distributedData.KVStoreType.SINGLE_VERSION; + tmp = "SINGLE_VERSION"; } - console.info("[JSDemo] SetKvStoreType:" + globalThis.kvStoreType); - this.logText+="SetKvStoreType:" + globalThis.kvStoreType + "\r\n"; + console.info("[JSDemo] SetKvStoreType:" + tmp); + this.logText+="SetKvStoreType:" + tmp + "\r\n"; }) globalThis.SetSecurityLevel = ((securityLevel) =>{ @@ -664,31 +543,41 @@ export default class MainAbility extends Ability { }) globalThis.SetSyncMode = ((syncMode) =>{ + let tmp = "NA"; if(syncMode==0){ globalThis.syncMode=distributedData.SyncMode.PULL_ONLY; + tmp="PULL_ONLY"; } else if(syncMode==1){ globalThis.syncMode=distributedData.SyncMode.PUSH_ONLY; + tmp="PUSH_ONLY"; } else if(syncMode==2){ globalThis.syncMode=distributedData.SyncMode.PUSH_PULL; + tmp="PUSH_PULL"; } else { // para range error globalThis.syncMode=distributedData.SyncMode.PUSH_PULL; + tmp="PUSH_PULL, para range error"; } - console.info("[JSDemo] SetSyncMode:" + globalThis.syncMode); - this.logText+="SetSyncMode:" + globalThis.syncMode + "\r\n"; + console.info("[JSDemo] SetSyncMode:" + tmp); + this.logText+="SetSyncMode:" + tmp + "\r\n"; }) globalThis.SetSubscribeType = ((subscribeType) =>{ + let tmp = "NA"; if(subscribeType==0){ globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL; + tmp="SUBSCRIBE_TYPE_LOCAL"; } else if(subscribeType==1){ globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; + tmp="SUBSCRIBE_TYPE_REMOTE"; } else if(subscribeType==2){ globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL; + tmp="SUBSCRIBE_TYPE_ALL"; } else { // para range error globalThis.subscribeType=distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE; + tmp="SUBSCRIBE_TYPE_REMOTE, para range error"; } - console.info("[JSDemo] SetSubscribeType:" + globalThis.subscribeType); - this.logText+="SetSubscribeType:" + globalThis.subscribeType + "\r\n"; + console.info("[JSDemo] SetSubscribeType:" + tmp); + this.logText+="SetSubscribeType:" + tmp + "\r\n"; }) } diff --git a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets index f23b099..22b3cbb 100644 --- a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets +++ b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets @@ -14,7 +14,6 @@ */ import {Core, ExpectExtend} from "hypium/index" -//import testsuite from "../../test/List.test" import router from '@ohos.router'; @@ -40,17 +39,12 @@ struct Index { 'id': 'extend' }) core.addService('expect', expectExtend) -// const reportExtend = new ReportExtend(file) - -// core.addService('report', reportExtend) core.init() -// core.subscribeEvent('task', reportExtend) const configService = core.getDefaultService('config') console.info('parameters---->' + JSON.stringify(globalThis.abilityWant.parameters)) globalThis.abilityWant.parameters.timeout = 70000; configService.setConfig(globalThis.abilityWant.parameters) globalThis.requestPermissions(); -// testsuite(globalThis.abilityContext) core.execute() } @@ -62,7 +56,7 @@ struct Index { @State autoSyncSelect:number = 0 private autoSyncLst:string[] = ['false','true'] - @State kvStoreTypeSelect:number = 0 + @State kvStoreTypeSelect:number = 1 private kvStoreTypeLst:string[] = ['DEVICE_COLLABORATION','SINGLE_VERSION'] @State securityLevelSelect:number = 0 @@ -293,4 +287,4 @@ struct Index { .height('100%') } -} \ No newline at end of file +} -- Gitee From cbc5a855bd69a309e16e95ccf320b4eb7642628b Mon Sep 17 00:00:00 2001 From: genglingxia Date: Thu, 9 Mar 2023 17:37:23 +0800 Subject: [PATCH 3/4] Review comments modification. --- kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts index b81bf07..e8030f8 100644 --- a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts @@ -312,6 +312,7 @@ export default class MainAbility extends Ability { promise.then((data) => { console.log("[JSDemo] DeleteAllData success,"+ globalThis.needDelLst); self.logText += "DeleteAllData success,"+ globalThis.needDelLst +" \r\n"; + globalThis.needDelLst=[]; }).catch((err) => { console.error(`[JSDemo]Fail to DeleteAllData.code is ${err.code},message is ${err.message}`); self.logText += (`DeleteAllData, Fail to delete.code is ${err.code},message is ${err.message}` + "\r\n"); -- Gitee From 2d359b8c7097df65ffd889eaf669d684d9d316ab Mon Sep 17 00:00:00 2001 From: genglingxia Date: Sat, 11 Mar 2023 17:54:46 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E9=A1=B5=E9=9D=A2=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/MainAbility/MainAbility.ts | 3 + .../entry/src/main/ets/pages/index/index.ets | 97 +++++++++++++------ kvdb_stage_demo/entry/src/main/module.json5 | 6 +- 3 files changed, 77 insertions(+), 29 deletions(-) diff --git a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts index e8030f8..90b7f1d 100644 --- a/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/kvdb_stage_demo/entry/src/main/ets/MainAbility/MainAbility.ts @@ -219,6 +219,7 @@ export default class MainAbility extends Ability { let key = DEMO_KEY_PREFIX + nameList[Math.floor(Math.random() * nameList.length)]; let value = random(1, 100); let self = this; + self.logText += "put start. key=" + key + ", value=" + value + " \r\n"; this.kvStore.put(key, value).then((data) => { console.log("[JSDemo]: put success. "); self.logText += "put success. \r\n"; @@ -240,6 +241,8 @@ export default class MainAbility extends Ability { return; } try { + console.log("[JSDemo] Get All localDevice data start. "); + this.logText += ("Get All local localDevice start. \r\n"); let self = this; if (globalThis.kvStoreType == distributedData.KVStoreType.DEVICE_COLLABORATION) { //deviceKvStore var localDeviceId = this.devManager.getLocalDeviceInfoSync().deviceId; diff --git a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets index 22b3cbb..2de3aca 100644 --- a/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets +++ b/kvdb_stage_demo/entry/src/main/ets/pages/index/index.ets @@ -48,7 +48,13 @@ struct Index { core.execute() } - @State text: string = '' + @State rotateAngle: number = 0 + @State speed: number = 1 + + scroller: Scroller = new Scroller() + private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + @State text: string = 'The green button calls the interface, and the blue button sets the properties.' @State encryptSelect:number = 0 private encryptLst:string[] = ['false','true'] @@ -70,12 +76,12 @@ struct Index { build() { Column({space:2}) { Row({space:2}) { - Button('CreateKvManager').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('CreateKvManager').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.CreateKvManager(); this.text = globalThis.printLogs(); }) - Button('SetEncrypt').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetEncrypt').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.encryptLst, @@ -95,7 +101,7 @@ struct Index { } }) }) - Button('SetAutoSync').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetAutoSync').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.autoSyncLst, @@ -116,11 +122,12 @@ struct Index { }) }) } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Row({space:2}) { - Button('SetKvStoreType').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetKvStoreType').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.kvStoreTypeLst, @@ -140,7 +147,7 @@ struct Index { } }) }) - Button('SetSecurityLevel').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetSecurityLevel').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.securityLevelLst, @@ -160,18 +167,19 @@ struct Index { } }) }) - Button('GetKvStore').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('GetKvStore').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.GetKvStore(); this.text = globalThis.printLogs(); }) } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Row({space:2}) { - Button('SetSubscribeType').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetSubscribeType').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.subscribeTypeLst, @@ -191,43 +199,45 @@ struct Index { } }) }) - Button('RegDataChange').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('RegDataChange').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.RegDataChange(); this.text = globalThis.printLogs(); }) - Button('RegSyncComplete').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('RegSyncComplete').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.RegSyncComplete(); this.text = globalThis.printLogs(); }) } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Row({space:2}) { - Button('Put').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('Put').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.PutRandom(); this.text = globalThis.printLogs(); }) - Button('Get').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('Get').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.GetAllData(); this.text = globalThis.printLogs(); }) - Button('Delete').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('Delete').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.DeleteAllData(); this.text = globalThis.printLogs(); }) } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Row({space:2}) { - Button('SetSyncMode').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('SetSyncMode').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .onClick(() => { TextPickerDialog.show({ range:this.syncModLst, @@ -247,43 +257,74 @@ struct Index { } }) }) - Button('Sync').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('Sync').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.Sync(); this.text = globalThis.printLogs(); }) - Button('DeleteKvStore').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('DeleteKvStore').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { globalThis.DeleteKvStore(); this.text = globalThis.printLogs(); }) } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Row({space:2}){ - Button('PrintLogs').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('PrintLogs').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { this.text = globalThis.printLogs(); }) - Button('ClearLog').fontSize(12).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('150').height('100%') + Button('ClearLog').fontSize(10).fontWeight(FontWeight.Normal).type(ButtonType.Capsule).margin({}).width('33%').height('100%') .backgroundColor('#f32dc400').onClick(() => { this.text = globalThis.clearLog(); }) + Column().width('33%').height('100%') } - .width('100%') + .justifyContent(FlexAlign.Center) + .width('99%') .height('6%') Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) { - Text(this.text) { + Scroll(this.scroller) { + Column(){ + Text(this.text).textAlign(TextAlign.Start).fontSize(10).baselineOffset(10).width('100%') + } + .backgroundColor(0xFFFFFF) + .borderRadius(15) + .gesture( + SwipeGesture({ direction: SwipeDirection.Vertical }) // ´¹Ö±»¬¶¯ + .onAction((event: GestureEvent) => { + this.scroller.scrollBy(0, 150) + }) + ) + .width('100%') + .height('500%') + } + .scrollable(ScrollDirection.Vertical) // ¹ö¶¯·½Ïò×ÝÏò + .scrollBar(BarState.On) // ¹ö¶¯Ìõ³£×¤ÏÔʾ + .scrollBarColor(Color.Gray) // ¹ö¶¯ÌõÑÕÉ« + .scrollBarWidth(3) // ¹ö¶¯Ìõ¿í¶È + .edgeEffect(EdgeEffect.None) + .onScroll((xOffset: number, yOffset: number) => { + console.info(xOffset + ' ' + yOffset) + }) + .onScrollEdge((side: Edge) => { + console.info('To the edge') + }) + .onScrollEnd(() => { + console.info('Scroll Stop') + }) - }.baselineOffset(10).fontSize(9) } - .width('100%') + .width('99%') .height('60%') .border({width:1}) } - .width('99%') + .justifyContent(FlexAlign.Center) + .width('100%') .height('100%') } diff --git a/kvdb_stage_demo/entry/src/main/module.json5 b/kvdb_stage_demo/entry/src/main/module.json5 index 2373bba..c97c9bb 100644 --- a/kvdb_stage_demo/entry/src/main/module.json5 +++ b/kvdb_stage_demo/entry/src/main/module.json5 @@ -38,6 +38,10 @@ { "name": "ohos.permission.DISTRIBUTED_DATASYNC" } - ] + ], + "metadata": [{ + "name": "ArkTSPartialUpdate", + "value": "true" + }] } } \ No newline at end of file -- Gitee