2

My first question is where should I put my BroadcastReceiver class in a clean architecture project ? (Right now I put it inside data layer)

My project packages defined as below

├──data
   └──repositories
├──di
├──domain
   └── repository
   └── use_cases
├──presentation
├──utils

My second question is how to send/get data received in the BroadcastReceiver class to ViewModel following the clean architecture ?

class ResponseReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {  
      // received data 
    }
}

and this is the declaration in the manifest file

<receiver
    android:name=".data.ResponseReceiver"
    android:enabled="true"
    android:exported="true">
    ...
</receiver>
Felix Edelmann
  • 4,959
  • 3
  • 28
  • 34
Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34

1 Answers1

2

You are fine with putting your broadcast receiver inside the data package.

With View Model and repository, what you can do is add your broadcast receiver as a data source to the repository. Get your data from the receiver and put in the repository and provide it to the UI using View Model. You can use this answer as some help but the approach they have followed is wrong. They are accessing the repository directly in the activity which is not a good practice. You have to create similar functions to get data from the receiver. instead of instantiating the repository inside the activity, you will initialise your view model and access everything you need in UI from it. This is because your single source of truth is the view model and it should be if you are following MVVM which is a great architecture I will suggest for your use case. If you need some code to visualize, feel free to ask.

Arsh
  • 279
  • 2
  • 6