feat: input;

feat: log;
feat: List control
This commit is contained in:
Cheliangzhao 2024-09-18 19:18:18 +08:00
parent b9cd46db5e
commit 32dfd20454
13 changed files with 209 additions and 20 deletions

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "bg_01",
"value": "#F2F3F5"
}
]
}

View File

@ -1,6 +1,19 @@
{ {
"app": { "app": {
"signingConfigs": [ "signingConfigs": [
{
"name": "default",
"type": "HarmonyOS",
"material": {
"certpath": "C:\\Users\\wps\\.ohos\\config\\default_himi_ZikZBgBxljB_K_Bxk-D2cAAnn7PtvbqmLsGmCt8Tywc=.cer",
"storePassword": "0000001B9A5987C8C2ED4361B7927D584652F717FF7936EC3310D0B9BCEEA9B1E3485C8480FF92C40CA86E",
"keyAlias": "debugKey",
"keyPassword": "0000001BC10C705923EBF5BFF9E770159DFBE2DD5E92BD7DCC5F78CCF2629A20BA2BD3177C275B83ADB93F",
"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

@ -6,18 +6,28 @@ package ohos_app_cangjie_entry.components.chat
internal import ohos.component.* internal import ohos.component.*
internal import ohos.state_manage.* internal import ohos.state_manage.*
internal import ohos.base.* internal import ohos.base.*
import std.collection.*
import ohos.state_macro_manage.* import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.mock.* import ohos_app_cangjie_entry.mock.*
import ohos_app_cangjie_entry.types.ChatData import ohos_app_cangjie_entry.types.ChatData
import ohos_app_cangjie_entry.utils.Logger
import ohos_app_cangjie_entry.view_model.ChatListControl
@Component @Component
public class ChatList { public class ChatList {
let TAG: String = "ChatList"
@Prop @Prop
var datasource: ObservedArray<ChatData> var datasource: ArrayList<ChatData>
let scrollerForList = Scroller()
protected func aboutToAppear() {
Logger(TAG).info("About to Appear")
ChatListControl().setScrollerForList(this.scrollerForList)
}
func build() { func build() {
Column { Column {
List(space: 20, initialIndex: 0) { List(space: 20, initialIndex: Int32(this.datasource.size - 1), scroller: this.scrollerForList) {
ForEach( ForEach(
this.datasource, this.datasource,
itemGeneratorFunc: { itemGeneratorFunc: {
@ -27,7 +37,7 @@ public class ChatList {
source: item.source, source: item.source,
) )
} }
} },
) )
} }
}.height(100.percent).width(100.percent).padding(top: 5.vp) }.height(100.percent).width(100.percent).padding(top: 5.vp)

View File

@ -19,6 +19,9 @@ public enum EChatSource {
} }
} }
/**
* 只限定UI
**/
@Component @Component
public class ChatMessage { public class ChatMessage {
@Prop @Prop

View File

@ -7,12 +7,60 @@ internal import ohos.base.*
internal import ohos.component.* 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.utils.Logger
import ohos_app_cangjie_entry.store.ChatStore
import ohos_app_cangjie_entry.types.ChatData
import ohos_app_cangjie_entry.view_model.ChatListControl
@Component @Component
public class HomeBottom { public class HomeBottom {
let TAG: String = "HomeBottom"
@State
var isPedding: Bool = false
@State
var inputText: String = ""
let inputPlaceHolder: String = "有什么问题尽管问我"
let inputController = TextInputController()
protected func aboutToAppear() {
Logger(TAG).info("About to appear.")
}
func submitInput() {
if (this.inputText == "") {
return
}
if (this.isPedding) {
// 处理正在等待的情况
return
}
this.isPedding = true
ChatStore().add(
ChatData(
id: ChatStore().genID(),
message: this.inputText,
source: 'ME',
)
)
this.inputText = ""
this.isPedding = false
ChatListControl().toBottom()
}
func build() { func build() {
Row { Row {
Text("HomeBottom") TextInput(
text: this.inputText,
placeholder: this.inputPlaceHolder,
controller: this.inputController,
).onChange {
value => this.inputText = value
}.onSubmit {
_ => this.submitInput()
}
}.width(100.percent) }.width(100.percent)
} }
} }

View File

@ -6,15 +6,28 @@ package ohos_app_cangjie_entry.components.home
internal import ohos.base.* internal import ohos.base.*
internal import ohos.component.* internal import ohos.component.*
internal import ohos.state_manage.* internal import ohos.state_manage.*
import ohos_app_cangjie_entry.utils.Logger
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.mock.*
import ohos_app_cangjie_entry.types.ChatData import ohos_app_cangjie_entry.types.ChatData
import std.collection.*
import ohos_app_cangjie_entry.store.ChatStore
import ohos_app_cangjie_entry.view_model.ChatListControl
@Component @Component
public class HomeMain { public class HomeMain {
let TAG: String = "HomeMain"
@State @State
var mockData: ObservedArray<ChatData> = ObservedArray<ChatData>(genMockData()) var mockData: ArrayList<ChatData> = ChatStore().use()
let chatListController = ChatListControl()
protected func aboutToAppear(): Unit {
Logger(TAG).info("About to appear.")
ChatStore().register(this.TAG) {
this.mockData = ChatStore().use()
}
}
func build() { func build() {
Column { Column {

View File

@ -19,6 +19,6 @@ class MyView {
func build() { func build() {
Row { Row {
HomePage() HomePage()
}.height(100.percent).width(100.percent) }.height(100.percent).width(100.percent).backgroundColor(@r(app.color.bg_01))
} }
} }

View File

@ -11,6 +11,7 @@ public func genMockData() {
20, 20,
{ {
index: Int => ChatData( index: Int => ChatData(
id: index.toString(),
message: "teststesstasdasdasdadasdasdsaesrfsfdfsfdsf", message: "teststesstasdasdasdadasdasdsaesrfsfdfsfdsf",
source: getSource(index) source: getSource(index)
) )

View File

@ -9,16 +9,20 @@ import ohos.state_macro_manage.*
internal import ohos.component.* internal import ohos.component.*
internal import ohos.state_manage.* internal import ohos.state_manage.*
internal import ohos.base.* internal import ohos.base.*
import ohos_app_cangjie_entry.store.ChatStore
@Component @Component
public class HomePage { public class HomePage {
private let TAG = "HomePage" private let TAG = "HomePage"
protected func aboutToAppear() { protected func aboutToAppear() {
ChatStore().initStore()
} }
func build() { func build() {
Column(10) { Column(10) {
HomeTitle() HomeTitle()
HomeMain() Column {
HomeMain()
}.layoutWeight(1)
HomeBottom() HomeBottom()
}.width(100.percent).height(100.percent) }.width(100.percent).height(100.percent)
} }

View File

@ -8,17 +8,62 @@ internal import ohos.base.*
import ohos.state_macro_manage.* import ohos.state_macro_manage.*
import ohos_app_cangjie_entry.types.ChatData import ohos_app_cangjie_entry.types.ChatData
import ohos_app_cangjie_entry.mock.genMockData import ohos_app_cangjie_entry.mock.genMockData
import std.collection.*
import ohos_app_cangjie_entry.utils.Logger
@Observed
public class ChatStore { public class ChatStore {
@Publish private let TAG: String = "ChatStore"
private var chatDataList: ObservedArray<ChatData> = ObservedArray<ChatData>() private static let chatDataList: ArrayList<ChatData> = ArrayList<ChatData>()
private static let observers: HashMap<String, () -> Unit> = HashMap<String, () -> Unit>()
func initChatStore() { public func initStore() {
this.chatDataList = ObservedArray<ChatData>(genMockData()) ChatStore.chatDataList.appendAll(genMockData())
} }
func getChatDataList() { public func genID() {
return this.chatDataList return chatDataList.size.toString()
}
public func len() {
return chatDataList.size
}
public func use() {
return ChatStore.chatDataList.clone()
}
public func resetStore() {
ChatStore.chatDataList.clear()
}
public func register(id: String, action: () -> Unit) {
if (observers.contains(id)) {
Logger(TAG).warn("this id: ${id} is existed.")
}
ChatStore.observers[id] = action
return this
}
public func unregister(id: String) {
ChatStore.observers.remove(id)
return this
}
public func add(chatData: ChatData) {
chatDataList.append(chatData)
this.onUpdate()
return this
}
public func remove(chatData: ChatData) {
chatDataList.removeIf {item => item == chatData}
this.onUpdate()
return this
}
private func onUpdate() {
ChatStore.observers.values().iterator().forEach {
action => action()
}
} }
} }

View File

@ -3,6 +3,12 @@
*/ */
package ohos_app_cangjie_entry.types package ohos_app_cangjie_entry.types
public struct ChatData { public class ChatData {
public ChatData(public let message!: String, public let source!: String) {} public ChatData(public let id!: String, public let message!: String, public let source!: String) {}
public operator func ==(right: ChatData) {
return (this.id == right.id
&& this.message == right.message
&& this.source == right.source)
}
} }

View File

@ -3,16 +3,26 @@
*/ */
package ohos_app_cangjie_entry.utils package ohos_app_cangjie_entry.utils
internal import ohos.base.*
public class Logger { public class Logger {
private let TAG: String private let TAG: String
public init(TAG: String) { public init(TAG: String) {
this.TAG = TAG + ": " this.TAG = TAG + ": "
} }
public func debug(msg: String) {} public func debug(msg: String) {
AppLog.debug(this.TAG + msg)
}
public func error(msg: String) {} public func error(msg: String) {
AppLog.error(this.TAG + msg)
}
public func info(msg: String) {} public func info(msg: String) {
AppLog.info(this.TAG + msg)
}
public func warn(msg: String) {} public func warn(msg: String) {
AppLog.warn(this.TAG + msg)
}
} }

View File

@ -0,0 +1,28 @@
/**
* Created on 2024/9/18
*/
package ohos_app_cangjie_entry.view_model
internal import ohos.component.Scroller
import ohos_app_cangjie_entry.store.ChatStore
import ohos_app_cangjie_entry.utils.Logger
public class ChatListControl {
private let TAG: String = "ChatListControl"
private static var scrollerForList: ?Scroller = None
private let chatStore = ChatStore()
public func setScrollerForList(scroller: Scroller) {
scrollerForList = scroller
}
public func toBottom(smooth!: Bool = false): Unit {
Logger(TAG).info("toBottom")
scrollerForList?.scrollToIndex(Int32(this.chatStore.len() - 1), smooth: smooth)
}
public func toTop(smooth!: Bool = false): Unit {
Logger(TAG).info("toTop")
scrollerForList?.scrollToIndex(0, smooth: smooth)
}
}