Skip to content

Commit

Permalink
Fix community search not relaunching after process death (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
MV-GH authored Sep 27, 2024
1 parent f78d156 commit b726cb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/com/jerboa/model/CommunityListViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.jerboa.DEBOUNCE_DELAY
import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.toApiState
Expand All @@ -17,16 +18,21 @@ import it.vercruysse.lemmyapi.datatypes.Search
import it.vercruysse.lemmyapi.datatypes.SearchResponse
import it.vercruysse.lemmyapi.dto.SearchType
import it.vercruysse.lemmyapi.dto.SubscribedType
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class CommunityListViewModel(
communities: List<CommunityFollowerView>,
) : ViewModel() {
var searchRes: ApiState<SearchResponse> by mutableStateOf(ApiState.Empty)
private set
private var fetchCommunitiesJob: Job? = null

fun searchCommunities(form: Search) {
viewModelScope.launch {
fetchCommunitiesJob?.cancel()
fetchCommunitiesJob = viewModelScope.launch {
delay(DEBOUNCE_DELAY)
searchRes = ApiState.Loading
searchRes = API.getInstance().search(form).toApiState()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.jerboa.DEBOUNCE_DELAY
import com.jerboa.JerboaAppState
import com.jerboa.api.ApiState
import com.jerboa.feat.BlurNSFW
Expand All @@ -31,12 +31,8 @@ import it.vercruysse.lemmyapi.datatypes.CommunityFollowerView
import it.vercruysse.lemmyapi.datatypes.Search
import it.vercruysse.lemmyapi.dto.SearchType
import it.vercruysse.lemmyapi.dto.SortType
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

private var fetchCommunitiesJob: Job? = null

object CommunityListReturn {
const val COMMUNITY = "community-list::return(community)"
}
Expand All @@ -58,6 +54,20 @@ fun CommunityListScreen(

var search by rememberSaveable { mutableStateOf("") }

// Upon launch from process death
LaunchedEffect(Unit) {
if (search.isNotEmpty()) {
communityListViewModel.searchCommunities(
form =
Search(
q = search,
type_ = SearchType.Communities,
sort = SortType.TopAll,
),
)
}
}

val scope = rememberCoroutineScope()

val baseModifier = if (padding == null) {
Expand Down Expand Up @@ -85,19 +95,14 @@ fun CommunityListScreen(
search = search,
onSearchChange = {
search = it
fetchCommunitiesJob?.cancel()
fetchCommunitiesJob =
scope.launch {
delay(DEBOUNCE_DELAY)
communityListViewModel.searchCommunities(
form =
Search(
q = search,
type_ = SearchType.Communities,
sort = SortType.TopAll,
),
)
}
communityListViewModel.searchCommunities(
form =
Search(
q = search,
type_ = SearchType.Communities,
sort = SortType.TopAll,
),
)
},
)
},
Expand Down

0 comments on commit b726cb5

Please sign in to comment.