From 58d5e4da0ef40f0123fce413aa4476580f936e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Fri, 7 Jun 2019 19:03:26 +0200 Subject: [PATCH] Fix showing empty view in mobile device view (#407) --- .../mobiledevice/MobileDeviceRepository.kt | 12 ++++- .../mobiledevice/MobileDeviceAdapter.kt | 2 +- .../mobiledevice/MobileDeviceFragment.kt | 52 +++++++++++-------- .../mobiledevice/MobileDevicePresenter.kt | 20 ++++++- .../modules/mobiledevice/MobileDeviceView.kt | 7 +++ .../res/layout/fragment_mobile_device.xml | 2 +- 6 files changed, 68 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt index 3643a701..ac450ff4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt @@ -35,10 +35,18 @@ class MobileDeviceRepository @Inject constructor( } fun unregisterDevice(semester: Semester, device: MobileDevice): Single { - return remote.unregisterDevice(semester, device) + return ReactiveNetwork.checkInternetConnectivity(settings) + .flatMap { + if (it) remote.unregisterDevice(semester, device) + else Single.error(UnknownHostException()) + } } fun getToken(semester: Semester): Single { - return remote.getToken(semester) + return ReactiveNetwork.checkInternetConnectivity(settings) + .flatMap { + if (it) remote.getToken(semester) + else Single.error(UnknownHostException()) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceAdapter.kt index 518246b6..27c72ce6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceAdapter.kt @@ -6,5 +6,5 @@ import io.github.wulkanowy.data.db.entities.MobileDevice class MobileDeviceAdapter> : FlexibleAdapter(null, null, true) { - var onDeviceUnregisterListener: (MobileDevice, position: Int) -> Unit = { _, _ -> } + var onDeviceUnregisterListener: (device: MobileDevice, position: Int) -> Unit = { _, _ -> } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt index 98780e17..1d9104a8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt @@ -8,10 +8,10 @@ import android.view.View.VISIBLE import android.view.ViewGroup import eu.davidea.flexibleadapter.common.FlexibleItemDecoration import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager -import eu.davidea.flexibleadapter.helpers.EmptyViewHelper import eu.davidea.flexibleadapter.helpers.UndoHelper import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import io.github.wulkanowy.R +import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView @@ -48,7 +48,7 @@ class MobileDeviceFragment : BaseFragment(), MobileDeviceView, MainView.TitledVi } override fun initView() { - mobileDevicesRecycler.run { + with(mobileDevicesRecycler) { layoutManager = SmoothScrollLinearLayoutManager(context) adapter = devicesAdapter addItemDecoration(FlexibleItemDecoration(context) @@ -56,37 +56,43 @@ class MobileDeviceFragment : BaseFragment(), MobileDeviceView, MainView.TitledVi .withDrawDividerOnLastItem(false) ) } - EmptyViewHelper.create(devicesAdapter, mobileDevicesEmpty) + with(devicesAdapter) { + isPermanentDelete = false + onDeviceUnregisterListener = presenter::onUnregisterDevice + } mobileDevicesSwipe.setOnRefreshListener { presenter.onSwipeRefresh() } mobileDeviceAddButton.setOnClickListener { presenter.onRegisterDevice() } - devicesAdapter.run { - isPermanentDelete = false - onDeviceUnregisterListener = { device, position -> - val onActionListener = object : UndoHelper.OnActionListener { - override fun onActionConfirmed(action: Int, event: Int) { - presenter.onUnregister(device) - } - - override fun onActionCanceled(action: Int, positions: MutableList?) { - devicesAdapter.restoreDeletedItems() - } - } - UndoHelper(devicesAdapter, onActionListener) - .withConsecutive(false) - .withAction(UndoHelper.Action.REMOVE) - .start(listOf(position), mobileDevicesRecycler, R.string.mobile_device_removed, R.string.all_undo, 3000) - } - } } override fun updateData(data: List) { devicesAdapter.updateDataSet(data) } + override fun restoreDeleteItem() { + devicesAdapter.restoreDeletedItems() + } + override fun clearData() { devicesAdapter.clear() } + override fun showUndo(position: Int, device: MobileDevice) { + val onActionListener = object : UndoHelper.OnActionListener { + override fun onActionConfirmed(action: Int, event: Int) { + presenter.onUnregisterConfirmed(device) + } + + override fun onActionCanceled(action: Int, positions: MutableList?) { + presenter.onUnregisterCancelled() + } + } + + UndoHelper(devicesAdapter, onActionListener) + .withConsecutive(false) + .withAction(UndoHelper.Action.REMOVE) + .start(listOf(position), mobileDevicesRecycler, R.string.mobile_device_removed, R.string.all_undo, 3000) + } + override fun hideRefresh() { mobileDevicesSwipe.isRefreshing = false } @@ -95,6 +101,10 @@ class MobileDeviceFragment : BaseFragment(), MobileDeviceView, MainView.TitledVi mobileDevicesProgress.visibility = if (show) VISIBLE else GONE } + override fun showEmpty(show: Boolean) { + mobileDevicesEmpty.visibility = if (show) VISIBLE else GONE + } + override fun enableSwipe(enable: Boolean) { mobileDevicesSwipe.isEnabled = enable } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt index 47203c8a..71359a52 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt @@ -50,6 +50,7 @@ class MobileDevicePresenter @Inject constructor( view?.run { updateData(it) showContent(it.isNotEmpty()) + showEmpty(it.isEmpty()) } analytics.logEvent("load_devices", "items" to it.size, "force_refresh" to forceRefresh) }) { @@ -62,13 +63,27 @@ class MobileDevicePresenter @Inject constructor( view?.showTokenDialog() } - fun onUnregister(device: MobileDevice) { + fun onUnregisterDevice(device: MobileDevice, position: Int) { + view?.run { + showUndo(position, device) + showEmpty(isViewEmpty) + } + } + + fun onUnregisterCancelled() { + view?.run { + restoreDeleteItem() + showEmpty(isViewEmpty) + } + } + + fun onUnregisterConfirmed(device: MobileDevice) { Timber.i("Unregister device started") disposable.add(studentRepository.getCurrentStudent() .flatMap { semesterRepository.getCurrentSemester(it) } .flatMap { semester -> mobileDeviceRepository.unregisterDevice(semester, device) - .flatMap { mobileDeviceRepository.getDevices(semester, it) } + .flatMap { mobileDeviceRepository.getDevices(semester, it) } } .map { items -> items.map { MobileDeviceItem(it) } } .subscribeOn(schedulers.backgroundThread) @@ -84,6 +99,7 @@ class MobileDevicePresenter @Inject constructor( view?.run { updateData(it) showContent(it.isNotEmpty()) + showEmpty(it.isEmpty()) } }) { Timber.i("Unregister device result: An exception occurred") diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceView.kt index ef35cd75..58804e24 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceView.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.mobiledevice +import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.ui.base.BaseView interface MobileDeviceView : BaseView { @@ -10,6 +11,8 @@ interface MobileDeviceView : BaseView { fun updateData(data: List) + fun restoreDeleteItem() + fun hideRefresh() fun clearData() @@ -20,5 +23,9 @@ interface MobileDeviceView : BaseView { fun showContent(show: Boolean) + fun showEmpty(show: Boolean) + + fun showUndo(position: Int, device: MobileDevice) + fun showTokenDialog() } diff --git a/app/src/main/res/layout/fragment_mobile_device.xml b/app/src/main/res/layout/fragment_mobile_device.xml index a3be57ea..289cf2f4 100644 --- a/app/src/main/res/layout/fragment_mobile_device.xml +++ b/app/src/main/res/layout/fragment_mobile_device.xml @@ -20,7 +20,7 @@ android:gravity="center" android:orientation="vertical" android:padding="10dp" - android:alpha="0.0"> + android:visibility="gone">