Skip to content

Commit

Permalink
Implement Repository and UseCase for logout process
Browse files Browse the repository at this point in the history
  • Loading branch information
nuhkoca committed Sep 24, 2019
1 parent 1817f11 commit bd046a3
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class OrdersActivity : BaseActivity<OrdersViewModel>() {
@Inject
lateinit var itemAdapter: ItemAdapter

private var shouldUpdate = false

override val layoutId = R.layout.activity_orders

override fun getViewModelClass() = OrdersViewModel::class.java
Expand All @@ -35,26 +33,17 @@ class OrdersActivity : BaseActivity<OrdersViewModel>() {
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<LoginActivity>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/techchallenge/core/UseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface UseCase {

interface CompletableUseCase<params : Params> :
UseCase {
fun execute(params: params): Completable
fun execute(params: params? = null): Completable
}

interface SingleUseCase<params : Params, T : Any> :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ class ProgressDialogFragment : DaggerDialogFragment() {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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) :
Expand All @@ -28,16 +28,5 @@ class ItemAdapter @Inject constructor(private val delegateAdapterManager: Delega
)
}

fun add(items: List<AdapterItem>) {
this.items.clear()
this.items.addAll(items)
notifyDataSetChanged()
}

fun update(newItems: List<AdapterItem>) {
val diff = DiffUtil.calculateDiff(AdapterItemDiffUtil(items, newItems))
items.clear()
items.addAll(newItems)
diff.dispatchUpdatesTo(this)
}
fun set(newItems: List<AdapterItem>) = smartBind(items, newItems)
}
22 changes: 22 additions & 0 deletions core/src/main/java/com/techchallenge/core/util/ext/Adapter.kt
Original file line number Diff line number Diff line change
@@ -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<AdapterItem>,
newItems: List<AdapterItem>
) {
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)
}
}
1 change: 1 addition & 0 deletions core/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:splitMotionEvents">false</item>
</style>

</resources>
4 changes: 4 additions & 0 deletions domain/src/main/java/com/techchallenge/domain/DomainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ abstract class DomainModule {
@Binds
abstract fun provideMarketimRepository(marketimRepositoryImpl: MarketimRepositoryImpl):
MarketimRepository

@Binds
abstract fun provideLogoutRepository(logoutRepositoryImpl: LogoutRepositoryImpl):
LogoutRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.techchallenge.domain

import io.reactivex.Completable

interface LogoutRepository {
fun logout(): Completable
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
15 changes: 15 additions & 0 deletions domain/src/main/java/com/techchallenge/domain/LogoutUseCase.kt
Original file line number Diff line number Diff line change
@@ -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<None> {

override fun execute(params: None?): Completable {
return logoutRepository.logout()
}
}

0 comments on commit bd046a3

Please sign in to comment.