Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.adesso.movee.internal.popup.PopupCallback
import com.adesso.movee.internal.popup.PopupUiModel
import com.adesso.movee.internal.util.Event
import com.adesso.movee.internal.util.Failure
import com.adesso.movee.internal.util.event
import com.adesso.movee.navigation.NavigationCommand
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -67,37 +68,36 @@ abstract class BaseAndroidViewModel(application: Application) : AndroidViewModel
else -> Pair("", failure.message ?: failure.toString())
}

_failurePopup.value = Event(
_failurePopup.value =
PopupUiModel(
title = title,
message = message,
popUpType = PopUpType.ERROR
)
)
).event()
}

protected fun showSnackBar(message: String) {
_success.value = Event(message)
_success.value = message.event()
}

fun navigate(directions: NavDirections) {
_navigation.value = Event(NavigationCommand.ToDirection(directions))
_navigation.value = NavigationCommand.ToDirection(directions).event()
}

fun navigate(deepLink: String) {
_navigation.value = Event(NavigationCommand.ToDeepLink(deepLink))
_navigation.value = NavigationCommand.ToDeepLink(deepLink).event()
}

fun navigate(@StringRes deepLinkRes: Int) {
navigate(getString(deepLinkRes))
}

fun navigate(model: PopupUiModel, callback: PopupCallback?) {
_navigation.value = Event(NavigationCommand.Popup(model, callback))
_navigation.value = NavigationCommand.Popup(model, callback).event()
}

fun navigateBack() {
_navigation.value = Event(NavigationCommand.Back)
_navigation.value = NavigationCommand.Back.event()
}

protected suspend fun onUIThread(block: suspend CoroutineScope.() -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import com.adesso.movee.BR
import com.adesso.movee.internal.extension.observeNonNull
import com.adesso.movee.internal.extension.showPopup
import com.adesso.movee.internal.popup.PopupUiModel
import com.adesso.movee.internal.util.EventObserver
import com.adesso.movee.internal.util.functional.lazyThreadSafetyNone
import com.adesso.movee.navigation.NavigationCommand
import com.adesso.movee.scene.main.MainActivity
Expand Down Expand Up @@ -103,11 +104,10 @@ abstract class BaseBottomSheetDialogFragment<VM : BaseAndroidViewModel, B : View
}

private fun observeNavigation() {
viewModel.navigation.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { command ->
handleNavigation(command)
}
}
viewModel.navigation.observe(viewLifecycleOwner, navigation)
}
private val navigation = EventObserver<NavigationCommand> {
handleNavigation(it)
}

protected open fun handleNavigation(command: NavigationCommand) {
Expand All @@ -130,19 +130,17 @@ abstract class BaseBottomSheetDialogFragment<VM : BaseAndroidViewModel, B : View
}

private fun observeFailure() {
viewModel.failurePopup.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { popupUiModel ->
context?.showPopup(popupUiModel)
}
}
viewModel.failurePopup.observe(viewLifecycleOwner, failurePopup)
}
private val failurePopup = EventObserver<PopupUiModel> {
context?.showPopup(it)
}

private fun observeSuccess() {
viewModel.success.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { message ->
showSnackBarMessage(message)
}
}
viewModel.success.observe(viewLifecycleOwner, success)
}
private val success = EventObserver<String> {
showSnackBarMessage(it)
}

private fun showSnackBarMessage(message: String) {
Expand Down
30 changes: 14 additions & 16 deletions app/src/main/kotlin/com/adesso/movee/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import com.adesso.movee.BR
import com.adesso.movee.internal.extension.observeNonNull
import com.adesso.movee.internal.extension.showPopup
import com.adesso.movee.internal.popup.PopupUiModel
import com.adesso.movee.internal.util.EventObserver
import com.adesso.movee.internal.util.functional.lazyThreadSafetyNone
import com.adesso.movee.navigation.NavigationCommand
import com.adesso.movee.scene.main.MainActivity
Expand Down Expand Up @@ -88,11 +89,10 @@ abstract class BaseFragment<VM : BaseAndroidViewModel, B : ViewDataBinding> :
}

private fun observeNavigation() {
viewModel.navigation.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { command ->
handleNavigation(command)
}
}
viewModel.navigation.observe(viewLifecycleOwner, navigation)
}
private val navigation = EventObserver<NavigationCommand> {
handleNavigation(it)
}

protected open fun handleNavigation(command: NavigationCommand) {
Expand All @@ -115,19 +115,17 @@ abstract class BaseFragment<VM : BaseAndroidViewModel, B : ViewDataBinding> :
}

private fun observeFailure() {
viewModel.failurePopup.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { popupUiModel ->
context?.showPopup(popupUiModel)
}
}
viewModel.failurePopup.observe(viewLifecycleOwner, failurePopup)
}
private val failurePopup = EventObserver<PopupUiModel> {
context?.showPopup(it)
}

private fun observeSuccess() {
viewModel.success.observeNonNull(viewLifecycleOwner) {
it.getContentIfNotHandled()?.let { message ->
showSnackBarMessage(message)
}
}
viewModel.success.observe(viewLifecycleOwner, success)
}
private val success = EventObserver<String> {
showSnackBarMessage(it)
}

private fun showSnackBarMessage(message: String) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/com/adesso/movee/internal/util/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ open class Event<out T>(private val content: T) {
*/
fun peekContent(): T = content
}

inline fun <reified T> Any.event(): Event<T> {
return Event(this as T)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.adesso.movee.internal.util

import androidx.lifecycle.Observer

class EventObserver<T>(private val eventUnHandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(t: Event<T>?) {
t?.getContentIfNotHandled()?.let {
eventUnHandledContent(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import android.net.Uri
import com.adesso.movee.R
import com.adesso.movee.base.BaseTransparentStatusBarFragment
import com.adesso.movee.databinding.FragmentLoginBinding
import com.adesso.movee.internal.extension.observeNonNull
import com.adesso.movee.internal.util.Event
import com.adesso.movee.internal.util.EventObserver

class LoginFragment : BaseTransparentStatusBarFragment<LoginViewModel, FragmentLoginBinding>() {

Expand All @@ -15,13 +14,11 @@ class LoginFragment : BaseTransparentStatusBarFragment<LoginViewModel, FragmentL
override fun initialize() {
super.initialize()

viewModel.navigateUri.observeNonNull(viewLifecycleOwner, ::handleNavigateUriEvent)
viewModel.navigateUri.observe(viewLifecycleOwner, handleNavigateUriEvent)
}

private fun handleNavigateUriEvent(event: Event<Uri>) {
event.getContentIfNotHandled()?.let { uri ->
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
private val handleNavigateUriEvent = EventObserver<Uri> {
val intent = Intent(Intent.ACTION_VIEW, it)
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
import com.adesso.movee.base.BaseAndroidViewModel
import com.adesso.movee.domain.LoginUseCase
import com.adesso.movee.internal.util.Event
import com.adesso.movee.internal.util.event
import javax.inject.Inject
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -53,7 +54,8 @@ class LoginViewModel @Inject constructor(
}

private fun postNavigateUri(url: String) {
_navigateUri.value = Event(Uri.parse(url))
_navigateUri.value = url.event()
// _navigateUri.value = Event(Uri.parse(url))
}

companion object {
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/kotlin/com/adesso/movee/scene/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import androidx.navigation.ui.setupWithNavController
import com.adesso.movee.R
import com.adesso.movee.base.BaseBindingActivity
import com.adesso.movee.databinding.ActivityMainBinding
import com.adesso.movee.internal.extension.observeNonNull
import com.adesso.movee.internal.extension.showPopup
import com.adesso.movee.internal.util.EventObserver
import com.adesso.movee.navigation.NavigationCommand

class MainActivity : BaseBindingActivity<MainViewModel, ActivityMainBinding>() {
Expand All @@ -37,11 +37,10 @@ class MainActivity : BaseBindingActivity<MainViewModel, ActivityMainBinding>() {
}

private fun observeNavigation() {
viewModel.navigation.observeNonNull(this) {
it.getContentIfNotHandled()?.let { command ->
handleNavigation(command)
}
}
viewModel.navigation.observe(this, navigation)
}
private val navigation = EventObserver<NavigationCommand> {
handleNavigation(it)
}

private fun setupBottomNavigationView() {
Expand Down