DEV Community

Cover image for Day 37. Search challenge
Kiolk
Kiolk

Posted on

Day 37. Search challenge

What I did:

I worked on integrating the sorting and searching UI with the data-fetching logic, and I tested this with articles. Additionally, I added a simple input field for entering a search request. For this task, I had a common search challenge: implementing a delay when a user inputs a word. 
Here's what my first solution looked like:

private var searchJob: Job? = null

fun onSearchTextChanged(searchText: String) {
        _searchText.value = searchText
        searchJob?.cancel()

        searchJob = screenModelScope.launch {
            delay(500)
            if (searchText.isNotEmpty()) {
                _searchState.value = emptyList()
                pagination.restart()
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

I reviewed it logically, then asked Gemini: How can I improve it? It suggested a reasonable solution:

private val searchTextChangedChannel = Channel<String>()

viewModelScope.launch {
    searchTextChangedChannel
        .debounce(500)
        .collect { searchText ->
            if (searchText.isNotEmpty()) {
                _searchResults.value = emptyList()
                pagination.restart()
            }
        }
}

fun onSearchTextChanged(searchText: String) {
    _searchText.value = searchText
    viewModelScope.launch {
        searchTextChangedChannel.send(searchText)
    }
}
Enter fullscreen mode Exit fullscreen mode

The solution seemed functional, but the channels seemed out of data; using Flow made more sense. I didn't need to introduce a separate variable for that; I could just use the search text container I set up earlier. Here's how my code looks now:

 private val _searchText: MutableStateFlow<String> =
        MutableStateFlow("")
    val searchText = _searchText.asStateFlow()

init {
        screenModelScope.launch {
            _searchText
                .debounce(DEBOUNCE_TIME)
                .collect { text ->
                    if (text.isNotEmpty()) {
                        _searchState.value = emptyList()
                        pagination.restart()
                    }
                }
        }
    }

fun onSearchTextChanged(searchText: String) {
        _searchText.value = searchText
    }
Enter fullscreen mode Exit fullscreen mode

What I will do:

If you want to join the project, just leave a comment here, or write a message in LinkedIn.

What help I'm looking for:

Designer (create design of application in Figma)
Android/KMM developer
Any other help related to the project.

My timeline:

Day 1. Deep breath and dive
Day 2. Networking layer.
Day 3. Loading of articles.
Day 4. ArticleItem.
Day 5. Localization.
Day 6. Work on Sunday.
Day 7. First week.
Day 8. Enjoying coding.
Day 9. Expect/actual.
Day 10. TODOs.
Day 11. Friday evening.
Day 12. Frustration.
Day 13. Blocker
Day 14. Monday
Day 15. Reactions
Day 16. Feed
Day 17. stringWithFormat
Day 18. Comment
Day 19. 1 percent
Day 20. A bit of progress
Day 21. Pagination
Day 22. Lottie animation
Day 23. Sorting of articles
Day 24. Step by step
Day 25. Broken endpoint?
Day 26. After party
Day 27. Burnout
Day 28. Opportunity for growth
Day 29. Hard work
Day 30. Old code
Day 31. Technical debt
Day 32. API calls
Day 33. Generic response
Day 34. Response
Day 35. Coderabbit
Day 36. What is the power of Pull Request?

See you tomorrow. 

Top comments (0)