Author: Adam Pioterek <adam.pioterek@protonmail.ch>
in-a-moment & consistent time-till
%!v(PANIC=String method: strings: negative Repeat count)
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 90ec0eeb951bc1022bbea28479cf78c72a499416..cda5767a48380ef82399f1c84cf44fa818d7ccfa 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt @@ -172,7 +172,7 @@ adapter.departures = fullDepartures[(dateSpinner.selectedItem as ServiceAdapter.RowItem).service] } else { val now = Calendar.getInstance() val seconds = now.secondsAfterMidnight() - adapter.departures = this.departures.flatMap { it.value }.sortedBy { it.timeTill(seconds) } + adapter.departures = this.departures.flatMap { it.value }.sortedBy { it.timeTill(seconds) } // todo sorted by also onStop } adapter.notifyDataSetChanged() } diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt index 4435b56a85f6bd29a43e3de7a3e1e832eb6b1b58..a54cdf20ac0431df54961046c7f02b82490d3f36 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt @@ -1,6 +1,11 @@ package ml.adamsprogs.bimba.models +import android.content.Context +import ml.adamsprogs.bimba.Declinator +import ml.adamsprogs.bimba.R +import ml.adamsprogs.bimba.rollTime import ml.adamsprogs.bimba.safeSplit +import java.util.* data class Departure(val line: String, val mode: List<Int>, val time: Int, val lowFloor: Boolean, //time in seconds since midnight val modification: List<String>, val headsign: String, val vm: Boolean = false, @@ -41,4 +46,38 @@ return (time - relativeTo) / 60 } val lineText: String = line + + fun timeTillText(context: Context, relativeTime: Boolean = true): String { + val now = Calendar.getInstance() + val departureTime = Calendar.getInstance().rollTime(time) + if (tomorrow) + departureTime.add(Calendar.DAY_OF_MONTH, 1) + + val departureIn = ((departureTime.timeInMillis - now.timeInMillis) / (1000 * 60)).toInt() + + return if (departureIn > 60 || departureIn < 0 || !relativeTime) + context.getString(R.string.departure_at, "${String.format("%02d", departureTime.get(Calendar.HOUR_OF_DAY))}:${String.format("%02d", departureTime.get(Calendar.MINUTE))}") + else if (departureIn > 0 && !onStop) + context.getString(Declinator.decline(departureIn), departureIn.toString()) + else if (departureIn == 0 && !onStop) + context.getString(R.string.in_a_moment) + else if (departureIn == 0) + context.getString(R.string.now) + else + context.getString(R.string.just_departed) + } + + fun timeAtMessage(context: Context): String { + val departureTime = Calendar.getInstance().rollTime(time) + if (tomorrow) + departureTime.add(Calendar.DAY_OF_MONTH, 1) + + return context.getString(R.string.departure_at, + "${String.format("%02d", + departureTime.get(Calendar.HOUR_OF_DAY))}:${String.format("%02d", + departureTime.get(Calendar.MINUTE))}") + + if (isModified) + " " + modification.joinToString("; ", "(", ")") + else "" + } } \ No newline at end of file diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/DeparturesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/DeparturesAdapter.kt index 0ee09a73d37a03a364fde72c3a6dd229c96d694d..f0f16dfbc2366b77d7d65f2361b06ac63724f5cd 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/DeparturesAdapter.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/DeparturesAdapter.kt @@ -9,11 +9,8 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.core.content.res.ResourcesCompat -import ml.adamsprogs.bimba.Declinator import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.models.Departure -import ml.adamsprogs.bimba.rollTime -import java.util.* class DeparturesAdapter(val context: Context, var departures: List<Departure>?, var relativeTime: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<DeparturesAdapter.ViewHolder>() { @@ -40,20 +37,8 @@ time.text = context.getString(R.string.no_departures) return } val departure = departures!![position] - val now = Calendar.getInstance() - val departureTime = Calendar.getInstance().rollTime(departure.time) - if (departure.tomorrow) - departureTime.add(Calendar.DAY_OF_MONTH, 1) - val departureIn = ((departureTime.timeInMillis - now.timeInMillis) / (1000 * 60)).toInt() - val timeString: String - - timeString = if (departureIn > 60 || departureIn < 0 || !relativeTime) - context.getString(R.string.departure_at, "${String.format("%02d", departureTime.get(Calendar.HOUR_OF_DAY))}:${String.format("%02d", departureTime.get(Calendar.MINUTE))}") - else if (departureIn > 0 && !departure.onStop) - context.getString(Declinator.decline(departureIn), departureIn.toString()) - else - context.getString(R.string.now) + val timeString = departure.timeTillText(context, relativeTime) line.text = departure.lineText time.text = timeString @@ -73,14 +58,7 @@ AlertDialog.Builder(context) .setPositiveButton(context.getText(android.R.string.ok) ) { dialog: DialogInterface, _: Int -> dialog.cancel() } .setCancelable(true) - .setMessage( - context.getString(R.string.departure_at, - "${String.format("%02d", - departureTime.get(Calendar.HOUR_OF_DAY))}:${String.format("%02d", - departureTime.get(Calendar.MINUTE))}") - + if (departure.isModified) - " " + departure.modification.joinToString("; ", "(", ")") - else "") + .setMessage(departure.timeAtMessage(context)) .create().show() } } diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt index 0f97754e7e1a7a65ef5703f4beaa434fbac3163c..3762d36f92937345fefd52ea89c97aa608d52b2e 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouritesAdapter.kt @@ -14,11 +14,9 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.android.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import ml.adamsprogs.bimba.Declinator import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.collections.FavouriteStorage import ml.adamsprogs.bimba.models.Favourite -import ml.adamsprogs.bimba.secondsAfterMidnight import java.util.* @@ -82,12 +80,8 @@ val nextDepartureText: String val nextDepartureLineText: String if (nextDeparture != null) { - val interval = nextDeparture.timeTill(Calendar.getInstance().secondsAfterMidnight()) nextDepartureLineText = appContext.getString(R.string.departure_to_line, nextDeparture.line, nextDeparture.headsign) - nextDepartureText = if (interval < 0) - appContext.getString(R.string.just_departed) - else - appContext.getString(Declinator.decline(interval), interval.toString()) + nextDepartureText = nextDeparture.timeTillText(appContext) } else { nextDepartureText = appContext.getString(R.string.no_next_departure) nextDepartureLineText = "" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 88c0ef9490f6e2d0b91f440edd01389e43037b8c..cd4e74e8f7c1bd2633436a5a744ffba86404fb05 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -69,4 +69,5 @@Server error <string name="suggestion_row_image" translatable="false">suggestion row image</string> <string name="nothing_found">Nothing found</string> <string name="departures_empty_state_icon" translatable="false">departures empty state icon</string> + <string name="in_a_moment">In a moment</string> </resources> diff --git a/todo b/todo index 62196df2b060dc659ca9f94b58289aaca79c3dfe..48a6c79ed5f0f796aa1ad540a9ca20d6e70c12b1 100644 --- a/todo +++ b/todo @@ -11,4 +11,5 @@ - shed retry - departures retry x search results flickering - favourite cache -- in a moment +v in a moment +- departures sort (timeTill, onStop) : StopActivity