diff --git a/README.md b/README.md index b498ce8..f57aee3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # Template -database \ No newline at end of file +## database + +注意事项 + +1. 由于`constructor()`函数不能异步,因此在其中调用`getRdbStore()` + 函数时,不能保证新建对象后,数据库初始化完毕。那么此时如果调用`insert`等方法时,`RdbStore`可能还是`undefined`,导致数据库操作失败。 + + 因此,只能使用回调来处理。 + +```typescript + aboutToAppear() { + this.todoTable.getRdbStore(async () => { + const res = await this.todoTable.query(); + this.todoList = res; + }) +} +``` + diff --git a/build-profile.json5 b/build-profile.json5 index a352b70..bdef519 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,6 +1,20 @@ { "app": { - "signingConfigs": [], + "signingConfigs": [ + { + "name": "default", + "type": "HarmonyOS", + "material": { + "certpath": "C:\\Users\\Administrator\\.ohos\\config\\auto_debug_harmony4-template_com.example.myapplication_70086000144137060.cer", + "storePassword": "0000001BF0C7503EEB99D5360C10359536D3353B1F2C39787692D0526594DC53D8A9103366AB1B58CACBB6", + "keyAlias": "debugKey", + "keyPassword": "0000001BFE46D113BE907F9B0F067070188C870CD7DD7AF4E6CB9720F5CC73389CC6DE77567D43876B55FB", + "profile": "C:\\Users\\Administrator\\.ohos\\config\\auto_debug_harmony4-template_com.example.myapplication_70086000144137060.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "C:\\Users\\Administrator\\.ohos\\config\\auto_debug_harmony4-template_com.example.myapplication_70086000144137060.p12" + } + } + ], "products": [ { "name": "default", diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 248c3b7..225946c 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -1,10 +1,10 @@ { + "license": "", + "devDependencies": {}, + "author": "", "name": "entry", - "version": "1.0.0", "description": "Please describe the basic information.", "main": "", - "author": "", - "license": "", + "version": "1.0.0", "dependencies": {} } - diff --git a/entry/src/main/ets/common/database/Rdb.ets b/entry/src/main/ets/common/database/Rdb.ets index cecd72b..1571030 100644 --- a/entry/src/main/ets/common/database/Rdb.ets +++ b/entry/src/main/ets/common/database/Rdb.ets @@ -1,4 +1,5 @@ import relationalStore from '@ohos.data.relationalStore'; +import { Logger } from '../../utils/Logger'; import { DatabaseConfig } from '../config'; export default class Rdb { @@ -14,16 +15,17 @@ export default class Rdb { } - async getRdbStore(callback) { + getRdbStore(callback) { const context = getContext(this) - try { - const store = await relationalStore.getRdbStore(context, DatabaseConfig.STORE_CONFIG) + relationalStore.getRdbStore(context, DatabaseConfig.STORE_CONFIG, (err, store) => { + if (err) { + console.error('getRdbStore() failed, error: ' + err) + return + } this.rdbStore = store; console.info('getRdbStore() finished.'); callback(store) - } catch (err) { - console.error('getRdbStore() failed, error: ' + err) - } + }) } async insertData(data) { @@ -55,6 +57,7 @@ export default class Rdb { try { const rows = await this.rdbStore.delete(predicates); console.info('deleteData() finished: ' + rows); + return rows; } catch (err) { console.error('deleteData() failed, err: ' + err); diff --git a/entry/src/main/ets/common/database/tables/TodoTable.ets b/entry/src/main/ets/common/database/tables/TodoTable.ets index e2e0d55..a62d489 100644 --- a/entry/src/main/ets/common/database/tables/TodoTable.ets +++ b/entry/src/main/ets/common/database/tables/TodoTable.ets @@ -15,7 +15,7 @@ export default class TodoTable { this.getRdbStore(callback) } - private getRdbStore(callback) { + getRdbStore(callback) { // 假设当前数据库版本为3 this.todoTable.getRdbStore(store => { // 当数据库创建时,数据库版本默认为0 @@ -35,9 +35,10 @@ export default class TodoTable { store.executeSql(DatabaseConfig.TODO_TABLE.update2to3); store.version = 3; } + + if (callback) callback() }) - if (callback) callback() } async insertData(data: TodoData) { @@ -59,9 +60,14 @@ export default class TodoTable { return await this.todoTable.deleteData(predicates); } - async query(content: string) { + async query(content: string = ''): Promise { const predicates = new relationalStore.RdbPredicates(DatabaseConfig.TODO_TABLE.tableName) - predicates.contains('content', content) + + if (content) predicates.contains('content', content) + else { + predicates.glob('content', '*') + } + return await this.todoTable.query(predicates, (_, resSet: relationalStore.ResultSet): TodoData => { return { diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 21bb7d5..24b5cfa 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -19,7 +19,7 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.loadContent('pages/Index', (err, data) => { + windowStage.loadContent('pages/TodoPage', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; diff --git a/entry/src/main/ets/pages/TodoPage.ets b/entry/src/main/ets/pages/TodoPage.ets index f1d22e9..01307a9 100644 --- a/entry/src/main/ets/pages/TodoPage.ets +++ b/entry/src/main/ets/pages/TodoPage.ets @@ -1,19 +1,161 @@ /** * Database apply demo */ +import TodoTable from '../common/database/tables/TodoTable'; +import { TodoData } from '../common/types/Todo'; +import { Logger } from '../utils/Logger'; + + +@Preview @Entry @Component -struct Todo { - @State message: string = "This a Template Project" +struct TodoPage { + @State searchText: string = '' + @State isInput: boolean = false + @State isEdit: boolean = false + @State todoList: TodoData[] = [] + private todoTable = new TodoTable() + searchController: SearchController = new SearchController(); + addDialogController: CustomDialogController = new CustomDialogController({ + builder: AddTodoDialog({ submit: (value) => this.handleAddSubmit(value) }), + cancel: () => this.isInput = false, + }) + + aboutToAppear() { + this.todoTable.getRdbStore(async () => { + const res = await this.todoTable.query(); + this.todoList = res; + }) + } + + handleSearchSubmit(value: string) { + this.isInput = false + } + + async handleAddSubmit(value: string) { + this.isInput = false; + + const data = { + id: 0, + content: value, + status: 0, + } + const rowId = await this.todoTable.insertData(data); + data.id = rowId; + this.todoList.push(data) + } build() { - Row() { - Text(this.message) - .fontSize(40) - .fontWeight(FontWeight.Bold) + Stack() { + Column() { + Row() { + Text('Todo List') + .fontWeight(FontWeight.Bold) + .fontSize(24) + Image($rawfile('ic_public_edit.svg')) + .width(24) + .height(24) + } + .width('100%') + .justifyContent(FlexAlign.SpaceBetween) + .alignItems(VerticalAlign.Center) + .margin({ top: 12, bottom: 12 }) + + Row() { + Search({ value: this.searchText, placeholder: 'Search', controller: this.searchController }) + .onChange(value => this.searchText = value) + .onSubmit(this.handleSearchSubmit) + } + .width('100%') + + Column() { + ForEach(this.todoList, (item: TodoData) => { + TodoItem({ content: item.content }) + }) + } + .margin({ top: 12 }) + .width('100%') + .alignItems(HorizontalAlign.Start) + } + .padding({ left: 12, right: 12 }) + + if (!this.isEdit && !this.isInput) { + Button() { + Image($rawfile('add.png')) + } + .width(48) + .height(48) + .markAnchor({ x: '50%', y: '50%' }) + .position({ x: '90%', y: '95%' }) + .onClick(() => { + // this.isInput = true; + this.addDialogController.open() + }) + } + + if (this.isEdit) { + Button() { + Image($rawfile('delete.png')) + } + .width(48) + .height(48) + .markAnchor({ x: '50%', y: '50%' }) + .position({ x: '90%', y: '95%' }) + } } .height('100%') .width('100%') - .justifyContent(FlexAlign.Center) + .alignContent(Alignment.Top) } } + + +@CustomDialog +struct AddTodoDialog { + submit: (value) => void = () => { + } + @State inputValue: string = '' + controller: CustomDialogController = new CustomDialogController({ + builder: AddTodoDialog({ + submit: this.submit, + }), + }) + + build() { + Column() { + Stack() { + TextInput({ placeholder: "please input" }) + .onChange(value => this.inputValue = value) + Button("Submit") + .type(ButtonType.Normal) + .markAnchor({ x: "100%" }) + .position({ x: '100%' }) + .onClick(() => { + this.submit(this.inputValue); + this.controller.close(); + }) + } + } + } +} + + +@Preview +@Component +struct TodoItem { + @Prop content: string = 'Todo' + @Prop showSelect: boolean = false + + build() { + Row() { + Text(this.content) + .padding({ left: 8, right: 8 }) + if (this.showSelect) { + Checkbox() + } + } + .justifyContent(FlexAlign.SpaceBetween) + .width('100%') + .height(30) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/views/TodoItem.ets b/entry/src/main/ets/views/TodoItem.ets new file mode 100644 index 0000000..e69de29 diff --git a/entry/src/main/resources/rawfile/add.png b/entry/src/main/resources/rawfile/add.png new file mode 100644 index 0000000..98b9bd0 Binary files /dev/null and b/entry/src/main/resources/rawfile/add.png differ diff --git a/entry/src/main/resources/rawfile/delete.png b/entry/src/main/resources/rawfile/delete.png new file mode 100644 index 0000000..15f403e Binary files /dev/null and b/entry/src/main/resources/rawfile/delete.png differ diff --git a/entry/src/main/resources/rawfile/foods.png b/entry/src/main/resources/rawfile/foods.png new file mode 100644 index 0000000..68fc053 Binary files /dev/null and b/entry/src/main/resources/rawfile/foods.png differ diff --git a/entry/src/main/resources/rawfile/foods_selected.png b/entry/src/main/resources/rawfile/foods_selected.png new file mode 100644 index 0000000..b09ed6d Binary files /dev/null and b/entry/src/main/resources/rawfile/foods_selected.png differ diff --git a/entry/src/main/resources/rawfile/fuel.png b/entry/src/main/resources/rawfile/fuel.png new file mode 100644 index 0000000..83a7617 Binary files /dev/null and b/entry/src/main/resources/rawfile/fuel.png differ diff --git a/entry/src/main/resources/rawfile/fuel_selected.png b/entry/src/main/resources/rawfile/fuel_selected.png new file mode 100644 index 0000000..e93b0a0 Binary files /dev/null and b/entry/src/main/resources/rawfile/fuel_selected.png differ diff --git a/entry/src/main/resources/rawfile/games.png b/entry/src/main/resources/rawfile/games.png new file mode 100644 index 0000000..4edfe31 Binary files /dev/null and b/entry/src/main/resources/rawfile/games.png differ diff --git a/entry/src/main/resources/rawfile/games_selected.png b/entry/src/main/resources/rawfile/games_selected.png new file mode 100644 index 0000000..f7d4569 Binary files /dev/null and b/entry/src/main/resources/rawfile/games_selected.png differ diff --git a/entry/src/main/resources/rawfile/half.png b/entry/src/main/resources/rawfile/half.png new file mode 100644 index 0000000..f461b8b Binary files /dev/null and b/entry/src/main/resources/rawfile/half.png differ diff --git a/entry/src/main/resources/rawfile/ic_public_edit.svg b/entry/src/main/resources/rawfile/ic_public_edit.svg new file mode 100644 index 0000000..b007228 --- /dev/null +++ b/entry/src/main/resources/rawfile/ic_public_edit.svg @@ -0,0 +1,13 @@ + + + Public/ic_public_edit + + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/rawfile/ic_public_input_search.svg b/entry/src/main/resources/rawfile/ic_public_input_search.svg new file mode 100644 index 0000000..c71cd84 --- /dev/null +++ b/entry/src/main/resources/rawfile/ic_public_input_search.svg @@ -0,0 +1,18 @@ + + + Public/ic_public_input_search + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/entry/src/main/resources/rawfile/icon.png b/entry/src/main/resources/rawfile/icon.png new file mode 100644 index 0000000..ce307a8 Binary files /dev/null and b/entry/src/main/resources/rawfile/icon.png differ diff --git a/entry/src/main/resources/rawfile/icon1.png b/entry/src/main/resources/rawfile/icon1.png new file mode 100644 index 0000000..8f36e14 Binary files /dev/null and b/entry/src/main/resources/rawfile/icon1.png differ diff --git a/entry/src/main/resources/rawfile/income.png b/entry/src/main/resources/rawfile/income.png new file mode 100644 index 0000000..0d08684 Binary files /dev/null and b/entry/src/main/resources/rawfile/income.png differ diff --git a/entry/src/main/resources/rawfile/income_selected.png b/entry/src/main/resources/rawfile/income_selected.png new file mode 100644 index 0000000..5cb2674 Binary files /dev/null and b/entry/src/main/resources/rawfile/income_selected.png differ diff --git a/entry/src/main/resources/rawfile/invest.png b/entry/src/main/resources/rawfile/invest.png new file mode 100644 index 0000000..e3565e6 Binary files /dev/null and b/entry/src/main/resources/rawfile/invest.png differ diff --git a/entry/src/main/resources/rawfile/invest_selected.png b/entry/src/main/resources/rawfile/invest_selected.png new file mode 100644 index 0000000..637c043 Binary files /dev/null and b/entry/src/main/resources/rawfile/invest_selected.png differ diff --git a/entry/src/main/resources/rawfile/pets.png b/entry/src/main/resources/rawfile/pets.png new file mode 100644 index 0000000..6cd092c Binary files /dev/null and b/entry/src/main/resources/rawfile/pets.png differ diff --git a/entry/src/main/resources/rawfile/pets_selected.png b/entry/src/main/resources/rawfile/pets_selected.png new file mode 100644 index 0000000..b964f55 Binary files /dev/null and b/entry/src/main/resources/rawfile/pets_selected.png differ diff --git a/entry/src/main/resources/rawfile/snacks.png b/entry/src/main/resources/rawfile/snacks.png new file mode 100644 index 0000000..51c53e1 Binary files /dev/null and b/entry/src/main/resources/rawfile/snacks.png differ diff --git a/entry/src/main/resources/rawfile/snacks_selected.png b/entry/src/main/resources/rawfile/snacks_selected.png new file mode 100644 index 0000000..662549c Binary files /dev/null and b/entry/src/main/resources/rawfile/snacks_selected.png differ diff --git a/entry/src/main/resources/rawfile/travel.png b/entry/src/main/resources/rawfile/travel.png new file mode 100644 index 0000000..8a784e2 Binary files /dev/null and b/entry/src/main/resources/rawfile/travel.png differ diff --git a/entry/src/main/resources/rawfile/travel_selected.png b/entry/src/main/resources/rawfile/travel_selected.png new file mode 100644 index 0000000..7016109 Binary files /dev/null and b/entry/src/main/resources/rawfile/travel_selected.png differ diff --git a/oh-package.json5 b/oh-package.json5 index 1a9cfc5..f1b2443 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,13 +1,12 @@ { - "name": "myapplication", - "version": "1.0.0", - "description": "Please describe the basic information.", - "main": "", - "author": "", "license": "", - "dependencies": { - }, "devDependencies": { "@ohos/hypium": "1.0.6" }, + "author": "", + "name": "myapplication", + "description": "Please describe the basic information.", + "main": "", + "version": "1.0.0", + "dependencies": {} }