Skip to content

Commit

Permalink
Refactor MessengerViewModel and update Kotlin to latest RC
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengees committed Oct 9, 2018
1 parent 7a3a36e commit 53c6b95
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 93 deletions.
2 changes: 1 addition & 1 deletion gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ext {

// Core plugins
androidPluginVersion = '3.3.0-alpha13'
kotlinVersion = '1.3-M2'
kotlinVersion = '1.3.0-rc-146'

// Utility plugins
detektPluginVersion = '1.0.0.RC9.2'
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/anime/AnimeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class AnimeActivity : DrawerActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.clicks()
.autoDisposable(this.scope())
.subscribe { _ ->
.subscribe {
name?.let {
MediaActivity.navigateTo(this, id, it, Category.ANIME)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/me/proxer/app/anime/AnimeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ class AnimeFragment : BaseContentFragment<AnimeStreamInfo>() {
})

viewModel.userStateData.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
snackbar(root, R.string.fragment_set_user_info_success)
}
})

viewModel.userStateError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_set_user_info, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down Expand Up @@ -287,7 +287,7 @@ class AnimeFragment : BaseContentFragment<AnimeStreamInfo>() {
errorContainer.isInvisible = true

errorInnerContainer.doOnLayout {
header.doOnLayout { _ ->
header.doOnLayout {
val newCenter = root.height / 2f + header.height / 2f
val containerCenterCorrection = errorInnerContainer.height / 2f

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/me/proxer/app/auth/LoginDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class LoginDialog : BaseDialog() {
it.editorActionEvents(Predicate { event -> event.actionId() == EditorInfo.IME_ACTION_GO })
.filter { event -> event.actionId() == EditorInfo.IME_ACTION_GO }
.autoDisposable(dialogLifecycleOwner.scope())
.subscribe { _ -> validateAndLogin() }
.subscribe { validateAndLogin() }
}

listOf(username to usernameContainer, password to passwordContainer).forEach { (input, container) ->
Expand All @@ -116,7 +116,7 @@ class LoginDialog : BaseDialog() {

private fun setupViewModel() {
viewModel.data.observe(dialogLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
storageHelper.user = LocalUser(it.loginToken, it.id, username.safeText.trim().toString(), it.image)

bus.post(LoginEvent())
Expand All @@ -126,7 +126,7 @@ class LoginDialog : BaseDialog() {
})

viewModel.error.observe(dialogLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
viewModel.error.value = null

requireContext().toast(it.message)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/me/proxer/app/auth/LogoutDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class LogoutDialog : BaseDialog() {
super.onDialogCreated(savedInstanceState)

viewModel.data.observe(dialogLifecycleOwner, Observer {
it?.let { _ -> dismiss() }
it?.let { dismiss() }
})

viewModel.error.observe(dialogLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
viewModel.error.value = null

requireContext().toast(it.message)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/base/PagedContentFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ abstract class PagedContentFragment<T> : BaseContentFragment<List<T>>() {
.subscribe { viewModel.loadIfPossible() }

viewModel.refreshError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_refresh, getString(it.message)), Snackbar.LENGTH_LONG,
it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/bookmark/BookmarkFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class BookmarkFragment : PagedContentFragment<Bookmark>() {
innerAdapter.glide = GlideApp.with(this)

viewModel.itemDeletionError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_bookmark_deletion, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ class CreateConferenceFragment : BaseFragment() {
})

viewModel.result.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
requireActivity().finish()

MessengerActivity.navigateTo(requireActivity(), it)
}
})

viewModel.error.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root,
it.message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class MessengerFragment : PagedContentFragment<LocalMessage>() {
}

viewModel.conference.observe(viewLifecycleOwner, Observer {
it?.let { _ -> conference = it }
it?.let { conference = it }
})

recyclerView.scrollEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,9 @@ class MessengerViewModel(initialConference: LocalConference) : PagedViewModel<Lo
get() = safeConference.isFullyLoaded
set(value) = Unit

override val data = object : MediatorLiveData<List<LocalMessage>>() {

private val source by lazy { messengerDao.getMessagesLiveDataForConference(safeConference.id) }

override fun onActive() {
super.onActive()

addSource(source) {
it?.let { _ ->
if (storageHelper.isLoggedIn) {
if (it.isEmpty() && !hasReachedEnd) {
MessengerWorker.enqueueMessageLoad(safeConference.id)
} else {
if (error.value == null) {
dataDisposable?.dispose()

page = it.size.div(itemsOnPage)

isLoading.value = false
error.value = null
this.value = it

Completable.fromAction { messengerDao.markConferenceAsRead(safeConference.id) }
.subscribeOn(Schedulers.io())
.subscribeAndLogErrors()
}
}
}
}
}
}

override fun onInactive() {
removeSource(source)

super.onInactive()
}
}
override val data = MediatorLiveData<List<LocalMessage>>()
val conference = MediatorLiveData<LocalConference>()
val draft = ResettingMutableLiveData<String?>()

override val dataSingle: Single<List<LocalMessage>>
get() = Single.fromCallable { validators.validateLogin() }
Expand All @@ -84,26 +49,37 @@ class MessengerViewModel(initialConference: LocalConference) : PagedViewModel<Lo
Single.never<List<LocalMessage>>()
}

val conference = object : MediatorLiveData<LocalConference>() {
private val dataSource = { it: List<LocalMessage>? ->
it?.also {
if (storageHelper.isLoggedIn) {
if (it.isEmpty() && !hasReachedEnd) {
MessengerWorker.enqueueMessageLoad(safeConference.id)
} else {
if (error.value == null) {
dataDisposable?.dispose()

private val source by lazy { messengerDao.getConferenceLiveData(safeConference.id) }
page = it.size.div(itemsOnPage)

override fun onActive() {
super.onActive()
isLoading.value = false
error.value = null
data.value = it

addSource(source) {
it?.let { _ -> this.value = it }
Completable.fromAction { messengerDao.markConferenceAsRead(safeConference.id) }
.subscribeOn(Schedulers.io())
.subscribeAndLogErrors()
}
}
}
}

override fun onInactive() {
removeSource(source)

super.onInactive()
}
Unit
}

val draft = ResettingMutableLiveData<String?>()
private val conferenceSource = { it: LocalConference? ->
it?.also { conference.value = it }

Unit
}

private val messengerDao by inject<MessengerDao>()

Expand All @@ -115,6 +91,15 @@ class MessengerViewModel(initialConference: LocalConference) : PagedViewModel<Lo
init {
conference.value = initialConference

data.addSource(
messengerDao.getMessagesLiveDataForConference(initialConference.id),
dataSource
)
conference.addSource(
messengerDao.getConferenceLiveData(initialConference.id),
conferenceSource
)

disposables += bus.register(MessengerErrorEvent::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe { event: MessengerErrorEvent ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ class ChatReportDialog : BaseDialog() {
.subscribe { setError(messageContainer, null) }

viewModel.data.observe(dialogLifecycleOwner, Observer {
it?.let { _ -> dismiss() }
it?.let { dismiss() }
})

viewModel.error.observe(dialogLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
viewModel.error.value = null

requireContext().toast(it.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ class ChatViewModel(private val chatRoomId: String) : PagedViewModel<ParsedChatM
pollingDisposable = Single.fromCallable { validators.validateLogin() }
.flatMap { api.chat().messages(chatRoomId).messageId("0").buildSingle() }
.map { it.map { message -> message.toParsedMessage() } }
.repeatWhen { it.concatMap { _ -> Flowable.timer(3, TimeUnit.SECONDS) } }
.retryWhen { it.concatMap { _ -> Flowable.timer(3, TimeUnit.SECONDS) } }
.repeatWhen { it.concatMap { Flowable.timer(3, TimeUnit.SECONDS) } }
.retryWhen { it.concatMap { Flowable.timer(3, TimeUnit.SECONDS) } }
.map { newData -> mergeNewDataWithExistingData(newData, "0") }
.let { if (!immediate) it.delaySubscription(3, TimeUnit.SECONDS) else it }
.subscribeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ChatRoomInfoViewModel(private val chatRoomId: String) : BaseContentViewMod

private fun startPolling() {
pollingDisposable = dataSingle
.repeatWhen { it.concatMap { _ -> Flowable.timer(10, TimeUnit.SECONDS) } }
.retryWhen { it.concatMap { _ -> Flowable.timer(10, TimeUnit.SECONDS) } }
.repeatWhen { it.concatMap { Flowable.timer(10, TimeUnit.SECONDS) } }
.retryWhen { it.concatMap { Flowable.timer(10, TimeUnit.SECONDS) } }
.delaySubscription(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/forum/TopicFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TopicFragment : PagedContentFragment<ParsedPost>() {
innerAdapter.glide = GlideApp.with(this)

viewModel.metaData.observe(viewLifecycleOwner, Observer {
it?.let { _ -> topic = it.subject }
it?.let { topic = it.subject }
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/manga/MangaActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class MangaActivity : BaseActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.clicks()
.autoDisposable(this.scope())
.subscribe { _ ->
.subscribe {
name?.let {
MediaActivity.navigateTo(this, id, name, Category.MANGA)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/me/proxer/app/manga/MangaFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ class MangaFragment : BaseContentFragment<MangaChapterInfo>() {
recyclerView.adapter = adapter

viewModel.userStateData.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
snackbar(activityRoot, R.string.fragment_set_user_info_success)
}
})

viewModel.userStateError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_set_user_info, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down Expand Up @@ -397,7 +397,7 @@ class MangaFragment : BaseContentFragment<MangaChapterInfo>() {
errorContainer.isInvisible = true

errorInnerContainer.doOnLayout {
header.doOnLayout { _ ->
header.doOnLayout {
val newCenter = root.height / 2f + header.height / 2f
val containerCenterCorrection = errorInnerContainer.height / 2f

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/proxer/app/media/MediaInfoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MediaInfoViewModel(private val entryId: String) : BaseViewModel<Pair<Entry
.subscribeOn(Schedulers.io())
.flatMap { Single.fromCallable { it.apply { validators.validateLogin() } } }
.observeOn(AndroidSchedulers.mainThread())
.subscribeAndLogErrors({ _ ->
.subscribeAndLogErrors({
userInfoUpdateError.value = null
userInfoUpdateData.value = Unit

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/me/proxer/app/media/info/MediaInfoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ class MediaInfoFragment : BaseContentFragment<Pair<Entry, Optional<MediaUserInfo
}

viewModel.userInfoUpdateData.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
snackbar(root, R.string.fragment_set_user_info_success)
}
})

viewModel.userInfoUpdateError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_set_user_info, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down Expand Up @@ -450,13 +450,13 @@ class MediaInfoFragment : BaseContentFragment<Pair<Entry, Optional<MediaUserInfo
if (onClick != null) {
chip.clicks()
.autoDisposable(viewLifecycleOwner.scope(Lifecycle.Event.ON_DESTROY))
.subscribe { _ -> onClick.invoke(items[index]) }
.subscribe { onClick.invoke(items[index]) }
}

if (layout is MaxLineFlexboxLayout && !layout.canAddView(chip)) {
layout.showAllEvents
.autoDisposable(viewLifecycleOwner.scope(Lifecycle.Event.ON_DESTROY))
.subscribe { _ -> bindChips(layout, items, mapFunction, onClick) }
.subscribe { bindChips(layout, items, mapFunction, onClick) }

layout.enableShowAllButton()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NotificationFragment : PagedContentFragment<ProxerNotification>() {
super.onViewCreated(view, savedInstanceState)

viewModel.deletionError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_notification_deletion, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class UcpMediaListFragment : PagedContentFragment<UserMediaListEntry>() {
innerAdapter.glide = GlideApp.with(this)

viewModel.itemDeletionError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_ucp_entry_deletion, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class UcpTopTenFragment : BaseContentFragment<ZippedTopTenResult>() {
mangaRecyclerView.adapter = mangaAdapter

viewModel.itemDeletionError.observe(viewLifecycleOwner, Observer {
it?.let { _ ->
it?.let {
multilineSnackbar(
root, getString(R.string.error_topten_entry_removal, getString(it.message)),
Snackbar.LENGTH_LONG, it.buttonMessage, it.toClickListener(hostingActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ExpandableSelectionView @JvmOverloads constructor(
items.zip(itemContainer.children.toList()).forEach { (item, view) ->
view.clicks()
.autoDisposable(ViewScopeProvider.from(this))
.subscribeAndLogErrors { _ ->
.subscribeAndLogErrors {
selection.clear()
selection.add(item)

Expand Down
Loading

0 comments on commit 53c6b95

Please sign in to comment.