Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/org/mtransit/android/commons/TimeUtilsK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ object TimeUtilsK {
}.trim()

fun currentInstant() = TimeUtils.currentTimeMillis().millisToInstant()

val EPOCH_TIME_0: Instant = 0L.millisToInstant()
}

fun Duration.formatSimpleDuration() = this.inWholeMilliseconds.let { TimeUtilsK.formatSimpleDuration(it) }
Expand All @@ -30,9 +32,6 @@ fun Long.millisToInstant() = Instant.fromEpochMilliseconds(this)

fun Long.secsToInstant() = Instant.fromEpochSeconds(this)

@Suppress("unused")
fun Int.secsToInstant() = this.toLong().secsToInstant()

fun Instant.toMillis() = this.toEpochMilliseconds()

fun Instant.toSecs() = this.epochSeconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public int getType() {
@Override
public String getUUID() {
if (this.uuid == null) {
this.uuid = POI.POIUtils.getUUID(getAuthority(), getId());
this.uuid = POI.POIUtils.makeUUID(getAuthority(), getId());
}
return this.uuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public String getAuthority() {
@NonNull
@Override
public String getUUID() {
return POI.POIUtils.getUUID(this.authority, this.routeId, this.id);
return POI.POIUtils.makeUUID(this.authority, this.routeId, this.id);
}

public long getId() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mtransit/android/commons/data/POI.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getLogTag() {
public static final String UID_SEPARATOR = "-";

@NonNull
public static String getUUID(@NonNull String authority, @NonNull Object... poiUIDs) {
public static String makeUUID(@NonNull String authority, @NonNull Object... poiUIDs) {
StringBuilder sb = new StringBuilder(authority);
for (Object poiUID : poiUIDs) {
sb.append(UID_SEPARATOR).append(poiUID);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mtransit/android/commons/data/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public long getId() {
@Override
public String getUUID() {
if (this.uuid == null) {
this.uuid = POI.POIUtils.getUUID(this.authority, this.id);
this.uuid = POI.POIUtils.makeUUID(this.authority, this.id);
}
return this.uuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public String getUUID() {

@NonNull
public static String makeUUID(@NonNull String authority, long routeId, long directionId, int stopId) {
return POI.POIUtils.getUUID(authority, routeId, directionId, stopId);
return POI.POIUtils.makeUUID(authority, routeId, directionId, stopId);
}

@Override
Expand Down
83 changes: 70 additions & 13 deletions src/main/java/org/mtransit/android/commons/data/ServiceUpdateKtx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.mtransit.android.commons.MTLog
import org.mtransit.android.commons.StringUtils
import org.mtransit.android.commons.TimeUtils
import org.mtransit.android.commons.provider.serviceupdate.ServiceUpdateProviderContract
import org.mtransit.android.commons.toMillis
import kotlin.time.Duration
import kotlin.time.Instant

fun ServiceUpdate.syncTargetUUID(targetUUIDs: Map<String, String>?) {
targetUUIDs?.takeIf { it.isNotEmpty() } ?: return
Expand All @@ -14,13 +17,15 @@ fun ServiceUpdate.syncTargetUUID(targetUUIDs: Map<String, String>?) {
}
}

@Suppress("unused") // main app only
fun Iterable<ServiceUpdate>?.isSeverityWarningInfo(): Pair<Boolean, Boolean> {
this ?: return false to false
if (any { it.isSeverityWarning }) return true to false
if (any { it.isSeverityInfo }) return false to true
return false to false
}

@Suppress("unused") // main app only
fun Iterable<ServiceUpdate>.distinctByOriginalId() =
this.distinctBy { it.originalId ?: it.id } // keep 1st occurrence from sorted list (in *Manager)

Expand All @@ -30,17 +35,69 @@ fun ServiceUpdateProviderContract.makeServiceUpdateNoneList(targetable: Targetab
}

fun ServiceUpdateProviderContract.makeServiceUpdateNone(targetUUID: String, sourceId: String) =
ServiceUpdate(
null,
targetUUID,
null,
TimeUtils.currentTimeMillis(),
getServiceUpdateMaxValidityInMs(),
StringUtils.EMPTY,
null,
ServiceUpdate.SEVERITY_NONE,
sourceId,
StringUtils.EMPTY,
null,
getServiceUpdateLanguage(),
makeServiceUpdate(
targetUUID = targetUUID,
lastUpdateMs = TimeUtils.currentTimeMillis(),
maxValidityMs = getServiceUpdateMaxValidityInMs(),
text = StringUtils.EMPTY,
severity = ServiceUpdate.SEVERITY_NONE,
sourceId = sourceId,
sourceLabel = StringUtils.EMPTY,
language = getServiceUpdateLanguage(),
)

fun makeServiceUpdate(
optId: Int? = null,
targetUUID: String,
targetTripId: String? = null,
lastUpdate: Instant,
maxValidity: Duration,
text: String,
optTextHTML: String? = null,
severity: Int,
sourceId: String,
sourceLabel: String,
originalId: String? = null,
language: String
) = makeServiceUpdate(
optId,
targetUUID,
targetTripId,
lastUpdate.toMillis(),
maxValidity.inWholeMilliseconds,
text,
optTextHTML,
severity,
sourceId,
sourceLabel,
originalId,
language,
)

fun makeServiceUpdate(
optId: Int? = null,
targetUUID: String,
targetTripId: String? = null,
lastUpdateMs: Long,
maxValidityMs: Long,
text: String,
optTextHTML: String? = null,
severity: Int,
sourceId: String,
sourceLabel: String,
originalId: String? = null,
language: String
) = ServiceUpdate(
optId,
targetUUID,
targetTripId,
lastUpdateMs,
maxValidityMs,
text,
optTextHTML,
severity,
sourceId,
sourceLabel,
originalId,
language,
)
2 changes: 1 addition & 1 deletion src/main/java/org/mtransit/android/commons/data/Stop.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public int getId() {

@NonNull
public String getUUID(@NonNull String authority) {
return POI.POIUtils.getUUID(authority, this.id);
return POI.POIUtils.makeUUID(authority, this.id);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private static String getAgencyRouteStopTargetUUID(@NonNull RouteDirectionStop r

@NonNull
private static String getAgencyRouteStopTargetUUID(String agencyAuthority, String routeShortName, String stopCode) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName, stopCode);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName, stopCode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected static String getAgencyRouteStopTargetUUID(@NonNull String agencyAutho
@NonNull String routeShortName,
@Nullable String optTripHeaSignValue,
@NonNull String stopId) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName, optTripHeaSignValue, stopId);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName, optTripHeaSignValue, stopId);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private String getAgencyRouteStopTargetUUID(@NonNull RouteDirectionStop rds) {

@NonNull
protected static String getAgencyRouteStopTargetUUID(@NonNull String agencyAuthority, @NonNull String routeShortName, @NonNull String stopCode) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName, stopCode);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName, stopCode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ private POIStatus parseAgencyJSONStationStatus(@NonNull String authority,
}
BikeStationAvailabilityPercent newBikeStationStatus = new BikeStationAvailabilityPercent(
null,
POI.POIUtils.getUUID(authority, bikeStationId),
POI.POIUtils.makeUUID(authority, bikeStationId),
newLastUpdateInMs,
statusMaxValidityInMs,
newLastUpdateInMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,42 +745,42 @@ public String getStopTag(@NonNull Stop stop) {
@Nullable
public static String getAgencyStopTagTargetUUID(@NonNull String agencyTag, @Nullable String stopTag) {
if (stopTag == null) return null;
return POI.POIUtils.getUUID(agencyTag, "si" + stopTag);
return POI.POIUtils.makeUUID(agencyTag, "si" + stopTag);
}

@NonNull
public static String getAgencyRouteTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag) {
return POI.POIUtils.getUUID(agencyTag, "ri" + routeTag);
return POI.POIUtils.makeUUID(agencyTag, "ri" + routeTag);
}

@Nullable
public static String getAgencyRouteStopTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag, @Nullable String stopTag) {
if (stopTag == null) return null;
return POI.POIUtils.getUUID(agencyTag, "ri" + routeTag, "si" + stopTag);
return POI.POIUtils.makeUUID(agencyTag, "ri" + routeTag, "si" + stopTag);
}

@Nullable
public static String getAgencyRouteTypeTagTargetUUID(@NonNull String agencyTag, @Nullable Integer routeType) {
if (routeType == null) return null;
return POI.POIUtils.getUUID(agencyTag, "t" + routeType);
return POI.POIUtils.makeUUID(agencyTag, "t" + routeType);
}

@Nullable
public static String getAgencyRouteDirectionTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag, @Nullable Integer directionTag) {
if (directionTag == null) return null;
return POI.POIUtils.getUUID(agencyTag, "ri" + routeTag, "d" + directionTag);
return POI.POIUtils.makeUUID(agencyTag, "ri" + routeTag, "d" + directionTag);
}

@Nullable
public static String getAgencyRouteDirectionStopTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag, @Nullable Integer directionTag, @Nullable String stopTag) {
if (directionTag == null) return null;
if (stopTag == null) return null;
return POI.POIUtils.getUUID(agencyTag, "ri" + routeTag, "d" + directionTag, "si" + stopTag);
return POI.POIUtils.makeUUID(agencyTag, "ri" + routeTag, "d" + directionTag, "si" + stopTag);
}

@NonNull
public static String getAgencyTagTargetUUID(@NonNull String agencyTag) {
return POI.POIUtils.getUUID(agencyTag);
return POI.POIUtils.makeUUID(agencyTag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private static String getAgencyRouteStopTargetUUID(@NonNull RouteDirectionStop r
}

private static String getAgencyRouteStopTargetUUID(String agencyAuthority, String routeShortName, boolean noPickup, String stopCode) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName, noPickup ? 1 : 0, stopCode);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName, noPickup ? 1 : 0, stopCode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,17 +789,17 @@ private String cleanStopTag(@NonNull String stopTag) {

@NonNull
public static String getAgencyRouteStopTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag, @NonNull String stopTag) {
return POI.POIUtils.getUUID(agencyTag, routeTag, stopTag);
return POI.POIUtils.makeUUID(agencyTag, routeTag, stopTag);
}

@NonNull
public static String getAgencyRouteTagTargetUUID(@NonNull String agencyTag, @NonNull String routeTag) {
return POI.POIUtils.getUUID(agencyTag, routeTag);
return POI.POIUtils.makeUUID(agencyTag, routeTag);
}

@NonNull
public static String getAgencyTargetUUID(@NonNull String agencyTag) {
return POI.POIUtils.getUUID(agencyTag);
return POI.POIUtils.makeUUID(agencyTag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,12 @@ private Map<String, String> getTargetUUIDs(@NonNull Route route) {

@NonNull
protected static String getAgencyRouteShortNameTargetUUID(@NonNull String agencyAuthority, @NonNull String routeShortName) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName);
}

@NonNull
protected static String getAgencyTargetUUID(@NonNull String agencyAuthority) {
return POI.POIUtils.getUUID(agencyAuthority);
return POI.POIUtils.makeUUID(agencyAuthority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,13 @@ private String getUUID(Long pubDateInMs) {
private static final ThreadSafeDateFormatter ATOM_UPDATED_FORMATTER = new ThreadSafeDateFormatter(DATE_TIME_FORMAT, Locale.ENGLISH);
private static final ThreadSafeDateFormatter DC_DATE_FORMATTER = new ThreadSafeDateFormatter("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", Locale.ENGLISH);

private static final boolean DATE_PARSING_DEBUG = false;
Comment thread
mmathieum marked this conversation as resolved.

private void logDateParsing(@NonNull String msg, @NonNull Object... args) {
if (!DATE_PARSING_DEBUG) return;
MTLog.d(this, msg, args);
}

private long getPublicationDateInMs() {
final String currentPubDate = this.currentPubDateSb.toString().trim();
final String currentDate = this.currentDateSb.toString().trim();
Expand All @@ -1072,7 +1079,7 @@ private long getPublicationDateInMs() {
}
}
} catch (Exception e) {
MTLog.d(this, "Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER_X.toPattern());
logDateParsing("Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER_X.toPattern());
}
try {
if (!currentPubDate.isEmpty()) {
Expand All @@ -1082,7 +1089,7 @@ private long getPublicationDateInMs() {
}
}
} catch (Exception e) {
MTLog.d(this, "Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER.toPattern());
logDateParsing("Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER.toPattern());
}
// UPDATED
try {
Expand All @@ -1093,7 +1100,7 @@ private long getPublicationDateInMs() {
}
}
} catch (Exception e) {
MTLog.d(this, "Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, ATOM_UPDATED_FORMATTER.toPattern());
logDateParsing("Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, ATOM_UPDATED_FORMATTER.toPattern());
}
// DATE
try {
Expand All @@ -1104,7 +1111,7 @@ private long getPublicationDateInMs() {
}
}
} catch (Exception e) {
MTLog.d(this, "Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, DC_DATE_FORMATTER.toPattern());
logDateParsing("Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, DC_DATE_FORMATTER.toPattern());
}
// LINK
try {
Expand All @@ -1119,7 +1126,7 @@ private long getPublicationDateInMs() {
}
}
} catch (Exception e) {
MTLog.d(this, "Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER_X.toPattern());
logDateParsing("Error while parsing pub date '%s' with '%s'!", this.currentPubDateSb, RSS_PUB_DATE_FORMATTER_X.toPattern());
}
MTLog.w(this, "Created fake date for news item!");
return getLastItemPublicationDateInMs() - TimeUnit.HOURS.toMillis(6L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private static String getAgencyRouteStopTargetUUID(@NonNull RouteDirectionStop r

@NonNull
private static String getAgencyRouteStopTargetUUID(String agencyAuthority, String routeShortName, long directionId, String stopCode) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName, directionId, stopCode);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName, directionId, stopCode);
}

@NonNull
Expand All @@ -511,7 +511,7 @@ private Map<String, String> getServiceUpdateTargetUUID(@NonNull Route route) {

@NonNull
private static String getAgencyRouteShortNameTargetUUID(@NonNull String agencyAuthority, @NonNull String routeShortName) {
return POI.POIUtils.getUUID(agencyAuthority, routeShortName);
return POI.POIUtils.makeUUID(agencyAuthority, routeShortName);
}

@Override
Expand Down
Loading