Bimba.git

commit 6484ce8d2f4b5e4c08b0b45e5b4e1e648c6c38a1

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

date format in drawer

 app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt | 23 +
 app/src/main/java/ml/adamsprogs/bimba/extensions.kt | 12 


diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
index 2134f12397b6378e7fc50ec698b48a11e3a33f88..683eab7c01f99fe84388609b99d4006e00fd4575 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -15,16 +15,17 @@ import kotlin.concurrent.thread
 import kotlin.collections.ArrayList
 import kotlinx.android.synthetic.main.activity_dash.*
 import java.util.*
+import java.text.*
 
 import ml.adamsprogs.bimba.models.*
 import ml.adamsprogs.bimba.*
 import ml.adamsprogs.bimba.datasources.*
 import ml.adamsprogs.bimba.models.suggestions.*
+import ml.adamsprogs.bimba.models.adapters.*
+import ml.adamsprogs.bimba.collections.*
 
 import com.arlib.floatingsearchview.FloatingSearchView
 import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion
-import ml.adamsprogs.bimba.collections.FavouriteStorage
-import ml.adamsprogs.bimba.models.adapters.FavouritesAdapter
 
 //todo<p:1> searchView integration
 class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener,
@@ -82,10 +83,7 @@             drawerLayout.closeDrawer(drawerView)
             super.onOptionsItemSelected(item)
         }
 
-        val validSince = timetable.getValidSince()
-        val validTill = timetable.getValidTill()
-        drawerView.menu.findItem(R.id.drawer_validity_since).title = getString(R.string.valid_since, validSince) //todo date format
-        drawerView.menu.findItem(R.id.drawer_validity_till).title = getString(R.string.valid_till, validTill) //todo date format
+        showValidityInDrawer()
 
         searchView = search_view
 
@@ -142,6 +140,16 @@             iconView.setImageDrawable(getDrawable(suggestion.getIcon(), context))
         }
 
         searchView.attachNavigationDrawerToMenuButton(drawer_layout as DrawerLayout)
+    }
+
+    private fun showValidityInDrawer() {
+        val formatter = DateFormat.getDateInstance(DateFormat.SHORT)
+        var calendar = calendarFromIsoD(timetable.getValidSince())
+        formatter.timeZone = calendar.timeZone
+        drawerView.menu.findItem(R.id.drawer_validity_since).title = getString(R.string.valid_since, formatter.format(calendar.time))
+        calendar = calendarFromIsoD(timetable.getValidTill())
+        formatter.timeZone = calendar.timeZone
+        drawerView.menu.findItem(R.id.drawer_validity_till).title = getString(R.string.valid_till, formatter.format(calendar.time))
     }
 
     private fun filterSuggestions(newQuery: String) {
@@ -285,8 +293,7 @@         if (result == TimetableDownloader.RESULT_FINISHED) {
             timetable = Timetable.getTimetable(this, true)
             getSuggestions()
 
-            drawerView.menu.findItem(R.id.drawer_validity_since).title = getString(R.string.valid_since, timetable.getValidSince())
-            drawerView.menu.findItem(R.id.drawer_validity_till).title = getString(R.string.valid_since, timetable.getValidTill())
+            showValidityInDrawer()
         }
     }
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
index 869d2e6b5201d15a2da4f46ef93f9fce2111227c..b402854d0f1c67823aceb116cc38f19da783d694 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
@@ -36,11 +36,21 @@     return "$year$month$day"
 }
 
 const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
+const val ISO_8601_DATE_ONLY_FORMAT = "yyyyMMdd"
 
 @SuppressLint("SimpleDateFormat")
 fun calendarFromIso(iso: String): Calendar {
     val calendar = Calendar.getInstance()
     val dateFormat = SimpleDateFormat(ISO_8601_DATE_FORMAT)
+    val date = dateFormat.parse(iso)
+    calendar.time = date
+    return calendar
+}
+
+@SuppressLint("SimpleDateFormat")
+fun calendarFromIsoD(iso: String): Calendar {
+    val calendar = Calendar.getInstance()
+    val dateFormat = SimpleDateFormat(ISO_8601_DATE_ONLY_FORMAT)
     val date = dateFormat.parse(iso)
     calendar.time = date
     return calendar
@@ -82,7 +92,7 @@     return dirs[0]
 //    return dirs[dirs.size - 1]
 }
 
-internal fun InputStream.listenableCopyTo(out: OutputStream, bufferSize: Int = DEFAULT_BUFFER_SIZE, listener: (Long) -> Unit): Long {
+internal fun InputStream.listenableCopyTo(out: OutputStream, bufferSize: Int = DEFAULT_BUFFER_SIZE, listener: (Long) -> Unit): Long { //fixme may devour RAM?
     var bytesCopied: Long = 0
     val buffer = ByteArray(bufferSize)
     var bytes = read(buffer)