Bimba.git

commit 1913a6e05f9bade3a0e4fb9d527dd88e6dbff7c8

Author: Adam Pioterek <adam.pioterek@protonmail.ch>

hiding fab on scroll

 app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt | 37 +
 app/src/main/res/layout/activity_stop.xml | 1 


diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
index 03f2c61f6eb43219c44dfa164d82d4346527ca86..2825df4e54d376d7b431c928a3ac40d1116940b1 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -74,6 +74,8 @@             }
             else -> null
         }
 
+        showFab()
+
         sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, departures)
 
         container.adapter = sectionsPagerAdapter
@@ -82,8 +84,6 @@         container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
         tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
 
         selectTodayPage()
-
-        showFab()
 
         prepareOnDownloadListener()
     }
@@ -144,6 +144,17 @@             }
         }
     }
 
+    //fixme<p:5> maybe better effects
+    private fun updateFabVisibility(dy: Int) {
+        if (fab == null)
+            return
+        if (dy > 0) {
+            fab.hide()
+        } else {
+            fab.show()
+        }
+    }
+
     private fun prepareOnDownloadListener() {
         val filter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
         filter.addAction(VmClient.ACTION_READY)
@@ -166,7 +177,7 @@         // println("onVm")
         if (vmDepartures == null && this.vmDepartures.isEmpty() && hasDepartures) {
             // println("\tbut noVM")
             if (ticked()) {
-               //  println("\t\tbut ticked")
+                //  println("\t\tbut ticked")
                 refreshAdapterFromStop()
             }
             return
@@ -254,9 +265,9 @@         var relativeTime = true
 
         override fun getItem(position: Int): Fragment {
             if (departures == null)
-                return PlaceholderFragment.newInstance(null, relativeTime)
+                return PlaceholderFragment.newInstance(null, relativeTime) { updateFabVisibility(it) }
             if (departures!!.isEmpty())
-                return PlaceholderFragment.newInstance(ArrayList(), relativeTime)
+                return PlaceholderFragment.newInstance(ArrayList(), relativeTime) { updateFabVisibility(it) }
             val sat = try {
                 timetable.getServiceFor(Calendar.SATURDAY)
             } catch (e: IllegalArgumentException) {
@@ -279,7 +290,7 @@                     ArrayList()
                 }
                 else -> throw IndexOutOfBoundsException("No tab at index $position")
             }
-            return PlaceholderFragment.newInstance(list, relativeTime)
+            return PlaceholderFragment.newInstance(list, relativeTime) { updateFabVisibility(it) }
         }
 
         override fun getCount() = 3
@@ -293,7 +304,7 @@             return Calendar.getInstance().getMode()
         }
     }
 
-    class PlaceholderFragment : Fragment() {
+    class PlaceholderFragment(val updater: (Int) -> Unit) : Fragment() {
         override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
             val rootView = inflater.inflate(R.layout.fragment_stop, container, false)
 
@@ -307,12 +318,20 @@
             departuresList.adapter = DeparturesAdapter(activity as Context, departures,
                     arguments?.get("relativeTime") as Boolean)
             departuresList.layoutManager = layoutManager
+
+            departuresList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
+                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {}
+                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
+                    updater(dy)
+                    super.onScrolled(recyclerView, dx, dy)
+                }
+            })
             return rootView
         }
 
         companion object {
-            fun newInstance(departures: List<Departure>?, relativeTime: Boolean): PlaceholderFragment {
-                val fragment = PlaceholderFragment()
+            fun newInstance(departures: List<Departure>?, relativeTime: Boolean, updater: (Int) -> Unit): PlaceholderFragment {
+                val fragment = PlaceholderFragment(updater)
                 val args = Bundle()
                 if (departures != null) {
                     if (departures.isNotEmpty()) {




diff --git a/app/src/main/res/layout/activity_stop.xml b/app/src/main/res/layout/activity_stop.xml
index 069577f38984997e4c54e2918bef8d6506234a14..cad4b10f71491458bd9cc388e9db098c409a41da 100644
--- a/app/src/main/res/layout/activity_stop.xml
+++ b/app/src/main/res/layout/activity_stop.xml
@@ -58,7 +58,6 @@         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:layout_behavior="@string/appbar_scrolling_view_behavior" />
 
-    <!-- todo behaviour -->
     <android.support.design.widget.FloatingActionButton
         android:id="@+id/fab"
         android:layout_width="wrap_content"