feat: http; refactor: chat source type
This commit is contained in:
parent
470c66a612
commit
2cf7798754
|
@ -12,6 +12,7 @@ import ohos_app_cangjie_entry.mock.*
|
|||
import ohos_app_cangjie_entry.types.ChatData
|
||||
import ohos_app_cangjie_entry.utils.Logger
|
||||
import ohos_app_cangjie_entry.view_model.ChatListControl
|
||||
import ohos_app_cangjie_entry.types.EChatSource
|
||||
|
||||
@Component
|
||||
public class ChatList {
|
||||
|
@ -34,10 +35,13 @@ public class ChatList {
|
|||
item: ChatData, _: Int => ListItem {
|
||||
ChatMessage(
|
||||
message: item.message,
|
||||
source: item.source,
|
||||
source: EChatSource.fromString(item.source),
|
||||
)
|
||||
}
|
||||
},
|
||||
keyGeneratorFunc: {
|
||||
item, _ => item.id
|
||||
}
|
||||
)
|
||||
}
|
||||
}.height(100.percent).width(100.percent).padding(top: 5.vp)
|
||||
|
|
|
@ -9,15 +9,7 @@ import ohos.state_macro_manage.*
|
|||
import ohos.state_manage.*
|
||||
import ohos.base.*
|
||||
import ohos_app_cangjie_entry.components.chat.chatMessages.*
|
||||
|
||||
public enum EChatSource {
|
||||
| ME
|
||||
| YOU
|
||||
|
||||
operator func ==(right: EChatSource) {
|
||||
// fixme: enum无法比较
|
||||
}
|
||||
}
|
||||
import ohos_app_cangjie_entry.types.EChatSource
|
||||
|
||||
/**
|
||||
* 只限定UI
|
||||
|
@ -27,17 +19,13 @@ public class ChatMessage {
|
|||
@Prop
|
||||
var message: String
|
||||
@Prop
|
||||
var source: String
|
||||
|
||||
func upperSource() {
|
||||
return source.toAsciiUpper()
|
||||
}
|
||||
var source: EChatSource
|
||||
|
||||
@Builder
|
||||
func matchSource() {
|
||||
if (this.upperSource() == 'ME') {
|
||||
if (let EChatSource.ME <- this.source) {
|
||||
ChatMessageMe(message: this.message)
|
||||
} else if (this.upperSource() == 'YOU') {
|
||||
} else if (let EChatSource.YOU <- this.source) {
|
||||
ChatMessageYou(message: this.message)
|
||||
} else {}
|
||||
}
|
||||
|
|
32
entry/src/main/cangjie/src/support/support_net/Http.cj
Normal file
32
entry/src/main/cangjie/src/support/support_net/Http.cj
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* Created on 2024/9/19
|
||||
*/
|
||||
package ohos_app_cangjie_entry.support.support_net
|
||||
|
||||
import net.http.*
|
||||
import std.time.*
|
||||
import std.sync.*
|
||||
|
||||
public enum EHttpMethod {
|
||||
| GET
|
||||
| POST
|
||||
}
|
||||
|
||||
public class Http {
|
||||
public func get(url!: String): ?String {
|
||||
let client = ClientBuilder().build()
|
||||
try {
|
||||
let response = client.get(url)
|
||||
let responseHeaders = response.headers
|
||||
let reponseBody = response.body
|
||||
let dataBuffer = Array<Byte>(8 * 1024 * 1024 * 10, item: 0)
|
||||
let dataLength = reponseBody.read(dataBuffer)
|
||||
let str = String.fromUtf8(dataBuffer[..dataLength])
|
||||
return str
|
||||
} catch (e: Error) {
|
||||
return None
|
||||
} finally {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
27
entry/src/main/cangjie/src/types/EChatSource.cj
Normal file
27
entry/src/main/cangjie/src/types/EChatSource.cj
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Created on 2024/9/19
|
||||
*/
|
||||
package ohos_app_cangjie_entry.types
|
||||
|
||||
public enum EChatSource {
|
||||
| ME
|
||||
| YOU
|
||||
| SYS
|
||||
|
||||
public func toString() {
|
||||
match (this) {
|
||||
case EChatSource.ME => 'ME'
|
||||
case EChatSource.YOU => 'YOU'
|
||||
case EChatSource.SYS => 'SYS'
|
||||
}
|
||||
}
|
||||
|
||||
public static func fromString(str: String) {
|
||||
match (str) {
|
||||
case 'ME' => EChatSource.ME
|
||||
case 'YOU' => EChatSource.YOU
|
||||
case 'SYS' => EChatSource.SYS
|
||||
case _ => EChatSource.ME
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,6 +31,11 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"requestPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.INTERNET"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"meta": {
|
||||
"stableOrder": true
|
||||
"stableOrder": false
|
||||
},
|
||||
"lockfileVersion": 3,
|
||||
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
|
||||
|
@ -9,19 +9,19 @@
|
|||
"@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19"
|
||||
},
|
||||
"packages": {
|
||||
"@ohos/hamock@1.0.0": {
|
||||
"name": "@ohos/hamock",
|
||||
"version": "1.0.0",
|
||||
"integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==",
|
||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har",
|
||||
"registryType": "ohpm"
|
||||
},
|
||||
"@ohos/hypium@1.0.19": {
|
||||
"name": "@ohos/hypium",
|
||||
"version": "1.0.19",
|
||||
"integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==",
|
||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har",
|
||||
"registryType": "ohpm"
|
||||
},
|
||||
"@ohos/hamock@1.0.0": {
|
||||
"name": "@ohos/hamock",
|
||||
"version": "1.0.0",
|
||||
"integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==",
|
||||
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har",
|
||||
"registryType": "ohpm"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user