79235865

Date: 2024-11-29 04:52:05
Score: 1
Natty:
Report link

In an MVVM architecture with a single-activity, multiple-fragment Android application, centralizing HTTP response handling and navigation while avoiding redundant code across Fragments can be achieved by leveraging the ViewModel and sealed classes for navigation events. Below is a clean and scalable approach that adheres to MVVM principles.

Create a sealed class to represent navigation events, allowing the ViewModel to communicate them to the View layer

sealed class NavigationEvent {
    object NavigateToLogin : NavigationEvent()
    object NavigateToServerError : NavigationEvent()
}

Pass navigation events to the ViewModel during API calls:

   class ExampleRepository {
    fun checkOtpCode(
        phoneNumber: String,
        code: String,
        onNavigationEvent: (NavigationEvent) -> Unit
    ) = apiCall(
        { remoteDataSource.checkOtp(phoneNumber, code) },
        onNavigationEvent
    )
}

Expose a SharedFlow or LiveData for navigation events to the Fragment:

    class ExampleViewModel(private val repository: ExampleRepository) : ViewModel() {

    private val _navigationEvent = MutableSharedFlow<NavigationEvent>()
    val navigationEvent: SharedFlow<NavigationEvent> = _navigationEvent

    fun checkOtpCode(phoneNumber: String, code: String) = repository.checkOtpCode(
        phoneNumber,
        code
    ) { event ->
        viewModelScope.launch {
            _navigationEvent.emit(event)
        }
    }
}

Collect navigation events in your Fragment and perform the navigation:

class ExampleFragment : Fragment() {

    private val viewModel: ExampleViewModel by viewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        lifecycleScope.launchWhenStarted {
            viewModel.navigationEvent.collect { event ->
                when (event) {
                    is NavigationEvent.NavigateToLogin -> findNavController().navigate(R.id.loginFragment)
                    is NavigationEvent.NavigateToServerError -> findNavController().navigate(R.id.serverErrorFragment)
                }
            }
        }

        // Api call here
        viewModel.checkOtpCode("1234567890", "1234")
    }
}

This solution adheres to MVVM by keeping navigation logic within the ViewModel, making it centralized, reusable, and testable. It avoids global state by scoping navigation events to specific ViewModel instances, ensuring clean architecture and reducing tight coupling. To further improve, shared navigation logic can be moved to a BaseViewModel, and a SingleLiveEvent can be used for one-time navigation with LiveData. This approach ensures maintainable, modular, and scalable code.

I HOPE THIS IS HELP YOU. THANK YOU:)

Reasons:
  • Blacklisted phrase (0.5): THANK YOU
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): How to
  • Low reputation (1):
Posted by: Tulsi virani