Skip to content

Commit

Permalink
[1.60.*] Pre-release merge (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
tramline-github[bot] authored Feb 6, 2024
2 parents b5ca80a + 46b8b10 commit 4fc8533
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.sasikanth.rss.reader.core.network.parser

import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoField
Expand Down Expand Up @@ -55,61 +57,83 @@ internal actual fun String?.dateStringToEpochMillis(clock: Clock): Long? {

for (dateFormatter in dateFormatters) {
try {
val parsedValue = dateFormatter.parse(this)
val parsedValue = parseToInstant(dateFormatter, this)
return parsedValue.toEpochMilli()
} catch (e: Exception) {
try {
val parsedValue = fallbackParseToInstant(currentDate, dateFormatter, this)
return parsedValue.toEpochMilli()
} catch (e: Exception) {
// no-op
}
}
}

val year =
try {
parsedValue.get(ChronoField.YEAR)
} catch (e: UnsupportedTemporalTypeException) {
currentDate.get(ChronoField.YEAR)
}
val hourOfDay =
try {
parsedValue.get(ChronoField.HOUR_OF_DAY)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val minuteOfHour =
try {
parsedValue.get(ChronoField.MINUTE_OF_HOUR)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val secondOfMinute =
try {
parsedValue.get(ChronoField.SECOND_OF_MINUTE)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val nanoOfSecond =
try {
parsedValue.get(ChronoField.NANO_OF_SECOND)
} catch (e: UnsupportedTemporalTypeException) {
0
}
return null
}

val updatedDate =
currentDate
.withDayOfMonth(parsedValue.get(ChronoField.DAY_OF_MONTH))
.withMonth(parsedValue.get(ChronoField.MONTH_OF_YEAR))
.withYear(year)
.withHour(hourOfDay)
.withMinute(minuteOfHour)
.withSecond(secondOfMinute)
.withNano(nanoOfSecond)
private fun parseToInstant(dateTimeFormatter: DateTimeFormatter, text: String): Instant {
return dateTimeFormatter.parse(text, Instant::from)
}

/**
* In case the date string has values missing from it like year, day, hour, etc., we fill those
* using [currentDate] and then try to convert it to instant.
*/
private fun fallbackParseToInstant(
currentDate: LocalDateTime,
dateTimeFormatter: DateTimeFormatter,
text: String
): Instant {
val parsedValue = dateTimeFormatter.parse(text)

val year =
try {
parsedValue.get(ChronoField.YEAR)
} catch (e: UnsupportedTemporalTypeException) {
currentDate.get(ChronoField.YEAR)
}
val hourOfDay =
try {
parsedValue.get(ChronoField.HOUR_OF_DAY)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val minuteOfHour =
try {
parsedValue.get(ChronoField.MINUTE_OF_HOUR)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val secondOfMinute =
try {
parsedValue.get(ChronoField.SECOND_OF_MINUTE)
} catch (e: UnsupportedTemporalTypeException) {
0
}
val nanoOfSecond =
try {
parsedValue.get(ChronoField.NANO_OF_SECOND)
} catch (e: UnsupportedTemporalTypeException) {
0
}

val zoneId =
try {
ZoneId.from(parsedValue)
} catch (e: Exception) {
TimeZone.UTC.toJavaZoneId()
}
val updatedDate =
currentDate
.withDayOfMonth(parsedValue.get(ChronoField.DAY_OF_MONTH))
.withMonth(parsedValue.get(ChronoField.MONTH_OF_YEAR))
.withYear(year)
.withHour(hourOfDay)
.withMinute(minuteOfHour)
.withSecond(secondOfMinute)
.withNano(nanoOfSecond)

return updatedDate.atZone(zoneId).toInstant().toEpochMilli()
val zoneId =
try {
ZoneId.from(parsedValue)
} catch (e: Exception) {
// no-op
TimeZone.UTC.toJavaZoneId()
}
}

return null
return updatedDate.atZone(zoneId).toInstant()
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ class FeedsPresenter(
} else {
feedsPager(postsAfter = postsAfter)
}
val feedsWithHeaders = addFeedsHeaders(feeds).cachedIn(coroutineScope)

_state.update { it.copy(feedsInExpandedMode = addFeedsHeaders(feeds)) }
_state.update { it.copy(feedsInExpandedMode = feedsWithHeaders) }
}
.launchIn(coroutineScope)
}
Expand All @@ -234,7 +235,7 @@ class FeedsPresenter(
settingsRepository.postsType.distinctUntilChanged().flatMapLatest { postsType ->
val postsAfter = postsAfterInstantFromPostsType(postsType)

feedsPager(postsAfter)
feedsPager(postsAfter).cachedIn(coroutineScope)
}

observableSelectedFeed.selectedFeed
Expand All @@ -250,14 +251,12 @@ class FeedsPresenter(
rssRepository.searchFeed(searchQuery = transformedSearchQuery, postsAfter = postsAfter)
}
.flow
.cachedIn(coroutineScope)

private fun feedsPager(postsAfter: Instant) =
createPager(config = createPagingConfig(pageSize = 20)) {
rssRepository.allFeeds(postsAfter = postsAfter)
}
.flow
.cachedIn(coroutineScope)

private fun observeShowUnreadCountPreference() {
settingsRepository.showUnreadPostsCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ private object ReaderCSS {
iframe {
max-width: 100%;
max-height: 250px;
border-radius: 28px;
border: 1px solid #fff;
}
blockquote {
margin-left: 8px;
Expand Down

0 comments on commit 4fc8533

Please sign in to comment.