diff --git a/data/eTSLiteStorage/README.md b/data/eTSLiteStorage/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d790dbd098bf83cb4b88ccc3479c20c67c411a96 --- /dev/null +++ b/data/eTSLiteStorage/README.md @@ -0,0 +1,15 @@ +# 轻量级存储 + +### 简介 + +轻量级数据存储主要提供轻量级Key-Value操作,支持本地应用存储少量数据。本示例通过对购物车商品的添加和删除并保存退出的操作,使得再次打开应用时依然可以保留退出前的购物车信息,体现了轻量级存储在保存轻量级数据时的作用。 + +### 使用说明 + +1. 选择商品点击加入购物车,当购物车数量大于5时,右上方购物车图标为满状态,则购物车已满不能再添加商品。 +2. 点击点击上方购物车图标,查看已添加的商品,并可对购物车内的商品进行删除或者清空购物车,商品总价与购物车图标也会相应的发生变化。 +3. 退出应用再重新打开购物车时,将会展示购物车在退出前的商品信息。 + +### 约束与限制 + +本示例仅支持在标准系统上运行。 \ No newline at end of file diff --git a/data/eTSLiteStorage/build.gradle b/data/eTSLiteStorage/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..c2c8bbaed13747b913f19cef893f515bc32d02a7 --- /dev/null +++ b/data/eTSLiteStorage/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.3.4' + classpath 'com.huawei.ohos:decctest:1.2.6.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/data/eTSLiteStorage/entry/build.gradle b/data/eTSLiteStorage/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..1587dd1948941f3eaaf092ae6cae7969cb6895ff --- /dev/null +++ b/data/eTSLiteStorage/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/data/eTSLiteStorage/entry/src/main/config.json b/data/eTSLiteStorage/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..b62ad49d040dca94abed1b64f85e3fa7babb2da5 --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/config.json @@ -0,0 +1,63 @@ +{ + "app": { + "bundleName": "ohos.samples.etslitedatestorage", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etslitedatestorage", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/Login" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/ets/MainAbility/app.ets b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..bf28e58b36cf11db491d3f00768ef2c9e41d05cf --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/goods.ets b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/goods.ets new file mode 100644 index 0000000000000000000000000000000000000000..17096fc28936246aeed7e9d480c7e9ed7782fa60 --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/goods.ets @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Prompt from '@system.prompt'; +import {Good, addGood} from '../model/product' + +@Component +export struct Goods { + private good: Good + @State background: Color = Color.White + @Link cartNum: number + + build() { + Column() { + Image(this.good.img).height('55%').objectFit(ImageFit.ScaleDown) + Column() { + Text(this.good.name).fontSize(20).fontColor(Color.Blue) + Text('¥' + this.good.price).fontSize(25).fontColor(Color.Red) + } + + Row() { + Image($r('app.media.buycart')).width(20).height(20) + Text($r("app.string.insertCart")).fontSize(18).margin({ left: 3 }) + }.margin({ top: 3 }) + .backgroundColor(this.background) + .onTouch((event: TouchEvent) => { + if (event.type === TouchType.Down) { + this.background = Color.Gray + } + else if (event.type === TouchType.Up) { + this.background = Color.White + } + }) + .onClick(() => { + if (this.cartNum >= 5) { + Prompt.showToast({ message: '购物车已满' }) + } else { + this.cartNum++ + addGood(this.good) + } + }) + } + .width('100%') + .height(200) + .borderRadius(10) + .padding(10) + } +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/shopcart.ets b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/shopcart.ets new file mode 100644 index 0000000000000000000000000000000000000000..930308c87a31541bfc9d0e2a375b75649930da0e --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/common/shopcart.ets @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Good, deleteGood, clearGoodShop, getShopCart} from '../model/product' + +@Component +export struct ShoppingCart { + @State editFlag: boolean= false + @Link cartNum: number + @State shopList: Array = getShopCart() + @State priceCount: number = 0 + @Link show: boolean + + aboutToAppear() { + this.cartNum = this.shopList.length + this.getPriceCount() + } + + getPriceCount() { + console.info('getPriceCount') + this.priceCount = 0 + for (let i = 0;i < this.shopList.length; i++) { + this.priceCount += this.shopList[i].price + } + } + + build() { + Column() { + Image($r('app.media.close')) + .width(40) + .objectFit(ImageFit.ScaleDown) + .alignSelf(ItemAlign.Start) + .margin({ left: 5, top: 5 }) + .onClick(() => { + this.show = !this.show + }) + + Row() { + Text($r("app.string.myCarshopping")) + .width('100%') + .fontSize(25) + .layoutWeight(3) + .textAlign(TextAlign.Center) + .fontColor(Color.Black) + .fontWeight(FontWeight.Bold) + Text($r("app.string.deleteGoods")) + .fontSize(18) + .layoutWeight(2) + .textAlign(TextAlign.Center) + .fontColor('#0D9FFB') + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.editFlag = !this.editFlag + }) + Text($r("app.string.clearGoods")) + .fontSize(18) + .layoutWeight(2) + .textAlign(TextAlign.Center) + .fontColor('#0D9FFB') + .fontWeight(FontWeight.Bold) + .onClick(() => { + this.cartNum = 0 + this.priceCount = 0 + this.shopList = [] + this.show = false + this.show = true + clearGoodShop() + }) + }.width('100%').height(35) + + List() { + ForEach(this.shopList, item => { + ListItem() { + Row() { + Text(item.name) + .width('50%') + .fontSize(18) + .textAlign(TextAlign.Center) + Text('¥' + item.price) + .width('50%') + .fontSize(18) + .textAlign(TextAlign.Center) + .fontColor(Color.Red) + } + }.editable(true) + }, item => item.name) + } + .padding({ right: 10 }) + .edgeEffect(EdgeEffect.None) // 滑动到边缘无效果 + .editMode(this.editFlag) + .onItemDelete((index: number) => { + deleteGood(this.shopList[index]) + this.shopList = getShopCart() + this.cartNum-- + this.getPriceCount() + this.editFlag = false + return true + }) + .width('100%') + .height(85) + + Row() { + Text($r("app.string.totalPrice")) + .width('50%') + .fontSize(16) + .textAlign(TextAlign.Center) + Text('¥' + this.priceCount) + .width('50%') + .fontSize(16) + .textAlign(TextAlign.Center) + .fontColor(Color.Red) + }.height(35) + }.width('90%').height(200) + .backgroundColor(Color.White) + .border({ width: 2, radius: 10, color: Color.Black }) + } +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/ets/MainAbility/model/product.ets b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/model/product.ets new file mode 100644 index 0000000000000000000000000000000000000000..e65751c0c8e50fd7e2411e9843041c7e9f9fd594 --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/model/product.ets @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import featureAbility from '@ohos.ability.featureAbility'; +import dataStorage from '@ohos.data.storage'; + +export class Good { + name: string + price: number + img: Resource + + constructor(name: string, price: number, img: Resource) { + this.name = name + this.price = price + this.img = img + } +} + +export function GoodInit(): Array { + let goods = [{ name: 'MateX2', price: 17999, img: $r('app.media.product') }, + { name: 'Mate40Pro+', price: 8999, img: $r('app.media.product') }, + { name: 'P50Pro', price: 6488, img: $r('app.media.product') }, + { name: 'MatePadPro12.6', price: 6699, img: $r('app.media.product') }, + { name: 'MateBookXPro', price: 8999, img: $r('app.media.product') }, + { name: 'MateStation X', price: 11999, img: $r('app.media.product') }, + { name: 'MateStation S', price: 5199, img: $r('app.media.product') }, + { name: 'MateView', price: 4699, img: $r('app.media.product') }, + { name: 'PixLab', price: 1999, img: $r('app.media.product') }, + { name: 'WatchGT3', price: 1588, img: $r('app.media.product') }, + { name: 'Watch3Pro', price: 3299, img: $r('app.media.product') }, + { name: 'FreebudsPro', price: 1099, img: $r('app.media.product') }, + { name: 'Sound X', price: 2199, img: $r('app.media.product') }, + { name: 'V75 Super', price: 24999, img: $r('app.media.product') } + ] + return goods +} + +let shopCar: Array = [] + +export function addGood(good: Good) { + shopCar.push(good) + console.info('[storage]add good') + putStorage() +} + +export function clearGoodShop() { + shopCar = [] + if (storage !== null) { + storage.clearSync() + storage.flushSync() + } + console.info('[storage]clearGoodShop') +} + +export function deleteGood(good: Good) { + shopCar.splice(shopCar.indexOf(good), 1) + console.info('[storage]delete good') + putStorage() +} + +export function getShopCart(): Array { + console.info('[storage]getShopCart') + return shopCar +} + +let storage = null + +export function getShopCartFromStorage() { + let context = featureAbility.getContext() + let path1 + context.getOrCreateLocalDir().then((e) => { + path1 = e + '/files/storage' + storage = dataStorage.getStorageSync(path1); + let shopCarStr = storage.getSync('shopCar', 'test') + console.log('[storage]storage shopCar =' + JSON.stringify(shopCarStr)) + if (shopCarStr === '') { + shopCar = [] + return + } + shopCar = transStrToNoteModel(shopCarStr) + }) +} + +export function transStrToGoodsModel(str: string): Array { + console.log('[storage]transStrToNoteModel =' + str) + let goodList: Array = [] + var goods = JSON.parse(str) + console.info('[storage]notes.length = ' + goods.length) + for (var good in goods) { + goodList.push({ name: goods[good].name, price: goods[good].price, img: goods[good].img }) + } + return goodList +} + +export function putStorage() { + console.log('[Storage]putStorage') + if (storage !== null) { + storage.putSync('shopCar', JSON.stringify(shopCar)) + console.info('[Storage]put shopCar') + storage.flushSync() + let shopCarStr = storage.getSync('shopCar', 'test') + } +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/ets/MainAbility/pages/Login.ets b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/pages/Login.ets new file mode 100644 index 0000000000000000000000000000000000000000..f8ff4b0b2de44c993dd3b53379eccee0b6496db1 --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/ets/MainAbility/pages/Login.ets @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Goods} from '../common/goods.ets' +import {ShoppingCart} from '../common/shopcart.ets' +import {Good, GoodInit, getShopCartFromStorage, getShopCart} from '../model/product' + +@Entry +@Component +struct Shopping { + private goodsList: Array + @State cartNum: number = 0 + @State show: boolean = false + @State editFlag: boolean = false + + aboutToAppear() { + getShopCartFromStorage() + this.goodsList = GoodInit() + console.info('goodsList' + this.goodsList.length) + setTimeout(() => { + this.cartNum = getShopCart().length + }, 500) + } + + build() { + Stack({ alignContent: Alignment.Center }) { + Column() { + Row() { + Text('eTSLiteStorage') + .textAlign(TextAlign.Start) + .layoutWeight(7) + .fontSize(30) + .fontColor(Color.White) + .padding({ left: 10, top: 10 }) + + Badge({ + value: this.cartNum.toString(), + maxCount: 5, + position: 0, + style: { color: Color.Black, fontSize: 16, badgeSize: 20, badgeColor: Color.Red } + }) { + Button('message') { + if (this.cartNum >= 5) { + Image($r('app.media.cartfull')).width(40).height(40) + } else { + Image($r('app.media.cartnone')).width(40).height(40) + } + } + .onClick(() => { + this.show = !this.show + }) + .backgroundColor('#0D9FFB') + .width(50) + } + .height(50) + .margin({ top: 10, right: 5 }) + .layoutWeight(2) + }.width('100%') + .height(50) + .backgroundColor('#0D9FFB') + + Grid() { + ForEach(this.goodsList, item => { + GridItem() { + Goods({ good: item, cartNum: $cartNum }) + } + }, item => item.name) + } + .columnsTemplate('1fr 1fr') + .columnsGap(10) + .rowsGap(10) + .padding(10) + .margin({ bottom: 50 }) + + }.height('100%') + + if (this.show) { + ShoppingCart({ cartNum: $cartNum, show: $show }) + } + }.width('100%').height('100%').backgroundColor(0xFFFFFF) + } +} diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/element/string.json b/data/eTSLiteStorage/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1049a4360e388d760b02dc4904b93b636190aaca --- /dev/null +++ b/data/eTSLiteStorage/entry/src/main/resources/base/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSLiteDataStorage" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "description_main", + "value": "ETS_Empty Ability" + }, + { + "name": "entry_main", + "value": "entry_main" + }, + { + "name": "insertCart", + "value": "加入购物车" + }, + { + "name": "myCarshopping", + "value": "我的购物车" + }, + { + "name": "deleteGoods", + "value": "删除宝贝" + }, + { + "name": "clearGoods", + "value": "清空宝贝" + }, + { + "name": "totalPrice", + "value": "总价" + } + ] +} \ No newline at end of file diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/buycart.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/buycart.png new file mode 100644 index 0000000000000000000000000000000000000000..dd52a6aba5b0e9778c58af46a6a40651f4b15476 Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/buycart.png differ diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/cartfull.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/cartfull.png new file mode 100644 index 0000000000000000000000000000000000000000..cac156354bcaa6b29dcaaf822a0b2bd2151dd541 Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/cartfull.png differ diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/cartnone.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/cartnone.png new file mode 100644 index 0000000000000000000000000000000000000000..e1e73aad0fa0101a0bfe8933c23f702234f07fa7 Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/cartnone.png differ diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/close.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/close.png new file mode 100644 index 0000000000000000000000000000000000000000..42452d984b8f4b67e8647f16d0e0242501379ff7 Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/close.png differ diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/icon.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/icon.png differ diff --git a/data/eTSLiteStorage/entry/src/main/resources/base/media/product.png b/data/eTSLiteStorage/entry/src/main/resources/base/media/product.png new file mode 100644 index 0000000000000000000000000000000000000000..a48b8d3aa7dc785de43c21e7cfbe547a2c659f71 Binary files /dev/null and b/data/eTSLiteStorage/entry/src/main/resources/base/media/product.png differ diff --git a/data/eTSLiteStorage/screenshot/device/main.png b/data/eTSLiteStorage/screenshot/device/main.png new file mode 100644 index 0000000000000000000000000000000000000000..27853bfe1286879415c9a31ad85689cce9622013 Binary files /dev/null and b/data/eTSLiteStorage/screenshot/device/main.png differ diff --git a/data/eTSLiteStorage/screenshot/device/shopcart.png b/data/eTSLiteStorage/screenshot/device/shopcart.png new file mode 100644 index 0000000000000000000000000000000000000000..45062d3a8b4152c5c4b7498c8463942c1581761e Binary files /dev/null and b/data/eTSLiteStorage/screenshot/device/shopcart.png differ diff --git a/data/eTSLiteStorage/settings.gradle b/data/eTSLiteStorage/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/data/eTSLiteStorage/settings.gradle @@ -0,0 +1 @@ +include ':entry'