-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2222 from PratyushSingh07/help-fragment-mvvm
feat: migrated help fragment to mvvm
- Loading branch information
Showing
4 changed files
with
151 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.mifos.mobile.utils | ||
|
||
import org.mifos.mobile.models.FAQ | ||
|
||
sealed class HelpUiState { | ||
data class ShowFaq(val faqArrayList: ArrayList<FAQ?>) : HelpUiState() | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/org/mifos/mobile/viewModels/HelpViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.mifos.mobile.viewModels | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import org.mifos.mobile.models.FAQ | ||
import org.mifos.mobile.utils.HelpUiState | ||
import java.util.* | ||
import javax.inject.Inject | ||
import kotlin.collections.ArrayList | ||
|
||
@HiltViewModel | ||
class HelpViewModel @Inject constructor() : ViewModel() { | ||
|
||
private val _helpUiState = MutableLiveData<HelpUiState>() | ||
val helpUiState: LiveData<HelpUiState> get() = _helpUiState | ||
|
||
fun loadFaq(qs: Array<String>?, ans: Array<String>?) { | ||
val faqArrayList = ArrayList<FAQ?>() | ||
if (qs != null) { | ||
for (i in qs.indices) { | ||
faqArrayList.add(FAQ(qs[i], ans?.get(i))) | ||
} | ||
} | ||
_helpUiState.value = HelpUiState.ShowFaq(faqArrayList) | ||
} | ||
|
||
fun filterList(faqArrayList: ArrayList<FAQ?>?, query: String): ArrayList<FAQ?> { | ||
val filteredList = ArrayList<FAQ?>() | ||
if (faqArrayList != null) { | ||
for (faq in faqArrayList) { | ||
if (faq?.question?.lowercase(Locale.ROOT) | ||
?.contains(query.lowercase(Locale.ROOT)) == true | ||
) { | ||
filteredList.add(faq) | ||
} | ||
} | ||
} | ||
return filteredList | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/src/test/java/org/mifos/mobile/viewModels/HelpViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.mifos.mobile.viewModels | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.lifecycle.Observer | ||
import junit.framework.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mifos.mobile.models.FAQ | ||
import org.mifos.mobile.utils.HelpUiState | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.* | ||
import org.mockito.junit.MockitoJUnitRunner | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class HelpViewModelTest { | ||
|
||
@get:Rule | ||
val instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Mock | ||
private lateinit var mockFAQArrayList: ArrayList<FAQ?> | ||
|
||
@Mock | ||
private lateinit var helpUiStateObserver: Observer<HelpUiState> | ||
|
||
private lateinit var viewModel: HelpViewModel | ||
|
||
@Before | ||
fun setUp() { | ||
viewModel = HelpViewModel() | ||
viewModel.helpUiState.observeForever(helpUiStateObserver) | ||
} | ||
|
||
@Test | ||
fun testLoadFaq() { | ||
val qs = arrayOf("Question1", "Question2") | ||
val ans = arrayOf("Answer1", "Answer2") | ||
|
||
viewModel.loadFaq(qs, ans) | ||
|
||
verify(helpUiStateObserver).onChanged( | ||
HelpUiState.ShowFaq( | ||
arrayListOf( | ||
FAQ( | ||
"Question1", | ||
"Answer1", | ||
false | ||
), | ||
FAQ( | ||
"Question2", | ||
"Answer2", | ||
false | ||
) | ||
) | ||
) | ||
) | ||
verifyNoMoreInteractions(helpUiStateObserver) | ||
} | ||
|
||
@Test | ||
fun testFilterList() { | ||
val query = "app" | ||
val mockFAQ1 = mock(FAQ::class.java) | ||
val mockFAQ2 = mock(FAQ::class.java) | ||
|
||
`when`(mockFAQ1.question).thenReturn("How to use the app?") | ||
`when`(mockFAQ2.question).thenReturn("Is there a user guide available?") | ||
|
||
val filteredList = viewModel.filterList(arrayListOf(mockFAQ1,mockFAQ2), query) | ||
|
||
assertEquals(mockFAQ1, filteredList[0]) | ||
} | ||
} |