diff --git a/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersActivity.kt b/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersActivity.kt index 03dc144..98b4ca1 100644 --- a/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersActivity.kt +++ b/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersActivity.kt @@ -20,8 +20,6 @@ class OrdersActivity : BaseActivity() { @Inject lateinit var itemAdapter: ItemAdapter - private var shouldUpdate = false - override val layoutId = R.layout.activity_orders override fun getViewModelClass() = OrdersViewModel::class.java @@ -35,26 +33,17 @@ class OrdersActivity : BaseActivity() { title = getString(R.string.orders_logout_title), message = getString(R.string.orders_logout_message), positiveButton = Pair(getString(R.string.orders_logout_positive_button), { - viewModel.setRememberMe(false) + viewModel.logout() showDialog(getString(R.string.text_logout)) }), negativeButton = Pair(getString(R.string.orders_logout_negative_button), {}) ) } - btnOrders.setOnClickListener { - viewModel.getOrders() - } + btnOrders.setOnClickListener { viewModel.getOrders() } } override fun observeViewModel() = with(viewModel) { - ordersLiveData.observeWith(this@OrdersActivity) { - if (shouldUpdate.not()) { - itemAdapter.add(it) - shouldUpdate = true - } else { - itemAdapter.update(it) - } - } + ordersLiveData.observeWith(this@OrdersActivity, itemAdapter::set) logoutLiveData.observeWith(this@OrdersActivity) { hideDialog() launchActivity() diff --git a/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersViewModel.kt b/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersViewModel.kt index 7ddd5ad..569c07f 100644 --- a/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersViewModel.kt +++ b/app/src/main/java/com/techchallenge/marketim/ui/orders/OrdersViewModel.kt @@ -5,21 +5,19 @@ import androidx.lifecycle.MutableLiveData import com.github.ajalt.timberkt.w import com.techchallenge.core.BaseViewModel import com.techchallenge.core.UseCase.None -import com.techchallenge.core.local.BooleanPreference -import com.techchallenge.core.local.LocalStorageModule import com.techchallenge.core.util.executors.Executors import com.techchallenge.core.util.ext.applySchedulers import com.techchallenge.core.util.ext.progressify import com.techchallenge.data.ResponseViewItem import com.techchallenge.domain.GetOrdersUseCase +import com.techchallenge.domain.LogoutUseCase import com.techchallenge.marketim.vm.ActionLiveData import io.reactivex.rxkotlin.addTo import javax.inject.Inject -import javax.inject.Named class OrdersViewModel @Inject constructor( private val getOrdersUseCase: GetOrdersUseCase, - @Named(LocalStorageModule.REMEMBER_ME_PREF) private val rememberMePref: BooleanPreference, + private val logoutUseCase: LogoutUseCase, override var executors: Executors ) : BaseViewModel(executors) { @@ -43,8 +41,12 @@ class OrdersViewModel @Inject constructor( .addTo(disposables) } - fun setRememberMe(isRemember: Boolean) { - rememberMePref.set(isRemember) - _logoutLiveData.call() + fun logout() { + logoutUseCase.execute() + .applySchedulers(executors) + .subscribe({ + _logoutLiveData.call() + }, { w { "Cannot logout at the moment" } }) + .addTo(disposables) } } diff --git a/core/src/main/java/com/techchallenge/core/UseCase.kt b/core/src/main/java/com/techchallenge/core/UseCase.kt index 617ddd3..982d5a8 100644 --- a/core/src/main/java/com/techchallenge/core/UseCase.kt +++ b/core/src/main/java/com/techchallenge/core/UseCase.kt @@ -18,7 +18,7 @@ interface UseCase { interface CompletableUseCase : UseCase { - fun execute(params: params): Completable + fun execute(params: params? = null): Completable } interface SingleUseCase : diff --git a/core/src/main/java/com/techchallenge/core/dialog/ProgressDialogFragment.kt b/core/src/main/java/com/techchallenge/core/dialog/ProgressDialogFragment.kt index bdb7a81..38a52c4 100644 --- a/core/src/main/java/com/techchallenge/core/dialog/ProgressDialogFragment.kt +++ b/core/src/main/java/com/techchallenge/core/dialog/ProgressDialogFragment.kt @@ -52,4 +52,4 @@ class ProgressDialogFragment : DaggerDialogFragment() { } } } -} \ No newline at end of file +} diff --git a/core/src/main/java/com/techchallenge/core/util/delegate/ItemAdapter.kt b/core/src/main/java/com/techchallenge/core/util/delegate/ItemAdapter.kt index 1381b92..7d41a80 100644 --- a/core/src/main/java/com/techchallenge/core/util/delegate/ItemAdapter.kt +++ b/core/src/main/java/com/techchallenge/core/util/delegate/ItemAdapter.kt @@ -1,8 +1,8 @@ package com.techchallenge.core.util.delegate import android.view.ViewGroup -import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView +import com.techchallenge.core.util.ext.smartBind import javax.inject.Inject class ItemAdapter @Inject constructor(private val delegateAdapterManager: DelegateAdapterManager) : @@ -28,16 +28,5 @@ class ItemAdapter @Inject constructor(private val delegateAdapterManager: Delega ) } - fun add(items: List) { - this.items.clear() - this.items.addAll(items) - notifyDataSetChanged() - } - - fun update(newItems: List) { - val diff = DiffUtil.calculateDiff(AdapterItemDiffUtil(items, newItems)) - items.clear() - items.addAll(newItems) - diff.dispatchUpdatesTo(this) - } + fun set(newItems: List) = smartBind(items, newItems) } diff --git a/core/src/main/java/com/techchallenge/core/util/ext/Adapter.kt b/core/src/main/java/com/techchallenge/core/util/ext/Adapter.kt new file mode 100644 index 0000000..e3b8b4c --- /dev/null +++ b/core/src/main/java/com/techchallenge/core/util/ext/Adapter.kt @@ -0,0 +1,22 @@ +package com.techchallenge.core.util.ext + +import androidx.recyclerview.widget.DiffUtil +import com.techchallenge.core.util.delegate.AdapterItem +import com.techchallenge.core.util.delegate.AdapterItemDiffUtil +import com.techchallenge.core.util.delegate.ItemAdapter + +fun ItemAdapter.smartBind( + items: MutableList, + newItems: List +) { + if (items.isNullOrEmpty()) { + items.clear() + items.addAll(newItems) + notifyDataSetChanged() + } else { + val diff = DiffUtil.calculateDiff(AdapterItemDiffUtil(items, newItems)) + items.clear() + items.addAll(newItems) + diff.dispatchUpdatesTo(this) + } +} diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index aa4613b..4b22cce 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -33,6 +33,7 @@ @color/colorPrimary @color/colorPrimaryDark @color/colorAccent + false \ No newline at end of file diff --git a/domain/src/main/java/com/techchallenge/domain/DomainModule.kt b/domain/src/main/java/com/techchallenge/domain/DomainModule.kt index b1aa3bf..eb12878 100644 --- a/domain/src/main/java/com/techchallenge/domain/DomainModule.kt +++ b/domain/src/main/java/com/techchallenge/domain/DomainModule.kt @@ -9,4 +9,8 @@ abstract class DomainModule { @Binds abstract fun provideMarketimRepository(marketimRepositoryImpl: MarketimRepositoryImpl): MarketimRepository + + @Binds + abstract fun provideLogoutRepository(logoutRepositoryImpl: LogoutRepositoryImpl): + LogoutRepository } diff --git a/domain/src/main/java/com/techchallenge/domain/LogoutRepository.kt b/domain/src/main/java/com/techchallenge/domain/LogoutRepository.kt new file mode 100644 index 0000000..b7af84f --- /dev/null +++ b/domain/src/main/java/com/techchallenge/domain/LogoutRepository.kt @@ -0,0 +1,7 @@ +package com.techchallenge.domain + +import io.reactivex.Completable + +interface LogoutRepository { + fun logout(): Completable +} diff --git a/domain/src/main/java/com/techchallenge/domain/LogoutRepositoryImpl.kt b/domain/src/main/java/com/techchallenge/domain/LogoutRepositoryImpl.kt new file mode 100644 index 0000000..38dac3e --- /dev/null +++ b/domain/src/main/java/com/techchallenge/domain/LogoutRepositoryImpl.kt @@ -0,0 +1,17 @@ +package com.techchallenge.domain + +import com.techchallenge.core.local.BooleanPreference +import com.techchallenge.core.local.LocalStorageModule.REMEMBER_ME_PREF +import io.reactivex.Completable +import javax.inject.Inject +import javax.inject.Named + +class LogoutRepositoryImpl @Inject constructor( + @Named(REMEMBER_ME_PREF) private val rememberMePref: BooleanPreference +) : LogoutRepository { + + override fun logout(): Completable { + rememberMePref.set(false) + return Completable.complete() + } +} diff --git a/domain/src/main/java/com/techchallenge/domain/LogoutUseCase.kt b/domain/src/main/java/com/techchallenge/domain/LogoutUseCase.kt new file mode 100644 index 0000000..0a8afbe --- /dev/null +++ b/domain/src/main/java/com/techchallenge/domain/LogoutUseCase.kt @@ -0,0 +1,15 @@ +package com.techchallenge.domain + +import com.techchallenge.core.UseCase +import com.techchallenge.core.UseCase.None +import io.reactivex.Completable +import javax.inject.Inject + +class LogoutUseCase @Inject constructor( + private val logoutRepository: LogoutRepository +) : UseCase.CompletableUseCase { + + override fun execute(params: None?): Completable { + return logoutRepository.logout() + } +}