1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

fix the download progress bar logic and encapsulate it in FadeOutViewMediator

This commit is contained in:
Christophe Beyls 2020-04-21 20:00:12 +02:00
parent 7f282c4418
commit dc02336785
2 changed files with 60 additions and 27 deletions

View file

@ -1,7 +1,5 @@
package be.digitalia.fosdem.activities
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import android.app.SearchManager
import android.content.ActivityNotFoundException
@ -27,7 +25,6 @@ import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.edit
import androidx.core.content.getSystemService
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
@ -41,6 +38,7 @@ import be.digitalia.fosdem.db.AppDatabase
import be.digitalia.fosdem.fragments.*
import be.digitalia.fosdem.model.DownloadScheduleResult
import be.digitalia.fosdem.utils.*
import be.digitalia.fosdem.widgets.FadeOutViewMediator
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CancellationException
@ -96,36 +94,25 @@ class MainActivity : AppCompatActivity(R.layout.main), CreateNfcAppDataCallback
// Progress bar setup
val progressBar: ProgressBar = findViewById(R.id.progress)
val progressBarMediator = FadeOutViewMediator(progressBar)
FosdemApi.downloadScheduleProgress.observe(this) { progressValue ->
progressBar.apply {
if (progressValue != 100) {
// Visible
if (!isVisible) {
clearAnimation()
isVisible = true
}
if (progressValue != 100) {
// Visible
progressBarMediator.isVisible = true
with(progressBar) {
if (progressValue == -1) {
isIndeterminate = true
} else {
isIndeterminate = false
progress = progressValue
}
} else {
// Invisible
if (isVisible) {
// Hide the progress bar with a fill and fade out animation
isIndeterminate = false
progress = 100
animate()
.alpha(0f)
.withLayer()
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
isVisible = false
alpha = 1f
}
})
}
}
} else {
// Invisible
progressBarMediator.isVisible = false
with(progressBar) {
isIndeterminate = false
progress = 100
}
}
}
@ -279,7 +266,7 @@ class MainActivity : AppCompatActivity(R.layout.main), CreateNfcAppDataCallback
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
this.searchMenuItem = menu.findItem(R.id.search)?.apply {
searchMenuItem = menu.findItem(R.id.search)?.apply {
fixCollapsibleActionView()
// Associate searchable configuration with the SearchView
val searchManager: SearchManager? = getSystemService()

View file

@ -0,0 +1,46 @@
package be.digitalia.fosdem.widgets
import android.animation.ObjectAnimator
import android.view.View
import androidx.core.animation.addListener
import androidx.core.view.isVisible
/**
* Hide a view with a fade out animation.
* Cancel the animation if the view needs to be visible again.
*/
class FadeOutViewMediator(private val view: View) {
private val fadeOutAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f).apply {
duration = 500L
addListener(
onStart = {
view.setLayerType(View.LAYER_TYPE_HARDWARE, null)
},
onEnd = {
view.setLayerType(View.LAYER_TYPE_NONE, null)
view.alpha = 1f
if (!isVisible) {
view.isVisible = false
}
}
)
}
var isVisible = view.isVisible
set(value) {
if (field == value) {
return
}
field = value
if (value) {
if (fadeOutAnimator.isRunning) {
fadeOutAnimator.cancel()
} else {
view.isVisible = true
}
} else {
fadeOutAnimator.start()
}
}
}