diff --git a/app/src/main/java/com/example/util/simpletimetracker/di/NavigationModule.kt b/app/src/main/java/com/example/util/simpletimetracker/di/NavigationModule.kt index 2ae1aad1..ce4a94e6 100644 --- a/app/src/main/java/com/example/util/simpletimetracker/di/NavigationModule.kt +++ b/app/src/main/java/com/example/util/simpletimetracker/di/NavigationModule.kt @@ -10,43 +10,33 @@ import com.example.util.simpletimetracker.navigation.ScreenFactory import com.example.util.simpletimetracker.navigation.ScreenFactoryImpl import com.example.util.simpletimetracker.navigation.ScreenResolver import com.example.util.simpletimetracker.navigation.ScreenResolverImpl +import dagger.Binds import dagger.Module -import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) -class NavigationModule { +interface NavigationModule { - @Provides + @Binds @Singleton - fun getScreenResolver(screenResolverImpl: ScreenResolverImpl): ScreenResolver { - return screenResolverImpl - } + fun getScreenResolver(screenResolverImpl: ScreenResolverImpl): ScreenResolver - @Provides + @Binds @Singleton - fun getScreenFactory(screenFactoryImpl: ScreenFactoryImpl): ScreenFactory { - return screenFactoryImpl - } + fun getScreenFactory(screenFactoryImpl: ScreenFactoryImpl): ScreenFactory - @Provides + @Binds @Singleton - fun getActionResolver(actionResolverImpl: ActionResolverImpl): ActionResolver { - return actionResolverImpl - } + fun getActionResolver(actionResolverImpl: ActionResolverImpl): ActionResolver - @Provides + @Binds @Singleton - fun getNotificationResolver(notificationResolverImpl: NotificationResolverImpl): NotificationResolver { - return notificationResolverImpl - } + fun getNotificationResolver(notificationResolverImpl: NotificationResolverImpl): NotificationResolver - @Provides + @Binds @Singleton - fun getRouter(routerImpl: RouterImpl): Router { - return routerImpl - } + fun getRouter(routerImpl: RouterImpl): Router } \ No newline at end of file diff --git a/app/src/main/java/com/example/util/simpletimetracker/navigation/ActionResolverImpl.kt b/app/src/main/java/com/example/util/simpletimetracker/navigation/ActionResolverImpl.kt index 5b8c7d93..779640e5 100644 --- a/app/src/main/java/com/example/util/simpletimetracker/navigation/ActionResolverImpl.kt +++ b/app/src/main/java/com/example/util/simpletimetracker/navigation/ActionResolverImpl.kt @@ -45,6 +45,7 @@ class ActionResolverImpl @Inject constructor( val uri = Uri.parse(MARKET_INTENT + params.packageName) val intent = Intent(Intent.ACTION_VIEW, uri) intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) + try { activity?.startActivity(intent) } catch (e: ActivityNotFoundException) { @@ -76,14 +77,13 @@ class ActionResolverImpl @Inject constructor( } private fun createCsvFile(activity: Activity?, data: Any?) { - val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) - .format(Date()) + val timeString = getFileNameTimeStamp() val fileName = "stt_records_$timeString.csv" val intent = Intent(Intent.ACTION_CREATE_DOCUMENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "text/csv" - intent.putExtra(Intent.EXTRA_TITLE, fileName) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("text/csv") + .putExtra(Intent.EXTRA_TITLE, fileName) if (activity?.packageManager?.let(intent::resolveActivity) != null) { createCsvFileResultLauncher?.launch(intent) @@ -94,10 +94,10 @@ class ActionResolverImpl @Inject constructor( private fun openFile(activity: Activity?, data: Any?) { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "application/*" + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("application/*") - if (activity?.packageManager?.let(intent::resolveActivity) != null) { + if (activity.checkIfIntentResolves(intent)) { openFileResultLauncher?.launch(intent) } else { (data as? FileChooserParams)?.notHandledCallback?.invoke() @@ -105,16 +105,15 @@ class ActionResolverImpl @Inject constructor( } private fun createFile(activity: Activity?, data: Any?) { - val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) - .format(Date()) + val timeString = getFileNameTimeStamp() val fileName = "stt_$timeString.backup" val intent = Intent(Intent.ACTION_CREATE_DOCUMENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "application/x-binary" - intent.putExtra(Intent.EXTRA_TITLE, fileName) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("application/x-binary") + .putExtra(Intent.EXTRA_TITLE, fileName) - if (activity?.packageManager?.let(intent::resolveActivity) != null) { + if (activity.checkIfIntentResolves(intent)) { createFileResultLauncher?.launch(intent) } else { (data as? FileChooserParams)?.notHandledCallback?.invoke() @@ -130,6 +129,15 @@ class ActionResolverImpl @Inject constructor( } } + private fun getFileNameTimeStamp(): String { + return SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) + } + + private fun Activity?.checkIfIntentResolves(intent: Intent): Boolean { + if (this == null) return false + return packageManager?.let(intent::resolveActivity) != null + } + companion object { private const val MARKET_INTENT = "market://details?id=" private const val MARKET_LINK = "http://play.google.com/store/apps/details?id=" diff --git a/app/src/main/java/com/example/util/simpletimetracker/navigation/NotificationResolverImpl.kt b/app/src/main/java/com/example/util/simpletimetracker/navigation/NotificationResolverImpl.kt index 3f992893..a5e45fe7 100644 --- a/app/src/main/java/com/example/util/simpletimetracker/navigation/NotificationResolverImpl.kt +++ b/app/src/main/java/com/example/util/simpletimetracker/navigation/NotificationResolverImpl.kt @@ -1,5 +1,6 @@ package com.example.util.simpletimetracker.navigation +import android.annotation.SuppressLint import android.app.Activity import android.graphics.Color import android.view.View @@ -25,13 +26,14 @@ class NotificationResolverImpl @Inject constructor() : NotificationResolver { Toast.makeText(activity?.applicationContext, data.message, Toast.LENGTH_LONG).show() } + @SuppressLint("WrongConstant") private fun showSnackBar(activity: Activity?, data: Any?, anchor: Any?) { if (data !is SnackBarParams || activity == null) return val snackBar = Snackbar.make( activity.findViewById(android.R.id.content), data.message, - 5000 + SNACK_BAR_DURATION_MS ) val textViewId = com.google.android.material.R.id.snackbar_text @@ -57,4 +59,8 @@ class NotificationResolverImpl @Inject constructor() : NotificationResolver { snackBar.show() } + + companion object { + private const val SNACK_BAR_DURATION_MS = 5000 + } } \ No newline at end of file