-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Routing, Feedback, Settings & Calendar (#246)
- Loading branch information
1 parent
3544f24
commit 11c6e27
Showing
73 changed files
with
702 additions
and
782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 25 additions & 20 deletions
45
android/app/src/main/kotlin/de/tum/in/tumcampus/util/DateTimeSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
package de.tum.`in`.tumcampus.util | ||
|
||
import com.google.gson.JsonDeserializationContext | ||
import com.google.gson.JsonDeserializer | ||
import com.google.gson.JsonElement | ||
import org.joda.time.DateTime | ||
import org.joda.time.format.DateTimeFormat | ||
import java.lang.reflect.Type | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializer | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
class LocalDateTimeDeserializer : JsonDeserializer<DateTime?> { | ||
override fun deserialize( | ||
json: JsonElement?, | ||
typeOfT: Type?, | ||
context: JsonDeserializationContext? | ||
): DateTime? { | ||
return deserializeStringToDate(json?.asString) | ||
@OptIn(ExperimentalSerializationApi::class) | ||
@Serializer(forClass = LocalDateTime::class) | ||
object DateTimeSerializer : KSerializer<LocalDateTime> { | ||
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") | ||
|
||
override fun serialize(encoder: Encoder, value: LocalDateTime) { | ||
encoder.encodeString(value.format(formatter)) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): LocalDateTime { | ||
return LocalDateTime.parse(decoder.decodeString(), formatter) | ||
} | ||
} | ||
|
||
fun deserializeStringToDate(dateString: String?): DateTime? { | ||
return try { | ||
val formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS") | ||
DateTime.parse(dateString, formatter) | ||
} catch (_: Exception) { | ||
null | ||
fun deserializeStringToDate(dateString: String?): LocalDateTime? { | ||
return try { | ||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS") | ||
LocalDateTime.parse(dateString, formatter) | ||
} catch (_: Exception) { | ||
null | ||
} | ||
} | ||
} |
150 changes: 24 additions & 126 deletions
150
android/app/src/main/kotlin/de/tum/in/tumcampus/util/DateTimeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,37 @@ | ||
package de.tum.`in`.tumcampus.util | ||
|
||
import android.content.Context | ||
import android.text.format.DateUtils.* | ||
import de.tum.`in`.tumcampus.R | ||
import org.joda.time.DateTime | ||
import org.joda.time.DateTimeZone | ||
import org.joda.time.format.DateTimeFormat | ||
import org.joda.time.format.DateTimeFormatter | ||
import java.text.ParseException | ||
import java.util.* | ||
import java.time.Duration | ||
import java.time.LocalDateTime | ||
|
||
object DateTimeUtils { | ||
/** | ||
* Format an upcoming string nicely by being more precise as time comes closer | ||
* E.g.: | ||
* in 30 minutes | ||
* in 2 h 4 min | ||
* in 6 hours | ||
* tomorrow | ||
* @see getRelativeTimeSpanString() | ||
*/ | ||
private fun formatFutureTime(time: DateTime, context: Context): String { | ||
val timeInMillis = time.millis | ||
val now = DateTime.now().millis | ||
|
||
// Catch future dates: current clock might be running behind | ||
if (timeInMillis < now || timeInMillis <= 0) return formatTimeOrDay(time, context) | ||
|
||
val diff = timeInMillis - now | ||
return when { | ||
diff < 60 * MINUTE_IN_MILLIS -> { | ||
val formatter = DateTimeFormat.forPattern("m") | ||
.withLocale(Locale.ENGLISH) | ||
"${context.getString(R.string.IN)} ${ | ||
formatter.print( | ||
DateTime( | ||
diff, | ||
DateTimeZone.UTC | ||
) | ||
) | ||
} " + | ||
context.getString(R.string.MINUTES) | ||
} | ||
|
||
diff < 5 * HOUR_IN_MILLIS -> { | ||
val formatter = DateTimeFormat.forPattern("h 'h' m 'min'") | ||
.withLocale(Locale.ENGLISH) | ||
"${context.getString(R.string.IN)} ${ | ||
formatter.print( | ||
DateTime( | ||
diff, | ||
DateTimeZone.UTC | ||
) | ||
) | ||
}" | ||
} | ||
|
||
else -> getRelativeTimeSpanString( | ||
timeInMillis, now, MINUTE_IN_MILLIS, | ||
FORMAT_ABBREV_RELATIVE | ||
).toString() | ||
} | ||
} | ||
|
||
/** | ||
* @Deprecated use formatTimeOrDay(DateTime, Context) | ||
*/ | ||
@Deprecated("Use the version with a proper DateTime object, there's really no reason to pass datetimes as strings") | ||
fun formatTimeOrDayFromISO(datetime: String, context: Context): String { | ||
val d = parseIsoDate(datetime) ?: return "" | ||
return formatTimeOrDay(d, context) | ||
} | ||
|
||
/** | ||
* Format a *past* ISO string timestamp with degrading granularity as time goes by | ||
* E.g.: | ||
* Just now | ||
* 18:20 | ||
* Yesterday | ||
* 12.03.2016 | ||
* Checks whether two DateTime contain the same day | ||
* | ||
* Please note, that this does *not* use getRelativeTimeSpanString(), because lectures scheduled | ||
* at 12:00 and starting at 12:15 get a bit annoying nagging you with "12 minutes ago", when | ||
* they actually only start in a couple of minutes | ||
* This is similar to formatFutureTime(), but not specialized on future dates | ||
* When in doubt, use formatFutureTime() | ||
* @see formatFutureTime() | ||
* @return true if both dates are on the same day | ||
*/ | ||
private fun formatTimeOrDay(time: DateTime, context: Context): String { | ||
val timeInMillis = time.millis | ||
val now = DateTime.now().millis | ||
|
||
// Catch future dates: current clock might be running behind | ||
if (timeInMillis > now || timeInMillis <= 0) { | ||
return context.getString(R.string.just_now) | ||
} | ||
fun isSameDay(first: LocalDateTime, second: LocalDateTime) = | ||
first.year == second.year && first.dayOfYear == second.dayOfYear | ||
|
||
val diff = now - timeInMillis | ||
return when { | ||
diff < MINUTE_IN_MILLIS -> | ||
context.getString(R.string.just_now) | ||
|
||
diff < 24 * HOUR_IN_MILLIS -> | ||
DateTimeFormat.forPattern("HH:mm") | ||
.withLocale(Locale.ENGLISH) | ||
.print(time) | ||
|
||
diff < 48 * HOUR_IN_MILLIS -> | ||
context.getString(R.string.yesterday) | ||
|
||
else -> | ||
DateTimeFormat.forPattern("dd.MM.yyyy") | ||
.withLocale(Locale.ENGLISH) | ||
.print(time) | ||
} | ||
} | ||
|
||
/** | ||
* 2014-06-30T16:31:57Z | ||
*/ | ||
private val isoDateFormatter: DateTimeFormatter = | ||
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") | ||
} | ||
|
||
private fun parseIsoDate(datetime: String) = try { | ||
isoDateFormatter.parseDateTime(datetime) | ||
} catch (e: ParseException) { | ||
//Utils.log(e) | ||
null | ||
fun LocalDateTime.timeAgo(context: Context): String { | ||
val now = LocalDateTime.now() | ||
val duration = Duration.between(this, now) | ||
|
||
val years = duration.toDays() / 365 | ||
val months = duration.toDays() / 30 | ||
val days = duration.toDays() | ||
val hours = duration.toHours() | ||
val minutes = duration.toMinutes() | ||
|
||
return when { | ||
years > 0 -> context.resources.getQuantityString(R.plurals.yearsAgo, years.toInt()) | ||
months > 0 -> context.resources.getQuantityString(R.plurals.monthsAgo, months.toInt()) | ||
days > 0 -> context.resources.getQuantityString(R.plurals.daysAgo, days.toInt()) | ||
hours > 0 -> context.resources.getQuantityString(R.plurals.hoursAgo, hours.toInt()) | ||
minutes > 0 -> context.resources.getQuantityString(R.plurals.minutesAgo, minutes.toInt()) | ||
else -> context.resources.getString(R.string.just_now) | ||
} | ||
|
||
/** | ||
* Checks whether two DateTime contain the same day | ||
* | ||
* @return true if both dates are on the same day | ||
*/ | ||
fun isSameDay(first: DateTime, second: DateTime) = | ||
first.year() == second.year() && first.dayOfYear() == second.dayOfYear() | ||
} |
8 changes: 0 additions & 8 deletions
8
android/app/src/main/kotlin/de/tum/in/tumcampus/widgets/calendar/CalendarEventType.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.