Accessing BroadCastReceiver in Viewmodel

后端 未结 1 1400

I am struggling in choosing the right way to pass data from broadcastReceiver to ViewModel and from there I pass data to my Repository and update LiveData. I use FCM push notifi

相关标签:
1条回答
  • 2021-02-04 05:34

    You need to define single source of truth (SSoT). In your case it Repository (if Repository encapsulate db persistence storage, SSoT it is db). Now you need to implement data flow from receiver to view through SSoT (Repository) like in example below:

    Receiver implementation

    public class FcmReceiver extends BroadcastReceiver {
    
        private final MutableLiveData<MyData> mData = new MutableLiveData<>();
    
        public LiveData<MyData> getData() {
            return mData;
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // entry point of data
            mData.setValue(new MyData());
        }
    }
    

    Repository

    public class Repository {
    
        private static final Repository INSTANCE = new Repository();
    
        private final MediatorLiveData<MyData> mData = new MediatorLiveData<>();
    
        private Repository() {}
    
        public static Repository instance() {
            return INSTANCE;
        }
    
        public LiveData<MyData> getData() {
            return mData;
        }
    
        public void addDataSource(LiveData<MyData> data) {
            mData.addSource(data, mData::setValue);
        }
    
        public void removeDataSource(LiveData<MyData> data) {
            mData.removeSource(data);
        }
    }
    

    View model

    public class MyViewModel extends ViewModel {
    
        public LiveData<MyData> getData() {
            // for simplicity return data directly to view
            return Repository.instance().getData();
        }
    }
    

    Activity

    There is binding of receiver data and Repository

    public class MainActivity extends AppCompatActivity {
    
        private FcmReceiver mReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mReceiver = new FcmReceiver();
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            // add data source
            Repository.instance().addDataSource(mReceiver.getData());
            registerReceiver(mReceiver, IntentFilter.create("action", "type"));
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            // don't forget to remove receiver data source
            Repository.instance().removeDataSource(mReceiver.getData());
            unregisterReceiver(mReceiver);
        }
    }
    
    0 讨论(0)
提交回复
热议问题