Skip to content

Commit

Permalink
Fix issues with date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jul 29, 2023
1 parent baf3fc4 commit eabc839
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 25 deletions.
7 changes: 3 additions & 4 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ if (localProperties.exists()) {
localProperties.inputStream().use { local.load(it) }
}


android {
namespace = "com.prof18.feedflow"
compileSdk = libs.versions.android.compile.sdk.get().toInt()
Expand All @@ -34,12 +33,12 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.majorVersion
jvmTarget = JavaVersion.VERSION_17.majorVersion
}

signingConfigs {
Expand Down
22 changes: 21 additions & 1 deletion androidApp/src/main/kotlin/com/prof18/feedflow/BrowserManager.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.prof18.feedflow

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import co.touchlab.kermit.Logger
import com.prof18.feedflow.domain.feed.manager.FeedManagerRepository
import com.prof18.feedflow.domain.model.Browser
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -14,6 +16,7 @@ import kotlinx.coroutines.flow.update
class BrowserManager(
private val context: Context,
private val feedManagerRepository: FeedManagerRepository,
private val logger: Logger,
) {

private val browserListMutableState = MutableStateFlow<List<Browser>>(emptyList())
Expand Down Expand Up @@ -73,7 +76,7 @@ class BrowserManager(
browserListMutableState.update { browserList }
}

fun openUrl(
fun openUrlWithFavoriteBrowser(
url: String,
context: Context,
) {
Expand All @@ -83,6 +86,23 @@ class BrowserManager(
setPackage(packageName)
}
}
try {
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
logger.e(e) {
"Favourite browser not valid, open with the default one"
}
openUrlWithDefaultBrowser(url, context)
}
}

fun openUrlWithDefaultBrowser(
url: String,
context: Context,
) {
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(url)
}
context.startActivity(intent)
}
}
2 changes: 2 additions & 0 deletions androidApp/src/main/kotlin/com/prof18/feedflow/FeedFlowApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.prof18.feedflow

import android.app.Application
import android.content.Context
import com.prof18.feedflow.di.getWith
import com.prof18.feedflow.di.initKoin
import com.prof18.feedflow.utils.AppEnvironment
import com.prof18.feedflow.utils.enableKmpCrashlytics
Expand Down Expand Up @@ -31,6 +32,7 @@ class FeedFlowApp : Application() {
BrowserManager(
context = this@FeedFlowApp,
feedManagerRepository = get(),
logger = getWith("BrowserManager"),
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ internal fun HomeScreen(
homeViewModel.updateReadStatus(lastVisibleIndex)
},
onFeedItemClick = { feedInfo ->
browserManager.openUrl(feedInfo.url, context)
browserManager.openUrlWithFavoriteBrowser(feedInfo.url, context)
homeViewModel.markAsRead(feedInfo.id)
},
onFeedItemLongClick = { feedInfo ->
browserManager.openUrl(feedInfo.url, context)
browserManager.openUrlWithFavoriteBrowser(feedInfo.url, context)
homeViewModel.markAsRead(feedInfo.id)
},
onAddFeedClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fun SettingsScreen(
navigateBack = navigateBack,
onAboutClick = onAboutClick,
onBugReportClick = {
browserManager.openUrl(
browserManager.openUrlWithFavoriteBrowser(
url = UserFeedbackReporter.getFeedbackUrl(),
context = context,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ fun AboutScreen(
AboutScreenContent(
licensesClicked = navigateToLibrariesScreen,
nameClicked = {
browserManager.openUrl(
browserManager.openUrlWithDefaultBrowser(
url = MG_WEBSITE,
context = context,
)
},
onOpenWebsiteClick = {
browserManager.openUrl(
browserManager.openUrlWithDefaultBrowser(
url = FEED_FLOW_WEBSITE,
context = context,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,36 @@ import java.time.format.DateTimeFormatter
import java.util.Locale

internal actual fun getDateMillisFromString(dateString: String, logger: Logger?): Long? {
val dateFormat = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault())
return try {
var exception: Throwable? = null
var message: String? = null

var date = try {
val dateFormat = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault())
dateFormat.parse(dateString)?.time
} catch (e: ParseException) {
logger?.e(e) {
"Error while trying to format the date with dateFormatter. Date: $dateString"
}
exception = e
message = "Error while trying to format the date with dateFormatter. Date: $dateString"
null
}

if (date == null) {
date = try {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.getDefault())
dateFormat.parse(dateString)?.time
} catch (e: ParseException) {
exception = e
message = "Error while trying to format the date with dateFormatter. Date: $dateString"
null
}
}

if (date == null && exception != null && message != null) {
logger?.e(exception) {
message
}
}

return date
}

internal actual fun formatDate(millis: Long): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ internal expect inline fun <reified T : BaseViewModel> Module.viewModel(
noinline definition: Definition<T>,
): KoinDefinition<T>

internal inline fun <reified T> Scope.getWith(vararg params: Any?): T {
inline fun <reified T> Scope.getWith(vararg params: Any?): T {
return get(parameters = { parametersOf(*params) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@ class DateUtilsTest {
val millis = getDateMillisFromString(dateString)
assertNull(millis)
}

@Test
fun `getDateMillisFromString returns a value with a strange format`() {
val dateString = "2023-07-28T15:01:10+02:00"

val millis = getDateMillisFromString(dateString)
assertEquals(1690549270000, millis)
}

@Test
fun `getDateMillisFromString returns a value with different time zone`() {
val dateString = "Fri, 28 Jul 2023 12:37:25 +0300"

val millis = getDateMillisFromString(dateString)
assertEquals(1690537045000, millis)
}
}
31 changes: 22 additions & 9 deletions shared/src/iosMain/kotlin/com/prof18/feedflow/domain/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ import platform.Foundation.timeIntervalSince1970

@Suppress("TooGenericExceptionCaught")
internal actual fun getDateMillisFromString(dateString: String, logger: Logger?): Long? {
val dateFormatter = NSDateFormatter().apply {
setDateFormat("E, d MMM yyyy HH:mm:ss Z")
setLocale(NSLocale("en_US_POSIX"))
}
val timeZoneDateFormatter = NSDateFormatter().apply {
setDateFormat("E, d MMM yyyy HH:mm:ss zzz")
setLocale(NSLocale("en_US_POSIX"))
}

var exception: Throwable? = null
var message: String? = null

var date = try {
val dateFormatter = NSDateFormatter().apply {
setDateFormat("E, d MMM yyyy HH:mm:ss Z")
setLocale(NSLocale("en_US_POSIX"))
}
dateFormatter.dateFromString(dateString)
} catch (e: Throwable) {
exception = e
Expand All @@ -35,6 +30,24 @@ internal actual fun getDateMillisFromString(dateString: String, logger: Logger?)

if (date == null) {
date = try {
val timeZoneDateFormatter = NSDateFormatter().apply {
setDateFormat("E, d MMM yyyy HH:mm:ss zzz")
setLocale(NSLocale("en_US_POSIX"))
}
timeZoneDateFormatter.dateFromString(dateString)
} catch (e: Throwable) {
exception = e
message = "Error while trying to format the date with timeZoneDateFormatter. Date: $dateString"
null
}
}

if (date == null) {
date = try {
val timeZoneDateFormatter = NSDateFormatter().apply {
setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
setLocale(NSLocale("en_US_POSIX"))
}
timeZoneDateFormatter.dateFromString(dateString)
} catch (e: Throwable) {
exception = e
Expand Down

0 comments on commit eabc839

Please sign in to comment.