Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Refactor UI components #135

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.*
import com.xinto.opencord.domain.message.DomainMessage
import com.xinto.opencord.domain.message.toDomain
import com.xinto.opencord.manager.PersistentDataManager
import com.xinto.opencord.manager.ToastManager
import com.xinto.opencord.rest.service.DiscordApiService
import com.xinto.opencord.store.GuildStore
import com.xinto.opencord.ui.viewmodel.BasePersistenceViewModel
import com.xinto.opencord.store.PersistentDataStore
import com.xinto.opencord.util.collectIn
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.launch

@Stable
class MentionsViewModel(
persistentDataManager: PersistentDataManager,
guilds: GuildStore,
persistentDataStore: PersistentDataStore,
private val toasts: ToastManager,
private val api: DiscordApiService,
) : BasePersistenceViewModel(persistentDataManager) {
) : ViewModel() {
var includeRoles by mutableStateOf(true)
private set
var includeEveryone by mutableStateOf(true)
Expand All @@ -35,6 +35,8 @@ class MentionsViewModel(
var messages by mutableStateOf(emptyFlow<PagingData<DomainMessage>>())
private set

private var guildId = 0L

fun toggleRoles() {
includeRoles = !includeRoles
initPager()
Expand All @@ -46,7 +48,7 @@ class MentionsViewModel(
}

fun toggleCurrentServer() {
if (includeAllServers && persistentGuildId <= 0) {
if (includeAllServers && guildId <= 0) {
toasts.showToast("No server currently selected!")
} else {
includeAllServers = !includeAllServers
Expand All @@ -57,14 +59,17 @@ class MentionsViewModel(
init {
initPager()

if (persistentGuildId > 0) {
viewModelScope.launch {
val guild = guilds.fetchGuild(persistentGuildId)
?: return@launch
persistentDataStore.observeCurrentGuild()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not need to be updated when the current guild changes (it won't except for a tiny edge case)

it should stay the same for as long as the screen is open

should probably add some getGuildId() or something to persistent store

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can just make it get the last value, no problems with that

.collectIn(viewModelScope) {
guildId = it

if (it <= 0) return@collectIn

val guild = guilds.fetchGuild(guildId)
?: return@collectIn

currentGuildName = guild.name
}
}
}

private fun initPager() {
Expand All @@ -76,7 +81,7 @@ class MentionsViewModel(
initialLoadSize = 25,
),
pagingSourceFactory = {
val guildId = if (!includeAllServers) persistentGuildId else null
val guildId = if (!includeAllServers) guildId else null
MentionsPagingSource(api, includeRoles, includeEveryone, guildId)
},
).flow.cachedIn(viewModelScope)
Expand Down

This file was deleted.