Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce search box input. Fixes #154 #157

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ val prettyTime = PrettyTime(Locale.getDefault())
val gson = Gson()

const val LAUNCH_DELAY = 300L
const val DEBOUNCE_DELAY = 1000L
const val MAX_POST_TITLE_LENGTH = 200

val DEFAULT_LEMMY_INSTANCES = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -14,10 +15,16 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import com.jerboa.DEBOUNCE_DELAY
import com.jerboa.db.AccountViewModel
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.community.CommunityViewModel
import com.jerboa.ui.components.community.communityClickWrapper
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

private var fetchCommunitiesJob: Job? = null

@Composable
fun CommunityListActivity(
Expand All @@ -35,21 +42,25 @@ fun CommunityListActivity(
var search by rememberSaveable { mutableStateOf("") }

val ctx = LocalContext.current
val scope = rememberCoroutineScope()

Surface(color = MaterialTheme.colors.background) {
Scaffold(
topBar = {
CommunityListHeader(
navController = navController,
search = search,
// TODO figure out how to debounce this
onSearchChange = {
search = it
communityListViewModel.searchCommunities(
query = search,
account = account,
ctx = ctx,
)
fetchCommunitiesJob?.cancel()
fetchCommunitiesJob = scope.launch {
delay(DEBOUNCE_DELAY)
communityListViewModel.searchCommunities(
query = search,
account = account,
ctx = ctx,
)
}
},
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import com.jerboa.DEBOUNCE_DELAY
import com.jerboa.api.uploadPictrsImage
import com.jerboa.db.AccountViewModel
import com.jerboa.imageInputStreamFromUri
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.community.list.CommunityListViewModel
import com.jerboa.ui.components.post.PostViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

private var fetchSuggestedTitleJob: Job? = null

@Composable
fun CreatePostActivity(
accountViewModel: AccountViewModel,
Expand Down Expand Up @@ -84,8 +89,12 @@ fun CreatePostActivity(
url = url,
onUrlChange = { cUrl ->
url = cUrl
if (Patterns.WEB_URL.matcher(cUrl).matches()) {
createPostViewModel.fetchSuggestedTitle(cUrl, ctx)
fetchSuggestedTitleJob?.cancel()
fetchSuggestedTitleJob = scope.launch {
delay(DEBOUNCE_DELAY)
if (Patterns.WEB_URL.matcher(cUrl).matches()) {
createPostViewModel.fetchSuggestedTitle(cUrl, ctx)
}
}
},
navController = navController,
Expand Down