0

I have two fragments. One activity which is ideal, I use nav Host to navigate from 1st fragment to another. I do not have a database as of now. I make a call in the repository using retrofit which returns the result as OutPut which is sealed class, I got a result of API call into first fragment ViewModel and I can observe it in first fragment. Now how do I send that to Second fragment or second ViewModel. what is the best solution here? I do not want to implement the database. I do not want to make another call by creating a repository for the second ViewModel and call the same method. I also want to observe any changes in list which I can do by DiffUtil if am I correct? What is the best solution in this case? Below is my code. How can I send wordResponse live data in the second fragment adapter and also observe changes.

My Repository

class DictionaryRepository internal constructor(private val networkService: NetworkService) {


companion object {
    @Volatile
    private var dictionaryRepoInstance: DictionaryRepository? = null

    fun getInstance(dictionaryService: NetworkService) =
        dictionaryRepoInstance ?: synchronized(this) {
            dictionaryRepoInstance
                ?: DictionaryRepository(dictionaryService).also { dictionaryRepoInstance = it }
        }
}

/**
 * Fetch a new searched word from the network
 */
suspend fun fetchRecentSearchedWord(term: CharSequence) = try {
    val response = networkService.retrofitClient().makeCallForWordDefinition(term)
    OutputResult.Success(response.list)
} catch (t: Throwable) {
    OutputResult.Error(t)
}

}

MyViewModel

class SearchFragmentViewModel internal constructor(
private val dictionaryRepository: DictionaryRepository) : ViewModel() {

/** Show a loading spinner if true*/
private val _spinner = MutableLiveData(false)
val spinner: LiveData<Boolean> get() = _spinner

/**take the data into the result live data*/
private val _wordResponse = MutableLiveData<OutputResult>()
val wordResponse: LiveData<OutputResult> get() = _wordResponse


fun makeAPICallWithSuspendFunction(term: CharSequence) {
    _spinner.value = true
    viewModelScope.launch(Dispatchers.Main) {

        val result = dictionaryRepository.fetchRecentSearchedWord(term)

        _wordResponse.value = when (result) {
            is OutputResult.Success -> {
                OutputResult.Success(result.output)
            }
            is OutputResult.Error -> {
                OutputResult.Error(result.throwable)
            }
        }
    }
    _spinner.value = false
}

}

K P
  • 182
  • 9
  • 1
    You can use a shared ViewModel scoped to the activity lifecycle. See https://developer.android.com/topic/libraries/architecture/viewmodel#sharing. Also, the question you've provides details which are not useful and you haven't provided any code. See how to ask a good question https://stackoverflow.com/help/how-to-ask. – EraftYps Jun 19 '20 at 04:02
  • @EraftYps I have edited. I didn't paste it as it was pretty straightforward. – K P Jun 19 '20 at 04:10
  • 1
    try to refer https://codinginflow.com/tutorials/android/fragment-to-fragment-communication-with-shared-viewmodel – APP Jun 19 '20 at 06:35

0 Answers0