Skip to content

Commit 140b359

Browse files
committed
remove livedata usage
1 parent c72afb9 commit 140b359

File tree

5 files changed

+75
-90
lines changed

5 files changed

+75
-90
lines changed

app/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ dependencies {
8888
implementation "androidx.compose.material:material"
8989
implementation "androidx.compose.material:material-icons-extended"
9090
implementation "androidx.compose.material3:material3"
91-
implementation "androidx.compose.runtime:runtime-livedata"
9291
implementation "androidx.compose.ui:ui-text-google-fonts"
9392
implementation "androidx.compose.ui:ui-tooling-preview"
9493
debugImplementation "androidx.compose.ui:ui-tooling"
@@ -99,7 +98,6 @@ dependencies {
9998

10099
def lifecycle_version = "2.8.7"
101100
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
102-
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
103101
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
104102

105103
implementation "androidx.datastore:datastore:1.1.1"

app/src/main/java/xyz/ivaniskandar/shouko/activity/MainActivityViewModel.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import androidx.compose.ui.graphics.asImageBitmap
1111
import androidx.core.content.getSystemService
1212
import androidx.core.graphics.drawable.toBitmap
1313
import androidx.lifecycle.AndroidViewModel
14-
import androidx.lifecycle.LiveData
15-
import androidx.lifecycle.MutableLiveData
1614
import androidx.lifecycle.viewModelScope
1715
import kotlinx.coroutines.Dispatchers
1816
import kotlinx.coroutines.flow.MutableStateFlow
@@ -31,13 +29,13 @@ class MainActivityViewModel(
3129
application: Application,
3230
) : AndroidViewModel(application) {
3331
private val _isRefreshingAppsList = MutableStateFlow(true)
34-
private val _appsList = MutableLiveData<List<ApplicationItem>>()
32+
private val _appsList = MutableStateFlow<List<ApplicationItem>>(emptyList())
3533

3634
private val _isRefreshingShortcutList = MutableStateFlow(true)
37-
private val _shortcutList = MutableLiveData<List<ShortcutCreatorItem>>()
35+
private val _shortcutList = MutableStateFlow<List<ShortcutCreatorItem>>(emptyList())
3836

3937
private val _isRefreshingLinkHandlerList = MutableStateFlow(true)
40-
private val _linkHandlerList = MutableLiveData<List<LinkHandlerAppItem>>()
38+
private val _linkHandlerList = MutableStateFlow<List<LinkHandlerAppItem>>(emptyList())
4139

4240
val isRefreshingAppsList: StateFlow<Boolean>
4341
get() = _isRefreshingAppsList.asStateFlow()
@@ -49,49 +47,49 @@ class MainActivityViewModel(
4947
get() = _isRefreshingLinkHandlerList.asStateFlow()
5048

5149
/**
52-
* LiveData of list containing available launcher intents as [ApplicationItem]
50+
* Flow of list containing available launcher intents as [ApplicationItem]
5351
*
5452
* @see ApplicationItem
5553
*/
56-
val appsList: LiveData<List<ApplicationItem>>
54+
val appsList: StateFlow<List<ApplicationItem>>
5755
get() {
58-
if (_appsList.value == null) refreshAppsList()
56+
if (_appsList.value.isEmpty()) refreshAppsList()
5957
return _appsList
6058
}
6159

6260
/**
63-
* LiveData of list containing available intents to create an app shortcut as [ShortcutCreatorItem]
61+
* Flow of list containing available intents to create an app shortcut as [ShortcutCreatorItem]
6462
*
6563
* @see ShortcutCreatorItem
6664
*/
67-
val shortcutList: LiveData<List<ShortcutCreatorItem>>
65+
val shortcutList: StateFlow<List<ShortcutCreatorItem>>
6866
get() {
69-
if (_shortcutList.value == null) refreshShortcutCreatorList()
67+
if (_shortcutList.value.isEmpty()) refreshShortcutCreatorList()
7068
return _shortcutList
7169
}
7270

7371
/**
74-
* LiveData of list containing apps that can open a supported link by default as [ApplicationItem]
72+
* Flow of list containing apps that can open a supported link by default as [ApplicationItem]
7573
*/
76-
val linkHandlerList: LiveData<List<LinkHandlerAppItem>>
74+
val linkHandlerList: StateFlow<List<LinkHandlerAppItem>>
7775
@RequiresApi(Build.VERSION_CODES.S)
7876
get() {
79-
if (_linkHandlerList.value == null) refreshLinkHandlerList()
77+
if (_linkHandlerList.value.isEmpty()) refreshLinkHandlerList()
8078
return _linkHandlerList
8179
}
8280

8381
fun refreshAppsList() {
8482
viewModelScope.launch(Dispatchers.Default) {
8583
_isRefreshingAppsList.emit(true)
86-
_appsList.postValue(getAppsList())
84+
_appsList.emit(getAppsList())
8785
_isRefreshingAppsList.emit(false)
8886
}
8987
}
9088

9189
fun refreshShortcutCreatorList() {
9290
viewModelScope.launch(Dispatchers.Default) {
9391
_isRefreshingShortcutList.emit(true)
94-
_shortcutList.postValue(getShortcutCreatorList())
92+
_shortcutList.emit(getShortcutCreatorList())
9593
_isRefreshingShortcutList.emit(false)
9694
}
9795
}
@@ -100,7 +98,7 @@ class MainActivityViewModel(
10098
fun refreshLinkHandlerList() {
10199
viewModelScope.launch(Dispatchers.Default) {
102100
_isRefreshingLinkHandlerList.emit(true)
103-
_linkHandlerList.postValue(getLinkHandlerList())
101+
_linkHandlerList.emit(getLinkHandlerList())
104102
_isRefreshingLinkHandlerList.emit(false)
105103
}
106104
}

app/src/main/java/xyz/ivaniskandar/shouko/ui/destination/AndroidAppLink.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
3737
import androidx.compose.runtime.Composable
3838
import androidx.compose.runtime.collectAsState
3939
import androidx.compose.runtime.getValue
40-
import androidx.compose.runtime.livedata.observeAsState
4140
import androidx.compose.runtime.mutableStateOf
4241
import androidx.compose.runtime.remember
4342
import androidx.compose.runtime.setValue
@@ -203,7 +202,7 @@ fun LinkTargetList(
203202
modifier: Modifier = Modifier,
204203
mainViewModel: MainActivityViewModel = viewModel(),
205204
) {
206-
val items by mainViewModel.linkHandlerList.observeAsState()
205+
val items by mainViewModel.linkHandlerList.collectAsState()
207206
val isRefreshing by mainViewModel.isRefreshingLinkHandlerList.collectAsState()
208207
val state = rememberPullToRefreshState()
209208
PullToRefreshBox(
@@ -219,21 +218,19 @@ fun LinkTargetList(
219218
)
220219
},
221220
) {
222-
val filteredItems = items?.filter { if (approved) it.linkHandlingAllowed && it.isApproved else it.isUnapproved }
223-
val disabledItems = if (approved) items?.filter { !it.linkHandlingAllowed && it.isApproved } else null
221+
val filteredItems = items.filter { if (approved) it.linkHandlingAllowed && it.isApproved else it.isUnapproved }
222+
val disabledItems = if (approved) items.filter { !it.linkHandlingAllowed && it.isApproved } else null
224223

225224
LazyColumn(
226225
contentPadding = contentPadding,
227226
) {
228-
if (filteredItems != null) {
229-
items(items = filteredItems, key = { it.packageName }) { item ->
230-
LinkTargetListItem(
231-
item = item,
232-
onClick = {
233-
navController.navigate(Screen.LinkTargetInfoSheet.createRoute(item.packageName))
234-
},
235-
)
236-
}
227+
items(items = filteredItems, key = { it.packageName }) { item ->
228+
LinkTargetListItem(
229+
item = item,
230+
onClick = {
231+
navController.navigate(Screen.LinkTargetInfoSheet.createRoute(item.packageName))
232+
},
233+
)
237234
}
238235

239236
if (!disabledItems.isNullOrEmpty()) {
@@ -314,8 +311,8 @@ fun LinkTargetInfoSheet(
314311
modifier: Modifier = Modifier,
315312
mainViewModel: MainActivityViewModel = viewModel(),
316313
) {
317-
val list by mainViewModel.linkHandlerList.observeAsState()
318-
val item = list!!.find { it.packageName == packageName }!!
314+
val list by mainViewModel.linkHandlerList.collectAsState()
315+
val item = list.find { it.packageName == packageName }!!
319316
Column(
320317
modifier = modifier
321318
.fillMaxWidth()

app/src/main/java/xyz/ivaniskandar/shouko/ui/destination/AssistantButton.kt

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
2525
import androidx.compose.runtime.Composable
2626
import androidx.compose.runtime.collectAsState
2727
import androidx.compose.runtime.getValue
28-
import androidx.compose.runtime.livedata.observeAsState
2928
import androidx.compose.runtime.rememberCoroutineScope
3029
import androidx.compose.ui.Alignment
3130
import androidx.compose.ui.Modifier
@@ -139,7 +138,7 @@ fun AssistantActionSelection(
139138
) { page ->
140139
when (page) {
141140
0 -> {
142-
val items by mainViewModel.appsList.observeAsState()
141+
val items by mainViewModel.appsList.collectAsState()
143142
val isRefreshing by mainViewModel.isRefreshingAppsList.collectAsState()
144143
val state = rememberPullToRefreshState()
145144
PullToRefreshBox(
@@ -155,32 +154,30 @@ fun AssistantActionSelection(
155154
)
156155
},
157156
) {
158-
if (items != null) {
159-
LazyColumn(
160-
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
161-
) {
162-
items(items!!) { item ->
163-
ApplicationRow(
164-
item = item,
165-
onClick = {
166-
scope.launch {
167-
val intent = Intent().apply {
168-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
169-
component = it
170-
}
171-
prefs.setAssistButtonAction(IntentAction(intent))
172-
navController.popBackStack()
157+
LazyColumn(
158+
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
159+
) {
160+
items(items) { item ->
161+
ApplicationRow(
162+
item = item,
163+
onClick = {
164+
scope.launch {
165+
val intent = Intent().apply {
166+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
167+
component = it
173168
}
174-
},
175-
)
176-
}
169+
prefs.setAssistButtonAction(IntentAction(intent))
170+
navController.popBackStack()
171+
}
172+
},
173+
)
177174
}
178175
}
179176
}
180177
Spacer(modifier = Modifier.navigationBarsPadding())
181178
}
182179
1 -> {
183-
val items by mainViewModel.shortcutList.observeAsState()
180+
val items by mainViewModel.shortcutList.collectAsState()
184181
val context = LocalContext.current
185182
val createShortcut =
186183
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
@@ -213,21 +210,19 @@ fun AssistantActionSelection(
213210
)
214211
},
215212
) {
216-
if (items != null) {
217-
LazyColumn(
218-
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
219-
) {
220-
items(items!!) { item ->
221-
ShortcutCreatorRow(
222-
item = item,
223-
onClick = {
224-
val i = Intent(Intent.ACTION_CREATE_SHORTCUT).apply {
225-
component = it
226-
}
227-
createShortcut.launch(i)
228-
},
229-
)
230-
}
213+
LazyColumn(
214+
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
215+
) {
216+
items(items) { item ->
217+
ShortcutCreatorRow(
218+
item = item,
219+
onClick = {
220+
val i = Intent(Intent.ACTION_CREATE_SHORTCUT).apply {
221+
component = it
222+
}
223+
createShortcut.launch(i)
224+
},
225+
)
231226
}
232227
}
233228
}

app/src/main/java/xyz/ivaniskandar/shouko/ui/destination/LockscreenShortcut.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
1616
import androidx.compose.runtime.Composable
1717
import androidx.compose.runtime.collectAsState
1818
import androidx.compose.runtime.getValue
19-
import androidx.compose.runtime.livedata.observeAsState
2019
import androidx.compose.runtime.rememberCoroutineScope
2120
import androidx.compose.ui.Alignment
2221
import androidx.compose.ui.Modifier
@@ -109,7 +108,7 @@ fun LockscreenShortcutSelection(
109108
val context = LocalContext.current
110109
when (page) {
111110
0 -> {
112-
val items by mainViewModel.appsList.observeAsState()
111+
val items by mainViewModel.appsList.collectAsState()
113112
val isRefreshing by mainViewModel.isRefreshingAppsList.collectAsState()
114113
val state = rememberPullToRefreshState()
115114
PullToRefreshBox(
@@ -125,24 +124,22 @@ fun LockscreenShortcutSelection(
125124
)
126125
},
127126
) {
128-
if (items != null) {
129-
LazyColumn(
130-
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
131-
) {
132-
items(items!!) { item ->
133-
ApplicationRow(
134-
item = item,
135-
onClick = {
136-
scope.launch {
137-
ShoukoApplication.prefs.setLockscreenAction(
138-
key = settingsKey,
139-
value = it.flattenToString(),
140-
)
141-
navController.popBackStack()
142-
}
143-
},
144-
)
145-
}
127+
LazyColumn(
128+
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
129+
) {
130+
items(items) { item ->
131+
ApplicationRow(
132+
item = item,
133+
onClick = {
134+
scope.launch {
135+
ShoukoApplication.prefs.setLockscreenAction(
136+
key = settingsKey,
137+
value = it.flattenToString(),
138+
)
139+
navController.popBackStack()
140+
}
141+
},
142+
)
146143
}
147144
}
148145
}

0 commit comments

Comments
 (0)