feat: add data store

This commit is contained in:
clz 2024-09-15 17:20:20 +08:00
parent 71ee06965c
commit 891915b386
7 changed files with 92 additions and 29 deletions

View File

@ -1,19 +1,6 @@
{ {
"app": { "app": {
"signingConfigs": [ "signingConfigs": [
{
"name": "default",
"type": "HarmonyOS",
"material": {
"certpath": "C:\\Users\\wps\\.ohos\\config\\default_himi_ZikZBgBxljB_K_Bxk-D2cAAnn7PtvbqmLsGmCt8Tywc=.cer",
"storePassword": "0000001A324907DCD55F55682629687D2AECF782CD5EE7AFB5FD4E1CE3F410EBB601D67AC641B4072B46",
"keyAlias": "debugKey",
"keyPassword": "0000001A8EDEBEC4585D89D8F1A91107E51ECAE951E2849CB803CBA06C9E49A8A59CCB6CECAF7EA5DADA",
"profile": "C:\\Users\\wps\\.ohos\\config\\default_himi_ZikZBgBxljB_K_Bxk-D2cAAnn7PtvbqmLsGmCt8Tywc=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\wps\\.ohos\\config\\default_himi_ZikZBgBxljB_K_Bxk-D2cAAnn7PtvbqmLsGmCt8Tywc=.p12"
}
}
], ],
"products": [ "products": [
{ {

View File

@ -7,24 +7,24 @@ internal import ohos.component.*
internal import ohos.state_manage.* internal import ohos.state_manage.*
internal import ohos.base.* internal import ohos.base.*
import ohos.state_macro_manage.* import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.mock.*
import ohos_app_cangjie_entry.types.ChatData
@Component @Component
public class ChatList { public class ChatList {
let array: Array<String> = Array<String>(20, item: "teststesstasdasdasdadasdasdsaesrfsfdfsfdsf") @Prop
var datasource: ObservedArray<ChatData>
func build() { func build() {
Column { Column {
List(space: 20, initialIndex: 0) { List(space: 20, initialIndex: 0) {
ForEach( ForEach(
this.array, this.datasource,
itemGeneratorFunc: { itemGeneratorFunc: {
item: String, index: Int64 => ListItem { item: ChatData, _: Int => ListItem {
ChatMessage( ChatMessage(
message: item, message: item.message,
source: if (index % 2 == 0) { source: item.source,
EChatSource.ME
} else {
EChatSource.YOU
}
) )
} }
} }

View File

@ -14,8 +14,8 @@ public enum EChatSource {
| ME | ME
| YOU | YOU
operator func == (right: EChatSource) { operator func ==(right: EChatSource) {
// fixme: enum无法比较
} }
} }
@ -24,12 +24,24 @@ public class ChatMessage {
@Prop @Prop
var message: String var message: String
@Prop @Prop
var source: EChatSource var source: String
func upperSource() {
return source.toAsciiUpper()
}
@Builder
func matchSource() {
if (this.upperSource() == 'ME') {
ChatMessageMe(message: this.message)
} else if (this.upperSource() == 'YOU') {
ChatMessageYou(message: this.message)
} else {}
}
func build() { func build() {
if (this.source == EChatSource.ME) { Row() {
ChatMessageMe(message: this.message)} this.matchSource()
else {ChatMessageYou(message: this.message)}
} }
} }
} }

View File

@ -8,12 +8,17 @@ internal import ohos.component.*
internal import ohos.state_manage.* internal import ohos.state_manage.*
import ohos.state_macro_manage.* import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.components.chat.ChatList import ohos_app_cangjie_entry.components.chat.ChatList
import ohos_app_cangjie_entry.mock.*
import ohos_app_cangjie_entry.types.ChatData
@Component @Component
public class HomeMain { public class HomeMain {
@State
var mockData: ObservedArray<ChatData> = ObservedArray<ChatData>(genMockData())
func build() { func build() {
Column { Column {
ChatList() ChatList(datasource: this.mockData)
}.width(100.percent).height(100.percent).padding(right: 8.vp, left: 8.vp) }.width(100.percent).height(100.percent).padding(right: 8.vp, left: 8.vp)
} }
} }

View File

@ -0,0 +1,27 @@
/**
* Created on 2024/9/15
*/
package ohos_app_cangjie_entry.mock
import std.collection.*
import ohos_app_cangjie_entry.types.ChatData
public func genMockData() {
Array<ChatData>(
20,
{
index: Int => ChatData(
message: "teststesstasdasdasdadasdasdsaesrfsfdfsfdsf",
source: getSource(index)
)
}
)
}
func getSource(index: Int): String {
if (index % 2 == 0) {
'ME'
} else {
'YOU'
}
}

View File

@ -0,0 +1,24 @@
/**
* Created on 2024/9/15
*/
package ohos_app_cangjie_entry.store
internal import ohos.state_manage.*
internal import ohos.base.*
import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.types.ChatData
import ohos_app_cangjie_entry.mock.genMockData
@Observed
public class ChatStore {
@Publish
private var chatDataList: ObservedArray<ChatData> = ObservedArray<ChatData>()
func initChatStore() {
this.chatDataList = ObservedArray<ChatData>(genMockData())
}
func getChatDataList() {
return this.chatDataList
}
}

View File

@ -0,0 +1,8 @@
/**
* Created on 2024/9/15
*/
package ohos_app_cangjie_entry.types
public struct ChatData {
public ChatData(public let message!: String, public let source!: String) {}
}