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

make view reactive #3

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+45 −44
Diff settings

Always

Just for now

@@ -4,35 +4,37 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.jakewharton.rxbinding3.view.clicks
import github.com.worker8.archplayground.R
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.activity_reactive_views.*

class RxReactiveViewsActivity : AppCompatActivity() {
val disposables = CompositeDisposable()
val adapter = RxReactiveViewsAdapter()
val viewInput: RxReactiveViewsViewModel.ViewInput = object : RxReactiveViewsViewModel.ViewInput {
override val randomClickObservable by lazy {
rxViewChangeSubredditButton.clicks()
}
override val refreshClickObservable by lazy {
rxViewRefreshButton.clicks()
}
override val loadMoreClickObservable by lazy {
rxViewLoadMoreButton.clicks()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reactive_views)
rxViewRecyclerView.adapter = adapter

val viewModel = ViewModelProviders.of(this, RxReactiveViewsViewModelFactory()).get(RxReactiveViewsViewModel::class.java)
val viewModel = ViewModelProviders.of(this, RxReactiveViewsViewModelFactory(viewInput)).get(RxReactiveViewsViewModel::class.java)
lifecycle.addObserver(viewModel)

viewModel.screenState.observe(this, Observer { list ->
adapter.submitList(list)
})

rxViewChangeSubredditButton.setOnClickListener {
viewModel.randomSubredditClick()
}
rxViewRefreshButton.setOnClickListener {
viewModel.refreshClick()
}
rxViewLoadMoreButton.setOnClickListener {
viewModel.onLoadMoreClick()
}
}

override fun onDestroy() {
@@ -4,12 +4,13 @@ import androidx.lifecycle.*
import com.worker8.redditapi.RedditApi
import com.worker8.redditapi.model.t3_link.response.RedditLinkObject
import github.com.worker8.archplayground.common.addTo
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlin.properties.Delegates

class RxReactiveViewsViewModel : ViewModel(), LifecycleObserver {
class RxReactiveViewsViewModel(val viewInput: RxReactiveViewsViewModel.ViewInput) : ViewModel(), LifecycleObserver {
private val disposables = CompositeDisposable()
var redditApi: RedditApi by Delegates.observable(RedditApi()) { property, oldValue, newValue ->
screenState.postValue(listOf())
@@ -21,49 +22,47 @@ class RxReactiveViewsViewModel : ViewModel(), LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
}

fun refreshClick() {
redditApi = RedditApi(redditApi.subreddit)
loadMorePosts()
}

fun randomSubredditClick() {
redditApi = RedditApi(RedditApi.getRandomSubreddit())
loadMorePosts()
}

fun onLoadMoreClick() {
loadMorePosts()
}

fun loadMorePosts() {
redditApi.getMorePosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ (result, fuelError) ->
result?.value?.let { _list ->
val newList = screenState.value?.toMutableList() ?: mutableListOf()
newList.addAll(_list.getRedditImageLinks())
screenState.postValue(newList)
}
}, {
it.printStackTrace()
})
.addTo(disposables)
viewInput.apply {
Observable.merge(
refreshClickObservable.doOnNext { redditApi = RedditApi(redditApi.subreddit) },
randomClickObservable.doOnNext { redditApi = RedditApi(RedditApi.getRandomSubreddit()) },
loadMoreClickObservable
)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.flatMap { redditApi.getMorePosts() }
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ (result, fuelError) ->
result?.value?.let { _list ->
val newList = screenState.value?.toMutableList() ?: mutableListOf()
newList.addAll(_list.getRedditImageLinks())
screenState.postValue(newList)
}
}, {
it.printStackTrace()
})
.addTo(disposables)
}
}

override fun onCleared() {
super.onCleared()
disposables.dispose()
}

interface ViewInput {
val randomClickObservable: Observable<Unit>
val refreshClickObservable: Observable<Unit>
val loadMoreClickObservable: Observable<Unit>
}
}

@Suppress("UNCHECKED_CAST")
class RxReactiveViewsViewModelFactory() : ViewModelProvider.NewInstanceFactory() {
class RxReactiveViewsViewModelFactory(val viewInput: RxReactiveViewsViewModel.ViewInput) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return RxReactiveViewsViewModel() as T
return RxReactiveViewsViewModel(viewInput) as T
}
}


class RxReactiveViewsScreenState()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.