Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to pass an Rx obserable as the dataset instead? #21

Open
mohsin opened this issue Jan 15, 2020 · 0 comments
Open

[Question] How to pass an Rx obserable as the dataset instead? #21

mohsin opened this issue Jan 15, 2020 · 0 comments

Comments

@mohsin
Copy link

mohsin commented Jan 15, 2020

I'm trying to combine this with RxPreferences. I have a List stored as shared preferences and every time this pref object gets updated I want the recycler view (using this library) to be updated as well. Think of Whatsapp chats example where when a new message comes it updated the list but all data it stored internally (shared prefs in this example).

I'm currently using this code to solve my problem (which kinda works perfectly):

val chatsPref = rxSharedPreferences.getObject("chats", emptyList(), ChatsPreferenceAdapter(gson))
val chats = chatsPref.get()
val rxDataSource = RxDataSource<ChatItemBinding, Chat>(R.layout.chat_item, chats)
disposables.add(
    rxDataSource.asObservable()
        .subscribe {
            val item = it.item!!
            val binding = it.viewDataBinding!!

            binding.tvMessage.text = item.message
        }
)

disposables.add(chatsPref.asObservable()
    .subscribe {
        rxDataSource.updateDataSet(it).updateAdapter()
    }
)

You can see that in this, I'm first extracting the initial value of chats using chatsPref.get() and then initializing my RxDataSource object using that and then in the last few lines, using the observable stream of the preference item to let the data source update. Is there a way I can cut down the lines to directly pass chatsPref.asObservable() into RxDataSource during initialization to avoid all these extra steps?

@mohsin mohsin changed the title [Question] How to pass a Rx stream as dataset instead? [Question] How to pass a Rx stream as the dataset instead? Jan 15, 2020
@mohsin mohsin changed the title [Question] How to pass a Rx stream as the dataset instead? [Question] How to pass an Rx obserable as the dataset instead? Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant