Skip to content

Commit 3910b6d

Browse files
committed
optimize code
1 parent 9e9d29f commit 3910b6d

35 files changed

+202
-192
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ dependencies {
1717
implementation(libs.androidx.lifecycle.viewmodel.compose)
1818
implementation(libs.androidx.exifinterface)
1919
implementation(libs.kotlinx.coroutines)
20-
implementation(libs.leavesczy.matisse)
20+
implementation(libs.kotlinx.collections.immutable)
21+
implementation(libs.matisse)
2122
implementation(libs.coil.compose)
2223
implementation(libs.coil.gif)
2324
implementation(libs.coil.network.okhttp)

app/src/main/java/github/leavesczy/compose_chat/extend/ModifierExtends.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import androidx.compose.ui.graphics.Color
1313
* @Desc:
1414
* @Github:https://github.com/leavesCZY
1515
*/
16-
fun Modifier.scrim(color: Color): Modifier = drawWithContent {
17-
drawContent()
18-
drawRect(color = color)
16+
fun Modifier.scrim(color: Color): Modifier {
17+
return drawWithContent {
18+
drawContent()
19+
drawRect(color = color)
20+
}
1921
}
2022

2123
@Composable
2224
fun Modifier.clickableNoRipple(onClick: () -> Unit): Modifier {
23-
return then(
24-
other = Modifier.clickable(
25-
onClickLabel = null,
26-
indication = null,
27-
interactionSource = remember { MutableInteractionSource() },
28-
onClick = onClick
29-
)
25+
return clickable(
26+
onClickLabel = null,
27+
indication = null,
28+
interactionSource = remember { MutableInteractionSource() },
29+
onClick = onClick
3030
)
3131
}
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package github.leavesczy.compose_chat.provider
22

3-
import android.annotation.SuppressLint
43
import android.content.Context
5-
import android.os.Build
6-
import android.os.Handler
7-
import android.os.Message
84
import android.view.Gravity
95
import android.view.LayoutInflater
106
import android.widget.TextView
@@ -41,30 +37,4 @@ object ToastProvider {
4137
return toast
4238
}
4339

44-
@SuppressLint("DiscouragedPrivateApi", "PrivateApi")
45-
private fun hookToastIfNeed(toast: Toast) {
46-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
47-
val cToast = Toast::class.java
48-
val fTn = cToast.getDeclaredField("mTN")
49-
fTn.isAccessible = true
50-
val oTn = fTn.get(toast)
51-
val cTn = oTn.javaClass
52-
val fHandle = cTn.getDeclaredField("mHandler")
53-
fHandle.isAccessible = true
54-
fHandle.set(oTn, ProxyHandler(fHandle.get(oTn) as Handler))
55-
}
56-
}
57-
58-
private class ProxyHandler(private val mHandler: Handler) : Handler(mHandler.looper) {
59-
60-
override fun handleMessage(msg: Message) {
61-
try {
62-
mHandler.handleMessage(msg)
63-
} catch (e: Throwable) {
64-
e.printStackTrace()
65-
}
66-
}
67-
68-
}
69-
7040
}

app/src/main/java/github/leavesczy/compose_chat/ui/base/BaseActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ open class BaseActivity : AppCompatActivity() {
7878
}
7979
}
8080
val window = context.window
81-
window.statusBarColor = android.graphics.Color.TRANSPARENT
82-
window.navigationBarColor = android.graphics.Color.TRANSPARENT
8381
WindowInsetsControllerCompat(window, window.decorView).apply {
8482
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_DEFAULT
8583
isAppearanceLightStatusBars = systemBarsDarkIcon

app/src/main/java/github/leavesczy/compose_chat/ui/chat/ChatActivity.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.material.pullrefresh.rememberPullRefreshState
1313
import androidx.compose.material3.MaterialTheme
1414
import androidx.compose.material3.Scaffold
1515
import androidx.compose.runtime.Composable
16-
import androidx.compose.runtime.getValue
1716
import androidx.compose.ui.Alignment
1817
import androidx.compose.ui.Modifier
1918
import github.leavesczy.compose_chat.base.models.Chat
@@ -110,18 +109,16 @@ private fun ChatPage(chatViewModel: ChatViewModel, chatPageAction: ChatPageActio
110109
modifier = Modifier.fillMaxSize(),
111110
topBar = {
112111
ChatPageTopBar(
113-
title = chatPageViewState.topBarTitle.value,
112+
title = chatPageViewState.topBarTitle,
114113
chat = chatPageViewState.chat
115114
)
116115
},
117116
bottomBar = {
118117
ChatPageBottomBar(chatViewModel = chatViewModel)
119118
}
120119
) { innerPadding ->
121-
val refreshing by chatViewModel.loadMessageViewState.refreshing
122-
val loadFinish by chatViewModel.loadMessageViewState.loadFinish
123120
val pullRefreshState = rememberPullRefreshState(
124-
refreshing = refreshing,
121+
refreshing = chatViewModel.loadMessageViewState.refreshing,
125122
onRefresh = {
126123
chatViewModel.loadMoreMessage()
127124
}
@@ -132,7 +129,7 @@ private fun ChatPage(chatViewModel: ChatViewModel, chatPageAction: ChatPageActio
132129
.padding(paddingValues = innerPadding)
133130
.pullRefresh(
134131
state = pullRefreshState,
135-
enabled = !loadFinish
132+
enabled = !chatViewModel.loadMessageViewState.loadFinish
136133
)
137134
) {
138135
MessagePanel(
@@ -142,7 +139,7 @@ private fun ChatPage(chatViewModel: ChatViewModel, chatPageAction: ChatPageActio
142139
PullRefreshIndicator(
143140
modifier = Modifier
144141
.align(alignment = Alignment.TopCenter),
145-
refreshing = refreshing,
142+
refreshing = chatViewModel.loadMessageViewState.refreshing,
146143
state = pullRefreshState,
147144
backgroundColor = MaterialTheme.colorScheme.onSecondaryContainer,
148145
contentColor = MaterialTheme.colorScheme.primary

app/src/main/java/github/leavesczy/compose_chat/ui/chat/GroupProfileActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private fun GroupProfilePage(
151151
viewState: GroupProfilePageViewState,
152152
action: GroupProfilePageAction
153153
) {
154-
val groupProfile = viewState.groupProfile.value
154+
val groupProfile = viewState.groupProfile
155155
if (groupProfile != null) {
156156
val listState = rememberLazyListState()
157157
val maxOffsetHeightPx = with(LocalDensity.current) {
@@ -198,7 +198,7 @@ private fun GroupProfilePage(
198198
GroupHeader(groupProfile = groupProfile)
199199
}
200200
items(
201-
items = viewState.memberList.value,
201+
items = viewState.memberList,
202202
key = {
203203
it.detail.id
204204
},

app/src/main/java/github/leavesczy/compose_chat/ui/chat/MessagePanel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fun MessagePanel(pageViewState: ChatPageViewState, chatPageAction: ChatPageActio
6969
verticalArrangement = Arrangement.spacedBy(space = 30.dp),
7070
) {
7171
items(
72-
items = pageViewState.messageList.value,
72+
items = pageViewState.messageList,
7373
key = {
7474
it.detail.msgId
7575
},

app/src/main/java/github/leavesczy/compose_chat/ui/chat/logic/ChatViewModel.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import github.leavesczy.compose_chat.ui.base.BaseViewModel
2626
import github.leavesczy.compose_chat.ui.chat.InputSelector
2727
import github.leavesczy.compose_chat.ui.logic.ComposeChat
2828
import github.leavesczy.compose_chat.utils.CompressImageUtils
29+
import kotlinx.collections.immutable.persistentListOf
30+
import kotlinx.collections.immutable.toPersistentList
2931
import kotlinx.coroutines.channels.Channel
3032
import kotlinx.coroutines.delay
3133
import kotlinx.coroutines.launch
@@ -57,21 +59,23 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
5759
}
5860
}
5961

60-
val chatPageViewState by mutableStateOf(
62+
var chatPageViewState by mutableStateOf(
6163
value = ChatPageViewState(
6264
chat = chat,
63-
topBarTitle = mutableStateOf(value = ""),
65+
topBarTitle = "",
6466
listState = LazyListState(firstVisibleItemIndex = 0, firstVisibleItemScrollOffset = 0),
65-
messageList = mutableStateOf(value = emptyList())
67+
messageList = persistentListOf()
6668
)
6769
)
70+
private set
6871

69-
val loadMessageViewState by mutableStateOf(
72+
var loadMessageViewState by mutableStateOf(
7073
value = LoadMessageViewState(
71-
refreshing = mutableStateOf(value = false),
72-
loadFinish = mutableStateOf(value = false)
74+
refreshing = false,
75+
loadFinish = false
7376
)
7477
)
78+
private set
7579

7680
var textMessageInputted by mutableStateOf(value = TextFieldValue(text = ""))
7781
private set
@@ -99,14 +103,14 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
99103
groupProvider.getGroupInfo(groupId = chat.id)?.name
100104
}
101105
} ?: ""
102-
chatPageViewState.topBarTitle.value = name
106+
chatPageViewState = chatPageViewState.copy(topBarTitle = name)
103107
}
104108
loadMoreMessage()
105109
}
106110

107111
fun loadMoreMessage() {
108112
viewModelScope.launch {
109-
loadMessageViewState.refreshing.value = true
113+
loadMessageViewState = loadMessageViewState.copy(refreshing = true)
110114
val loadResult = messageProvider.getHistoryMessage(
111115
chat = chat,
112116
lastMessage = lastMessage
@@ -121,8 +125,10 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
121125
false
122126
}
123127
}
124-
loadMessageViewState.refreshing.value = false
125-
loadMessageViewState.loadFinish.value = loadFinish
128+
loadMessageViewState = loadMessageViewState.copy(
129+
refreshing = false,
130+
loadFinish = loadFinish
131+
)
126132
}
127133
}
128134

@@ -240,7 +246,7 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
240246
}
241247
}
242248
allMessage[index] = newMessage
243-
chatPageViewState.messageList.value = allMessage.toList()
249+
chatPageViewState = chatPageViewState.copy(messageList = allMessage.toPersistentList())
244250
}
245251
}
246252

@@ -250,7 +256,7 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
250256
allMessage.add(0, TimeMessage(targetMessage = newMessage))
251257
}
252258
allMessage.add(0, newMessage)
253-
chatPageViewState.messageList.value = allMessage.toList()
259+
chatPageViewState = chatPageViewState.copy(messageList = allMessage.toPersistentList())
254260
viewModelScope.launch {
255261
delay(timeMillis = 80)
256262
chatPageViewState.listState.scrollToItem(index = 0)
@@ -276,7 +282,7 @@ class ChatViewModel(private val chat: Chat) : BaseViewModel() {
276282
filteredMsg++
277283
}
278284
}
279-
chatPageViewState.messageList.value = allMessage.toList()
285+
chatPageViewState = chatPageViewState.copy(messageList = allMessage.toPersistentList())
280286
}
281287
}
282288

app/src/main/java/github/leavesczy/compose_chat/ui/chat/logic/GroupProfileViewModel.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import github.leavesczy.compose_chat.base.models.ActionResult
88
import github.leavesczy.compose_chat.base.provider.IGroupProvider
99
import github.leavesczy.compose_chat.proxy.GroupProvider
1010
import github.leavesczy.compose_chat.ui.base.BaseViewModel
11+
import kotlinx.collections.immutable.persistentListOf
12+
import kotlinx.collections.immutable.toPersistentList
1113
import kotlinx.coroutines.async
1214
import kotlinx.coroutines.launch
1315

@@ -20,12 +22,13 @@ class GroupProfileViewModel(private val groupId: String) : BaseViewModel() {
2022

2123
private val groupProvider: IGroupProvider = GroupProvider()
2224

23-
val pageViewState by mutableStateOf(
25+
var pageViewState by mutableStateOf(
2426
value = GroupProfilePageViewState(
25-
groupProfile = mutableStateOf(value = null),
26-
memberList = mutableStateOf(value = emptyList())
27+
groupProfile = null,
28+
memberList = persistentListOf()
2729
)
2830
)
31+
private set
2932

3033
var loadingDialogVisible by mutableStateOf(value = false)
3134
private set
@@ -42,8 +45,10 @@ class GroupProfileViewModel(private val groupId: String) : BaseViewModel() {
4245
val groupProfile = groupProfileAsync.await()
4346
val memberList = memberListAsync.await()
4447
if (groupProfile != null) {
45-
pageViewState.groupProfile.value = groupProfile
46-
pageViewState.memberList.value = memberList
48+
pageViewState = pageViewState.copy(
49+
groupProfile = groupProfile,
50+
memberList = memberList.toPersistentList()
51+
)
4752
}
4853
loadingDialog(visible = false)
4954
}
@@ -52,7 +57,7 @@ class GroupProfileViewModel(private val groupId: String) : BaseViewModel() {
5257
private fun getGroupProfile() {
5358
viewModelScope.launch {
5459
groupProvider.getGroupInfo(groupId = groupId)?.let {
55-
pageViewState.groupProfile.value = it
60+
pageViewState = pageViewState.copy(groupProfile = it)
5661
}
5762
}
5863
}

app/src/main/java/github/leavesczy/compose_chat/ui/chat/logic/Models.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package github.leavesczy.compose_chat.ui.chat.logic
22

33
import androidx.compose.foundation.lazy.LazyListState
4-
import androidx.compose.runtime.MutableState
54
import androidx.compose.runtime.Stable
65
import github.leavesczy.compose_chat.base.models.Chat
76
import github.leavesczy.compose_chat.base.models.GroupMemberProfile
87
import github.leavesczy.compose_chat.base.models.GroupProfile
98
import github.leavesczy.compose_chat.base.models.Message
9+
import kotlinx.collections.immutable.PersistentList
1010

1111
/**
1212
* @Author: leavesCZY
@@ -17,14 +17,14 @@ import github.leavesczy.compose_chat.base.models.Message
1717
data class ChatPageViewState(
1818
val chat: Chat,
1919
val listState: LazyListState,
20-
val topBarTitle: MutableState<String>,
21-
val messageList: MutableState<List<Message>>
20+
val topBarTitle: String,
21+
val messageList: PersistentList<Message>
2222
)
2323

2424
@Stable
2525
data class LoadMessageViewState(
26-
val refreshing: MutableState<Boolean>,
27-
val loadFinish: MutableState<Boolean>
26+
val refreshing: Boolean,
27+
val loadFinish: Boolean
2828
)
2929

3030
@Stable
@@ -36,8 +36,8 @@ data class ChatPageAction(
3636

3737
@Stable
3838
data class GroupProfilePageViewState(
39-
val groupProfile: MutableState<GroupProfile?>,
40-
val memberList: MutableState<List<GroupMemberProfile>>
39+
val groupProfile: GroupProfile?,
40+
val memberList: PersistentList<GroupMemberProfile>
4141
)
4242

4343
@Stable

0 commit comments

Comments
 (0)