Skip to content

Commit

Permalink
fix:[ANDROAPP-6673] Recover home filters
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy committed Dec 17, 2024
1 parent b642569 commit 97ff43f
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 15 deletions.
113 changes: 111 additions & 2 deletions app/src/main/java/org/dhis2/usescases/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.transition.ChangeBounds
import android.transition.TransitionManager
import android.view.View
import android.webkit.MimeTypeMap
import android.widget.TextView
Expand All @@ -21,6 +23,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.app.NotificationCompat
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
Expand All @@ -35,6 +38,11 @@ import org.dhis2.bindings.app
import org.dhis2.bindings.hasPermissions
import org.dhis2.commons.animations.hide
import org.dhis2.commons.animations.show
import org.dhis2.commons.date.DateUtils
import org.dhis2.commons.filters.FilterItem
import org.dhis2.commons.filters.FilterManager
import org.dhis2.commons.filters.FiltersAdapter
import org.dhis2.commons.orgunitselector.OUTreeFragment
import org.dhis2.commons.sync.OnDismissListener
import org.dhis2.commons.sync.SyncContext
import org.dhis2.databinding.ActivityMainBinding
Expand Down Expand Up @@ -68,11 +76,15 @@ class MainActivity :
DrawerLayout.DrawerListener {

private lateinit var binding: ActivityMainBinding

lateinit var mainComponent: MainComponent

@Inject
lateinit var presenter: MainPresenter

@Inject
lateinit var newAdapter: FiltersAdapter

@Inject
lateinit var pageConfigurator: NavigationPageConfigurator

Expand All @@ -83,6 +95,8 @@ class MainActivity :
// no-op
}

private var backDropActive = false

private val requestWritePermissions =
registerForActivityResult(
ActivityResultContracts.RequestPermission(),
Expand Down Expand Up @@ -136,9 +150,14 @@ class MainActivity :
mainNavigator = MainNavigator(
dispatcherProvider = presenter.dispatcherProvider,
supportFragmentManager,
{ /*no-op*/ },
) { titleRes, _, showBottomNavigation ->
{
if (backDropActive) {
showHideFilter()
}
},
) { titleRes, showFilterButton, showBottomNavigation ->
setTitle(getString(titleRes))
setFilterButtonVisibility(showFilterButton)
setBottomNavigationVisibility(showBottomNavigation)
}
super.onCreate(savedInstanceState)
Expand All @@ -157,6 +176,8 @@ class MainActivity :

binding.mainDrawerLayout.addDrawerListener(this)

binding.filterRecycler.adapter = newAdapter

setUpNavigationBar()
setUpDevelopmentMode()

Expand Down Expand Up @@ -218,6 +239,8 @@ class MainActivity :
super.onResume()
if (sessionManagerServiceImpl.isUserLoggedIn()) {
presenter.init()
presenter.initFilters()
binding.totalFilters = FilterManager.getInstance().totalFilters
}
}

Expand Down Expand Up @@ -289,10 +312,12 @@ class MainActivity :
when (it.running) {
true -> {
binding.syncActionButton.visibility = View.GONE
setFilterButtonVisibility(false)
setBottomNavigationVisibility(false)
}

false -> {
setFilterButtonVisibility(true)
binding.syncActionButton.visibility = View.VISIBLE
setBottomNavigationVisibility(true)
presenter.onDataSuccess()
Expand Down Expand Up @@ -329,6 +354,35 @@ class MainActivity :
}
}

override fun showHideFilter() {
val transition = ChangeBounds()
transition.duration = 200
TransitionManager.beginDelayedTransition(binding.backdropLayout, transition)
backDropActive = !backDropActive
val initSet = ConstraintSet()
initSet.clone(binding.backdropLayout)
if (backDropActive) {
initSet.connect(
R.id.fragment_container,
ConstraintSet.TOP,
R.id.filterRecycler,
ConstraintSet.BOTTOM,
50,
)
binding.navigationBar.hide()
} else {
initSet.connect(
R.id.fragment_container,
ConstraintSet.TOP,
R.id.toolbar,
ConstraintSet.BOTTOM,
0,
)
binding.navigationBar.show()
}
initSet.applyTo(binding.backdropLayout)
}

override fun showGranularSync() {
SyncStatusDialog.Builder()
.withContext(this)
Expand All @@ -353,6 +407,37 @@ class MainActivity :
.show("ALL_SYNC")
}

override fun updateFilters(totalFilters: Int) {
binding.totalFilters = totalFilters
}

override fun showPeriodRequest(periodRequest: FilterManager.PeriodRequest) {
if (periodRequest == FilterManager.PeriodRequest.FROM_TO) {
DateUtils.getInstance()
.fromCalendarSelector(this) { FilterManager.getInstance().addPeriod(it) }
} else {
DateUtils.getInstance()
.showPeriodDialog(
this,
{ datePeriods -> FilterManager.getInstance().addPeriod(datePeriods) },
true,
{ FilterManager.getInstance().addPeriod(null) },
)
}
}

override fun openOrgUnitTreeSelector() {
OUTreeFragment.Builder()
.withPreselectedOrgUnits(
FilterManager.getInstance().orgUnitFilters.map { it.uid() }.toMutableList(),
)
.onSelection { selectedOrgUnits ->
presenter.setOrgUnitFilters(selectedOrgUnits)
}
.build()
.show(supportFragmentManager, "OUTreeFragment")
}

override fun goToLogin(accountsCount: Int, isDeletion: Boolean) {
startActivity(
LoginActivity::class.java,
Expand All @@ -373,6 +458,19 @@ class MainActivity :
binding.executePendingBindings()
}

private fun setFilterButtonVisibility(showFilterButton: Boolean) {
binding.filterActionButton.visibility = if (showFilterButton) {
View.VISIBLE
} else {
View.GONE
}
binding.syncActionButton.visibility = if (showFilterButton) {
View.VISIBLE
} else {
View.GONE
}
}

override fun openDrawer(gravity: Int) {
if (!binding.mainDrawerLayout.isDrawerOpen(gravity)) {
binding.mainDrawerLayout.openDrawer(gravity)
Expand All @@ -381,6 +479,14 @@ class MainActivity :
}
}

override fun setFilters(filters: List<FilterItem>) {
newAdapter.submitList(filters)
}

override fun hideFilters() {
binding.filterActionButton.visibility = View.GONE
}

override fun onLockClick() {
if (!presenter.isPinStored()) {
binding.mainDrawerLayout.closeDrawers()
Expand Down Expand Up @@ -425,16 +531,19 @@ class MainActivity :
}

override fun onDrawerStateChanged(newState: Int) {
// no op
}

override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
// no op
}

override fun onDrawerClosed(drawerView: View) {
initCurrentScreen()
}

override fun onDrawerOpened(drawerView: View) {
// no op
}

private fun initCurrentScreen() {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/dhis2/usescases/main/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import dhis2.org.analytics.charts.Charts
import org.dhis2.commons.di.dagger.PerActivity
import org.dhis2.commons.featureconfig.data.FeatureConfigRepository
import org.dhis2.commons.filters.FilterManager
import org.dhis2.commons.filters.FiltersAdapter
import org.dhis2.commons.filters.data.FilterRepository
import org.dhis2.commons.matomo.MatomoAnalyticsController
import org.dhis2.commons.prefs.PreferenceProvider
import org.dhis2.commons.resources.ColorUtils
Expand All @@ -32,6 +34,7 @@ class MainModule(val view: MainView, private val forceToNotSynced: Boolean) {
preferences: PreferenceProvider,
workManagerController: WorkManagerController,
filterManager: FilterManager,
filterRepository: FilterRepository,
matomoAnalyticsController: MatomoAnalyticsController,
userManager: UserManager,
deleteUserData: DeleteUserData,
Expand All @@ -47,6 +50,7 @@ class MainModule(val view: MainView, private val forceToNotSynced: Boolean) {
preferences,
workManagerController,
filterManager,
filterRepository,
matomoAnalyticsController,
userManager,
deleteUserData,
Expand Down Expand Up @@ -74,6 +78,12 @@ class MainModule(val view: MainView, private val forceToNotSynced: Boolean) {
return HomeRepositoryImpl(d2, charts, featureConfigRepositoryImpl)
}

@Provides
@PerActivity
fun provideNewFiltersAdapter(): FiltersAdapter {
return FiltersAdapter()
}

@Provides
@PerActivity
fun providePageConfigurator(
Expand Down
62 changes: 61 additions & 1 deletion app/src/main/java/org/dhis2/usescases/main/MainPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import androidx.work.ExistingWorkPolicy
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.disposables.CompositeDisposable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand All @@ -15,6 +16,7 @@ import kotlinx.coroutines.launch
import org.dhis2.BuildConfig
import org.dhis2.commons.Constants
import org.dhis2.commons.filters.FilterManager
import org.dhis2.commons.filters.data.FilterRepository
import org.dhis2.commons.matomo.Actions.Companion.BLOCK_SESSION_PIN
import org.dhis2.commons.matomo.Actions.Companion.OPEN_ANALYTICS
import org.dhis2.commons.matomo.Actions.Companion.QR_SCANNER
Expand All @@ -40,6 +42,7 @@ import org.dhis2.usescases.login.SyncIsPerformedInteractor
import org.dhis2.usescases.settings.DeleteUserData
import org.dhis2.usescases.sync.WAS_INITIAL_SYNC_DONE
import org.dhis2.utils.TRUE
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit
import org.hisp.dhis.android.core.systeminfo.SystemInfo
import org.hisp.dhis.android.core.user.User
import timber.log.Timber
Expand All @@ -57,6 +60,7 @@ class MainPresenter(
private val preferences: PreferenceProvider,
private val workManagerController: WorkManagerController,
private val filterManager: FilterManager,
private val filterRepository: FilterRepository,
private val matomoAnalyticsController: MatomoAnalyticsController,
private val userManager: UserManager,
private val deleteUserData: DeleteUserData,
Expand All @@ -77,7 +81,6 @@ class MainPresenter(
val downloadingVersion = MutableLiveData(false)

fun init() {
filterManager.clearAllFilters()
preferences.removeValue(Preference.CURRENT_ORG_UNIT)
disposable.add(
repository.user()
Expand Down Expand Up @@ -117,6 +120,54 @@ class MainPresenter(
trackDhis2Server()
}

fun initFilters() {
disposable.add(
Flowable.just(filterRepository.homeFilters())
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ filters ->
if (filters.isEmpty()) {
view.hideFilters()
} else {
view.setFilters(filters)
}
},
{ Timber.e(it) },
),
)

disposable.add(
filterManager.asFlowable()
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ filterManager -> view.updateFilters(filterManager.totalFilters) },
{ Timber.e(it) },
),
)

disposable.add(
filterManager.periodRequest
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ periodRequest -> view.showPeriodRequest(periodRequest.first) },
{ Timber.e(it) },
),
)

disposable.add(
filterManager.ouTreeFlowable()
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(
{ view.openOrgUnitTreeSelector() },
{ Timber.e(it) },
),
)
}

fun trackDhis2Server() {
disposable.add(
repository.getServerVersion()
Expand Down Expand Up @@ -145,6 +196,10 @@ class MainPresenter(
}
}

fun setOrgUnitFilters(selectedOrgUnits: List<OrganisationUnit>) {
filterManager.addOrgUnits(selectedOrgUnits)
}

private fun getUserUid(): String {
return try {
userManager.d2.userModule().user().blockingGet()?.uid() ?: ""
Expand Down Expand Up @@ -201,6 +256,10 @@ class MainPresenter(
view.back()
}

fun showFilter() {
view.showHideFilter()
}

fun onDetach() {
disposable.clear()
}
Expand All @@ -219,6 +278,7 @@ class MainPresenter(

fun onNavigateBackToHome() {
view.goToHome()
initFilters()
}

fun onClickSyncManager() {
Expand Down
Loading

0 comments on commit 97ff43f

Please sign in to comment.