From b3d694b522dec57197097f88483555921414b7b5 Mon Sep 17 00:00:00 2001 From: razeeman Date: Sat, 25 Sep 2021 21:42:40 +0300 Subject: [PATCH] code review, refactor ActionResolverImpl --- .../navigation/ActionResolverImpl.kt | 100 +++++++++--------- 1 file changed, 51 insertions(+), 49 deletions(-) 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 fcc4c769..5b8c7d93 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 @@ -31,55 +31,11 @@ class ActionResolverImpl @Inject constructor( override fun execute(activity: Activity?, action: Action, data: Any?) { when (action) { - Action.OPEN_MARKET -> { - openMarket(activity, data) - } - Action.SEND_EMAIL -> { - sendEmail(activity, data) - } - Action.CREATE_FILE -> { - val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) - .format(Date()) - 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) - - if (activity?.packageManager?.let(intent::resolveActivity) != null) { - createFileResultLauncher?.launch(intent) - } else { - (data as? FileChooserParams)?.notHandledCallback?.invoke() - } - } - Action.OPEN_FILE -> { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "application/*" - - if (activity?.packageManager?.let(intent::resolveActivity) != null) { - openFileResultLauncher?.launch(intent) - } else { - (data as? FileChooserParams)?.notHandledCallback?.invoke() - } - } - Action.CREATE_CSV_FILE -> { - val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) - .format(Date()) - 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) - - if (activity?.packageManager?.let(intent::resolveActivity) != null) { - createCsvFileResultLauncher?.launch(intent) - } else { - (data as? FileChooserParams)?.notHandledCallback?.invoke() - } - } + Action.OPEN_MARKET -> openMarket(activity, data) + Action.SEND_EMAIL -> sendEmail(activity, data) + Action.CREATE_FILE -> createFile(activity, data) + Action.OPEN_FILE -> openFile(activity, data) + Action.CREATE_CSV_FILE -> createCsvFile(activity, data) } } @@ -119,6 +75,52 @@ class ActionResolverImpl @Inject constructor( } } + private fun createCsvFile(activity: Activity?, data: Any?) { + val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) + .format(Date()) + 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) + + if (activity?.packageManager?.let(intent::resolveActivity) != null) { + createCsvFileResultLauncher?.launch(intent) + } else { + (data as? FileChooserParams)?.notHandledCallback?.invoke() + } + } + + private fun openFile(activity: Activity?, data: Any?) { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = "application/*" + + if (activity?.packageManager?.let(intent::resolveActivity) != null) { + openFileResultLauncher?.launch(intent) + } else { + (data as? FileChooserParams)?.notHandledCallback?.invoke() + } + } + + private fun createFile(activity: Activity?, data: Any?) { + val timeString = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) + .format(Date()) + 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) + + if (activity?.packageManager?.let(intent::resolveActivity) != null) { + createFileResultLauncher?.launch(intent) + } else { + (data as? FileChooserParams)?.notHandledCallback?.invoke() + } + } + private fun ComponentActivity.registerForActivityResult(key: String): ActivityResultLauncher { return registerForActivityResult(StartActivityForResult()) { result -> val intent = result.data -- 2.54.0