소스 검색

Added show toast extension function (refactoring)

Vadik Sirekanyan 2 년 전
부모
커밋
7c96a50bfa

+ 3 - 3
app/src/main/java/org/sirekanyan/outline/MainState.kt

@@ -1,6 +1,5 @@
 package org.sirekanyan.outline
 package org.sirekanyan.outline
 
 
-import android.widget.Toast
 import androidx.compose.material3.DrawerState
 import androidx.compose.material3.DrawerState
 import androidx.compose.material3.DrawerValue
 import androidx.compose.material3.DrawerValue
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.Composable
@@ -29,6 +28,7 @@ import org.sirekanyan.outline.db.rememberKeyDao
 import org.sirekanyan.outline.db.rememberKeyValueDao
 import org.sirekanyan.outline.db.rememberKeyValueDao
 import org.sirekanyan.outline.db.rememberServerDao
 import org.sirekanyan.outline.db.rememberServerDao
 import org.sirekanyan.outline.ext.logError
 import org.sirekanyan.outline.ext.logError
+import org.sirekanyan.outline.ext.showToast
 import org.sirekanyan.outline.feature.keys.KeysErrorState
 import org.sirekanyan.outline.feature.keys.KeysErrorState
 import org.sirekanyan.outline.feature.keys.KeysIdleState
 import org.sirekanyan.outline.feature.keys.KeysIdleState
 import org.sirekanyan.outline.feature.keys.KeysLoadingState
 import org.sirekanyan.outline.feature.keys.KeysLoadingState
@@ -50,10 +50,10 @@ fun rememberMainState(): MainState {
             }
             }
             when (throwable) {
             when (throwable) {
                 is UnknownHostException, is ConnectException -> {
                 is UnknownHostException, is ConnectException -> {
-                    Toast.makeText(context, "Check network connection", Toast.LENGTH_SHORT).show()
+                    context.showToast("Check network connection")
                 }
                 }
                 else -> {
                 else -> {
-                    Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show()
+                    context.showToast("Something went wrong")
                 }
                 }
             }
             }
         }
         }

+ 1 - 2
app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt

@@ -6,7 +6,6 @@ import android.content.Intent.ACTION_VIEW
 import android.content.pm.PackageManager.GET_ACTIVITIES
 import android.content.pm.PackageManager.GET_ACTIVITIES
 import android.content.pm.PackageManager.NameNotFoundException
 import android.content.pm.PackageManager.NameNotFoundException
 import android.net.Uri
 import android.net.Uri
-import android.widget.Toast
 
 
 private const val OUTLINE_PACKAGE = "org.outline.android.client"
 private const val OUTLINE_PACKAGE = "org.outline.android.client"
 private const val OUTLINE_PLAY_LINK = "https://play.google.com/store/apps/details?id=$OUTLINE_PACKAGE"
 private const val OUTLINE_PLAY_LINK = "https://play.google.com/store/apps/details?id=$OUTLINE_PACKAGE"
@@ -38,6 +37,6 @@ fun openGooglePlay(context: Context) {
         context.startActivity(Intent(ACTION_VIEW, Uri.parse(OUTLINE_PLAY_LINK)))
         context.startActivity(Intent(ACTION_VIEW, Uri.parse(OUTLINE_PLAY_LINK)))
     } catch (exception: Exception) {
     } catch (exception: Exception) {
         logDebug("Cannot open Google Play", exception)
         logDebug("Cannot open Google Play", exception)
-        Toast.makeText(context, "Cannot open Google Play", Toast.LENGTH_SHORT).show()
+        context.showToast("Cannot open Google Play")
     }
     }
 }
 }

+ 13 - 0
app/src/main/java/org/sirekanyan/outline/ext/Toast.kt

@@ -0,0 +1,13 @@
+package org.sirekanyan.outline.ext
+
+import android.content.Context
+import android.widget.Toast
+import androidx.annotation.StringRes
+
+fun Context.showToast(text: String) {
+    Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
+}
+
+fun Context.showToast(@StringRes text: Int) {
+    Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
+}

+ 2 - 3
app/src/main/java/org/sirekanyan/outline/ui/KeyBottomSheet.kt

@@ -1,8 +1,6 @@
 package org.sirekanyan.outline.ui
 package org.sirekanyan.outline.ui
 
 
 import android.content.Intent
 import android.content.Intent
-import android.widget.Toast
-import android.widget.Toast.LENGTH_SHORT
 import androidx.compose.foundation.clickable
 import androidx.compose.foundation.clickable
 import androidx.compose.material.icons.Icons
 import androidx.compose.material.icons.Icons
 import androidx.compose.material.icons.filled.Delete
 import androidx.compose.material.icons.filled.Delete
@@ -20,6 +18,7 @@ import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.text.AnnotatedString
 import androidx.compose.ui.text.AnnotatedString
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.launch
 import org.sirekanyan.outline.api.model.Key
 import org.sirekanyan.outline.api.model.Key
+import org.sirekanyan.outline.ext.showToast
 import org.sirekanyan.outline.ui.icons.IconCopy
 import org.sirekanyan.outline.ui.icons.IconCopy
 
 
 @Composable
 @Composable
@@ -42,7 +41,7 @@ fun KeyBottomSheet(
                 leadingContent = { Icon(IconCopy, null) },
                 leadingContent = { Icon(IconCopy, null) },
                 modifier = Modifier.clickable {
                 modifier = Modifier.clickable {
                     localClipboard.setText(AnnotatedString(key.accessKey.accessUrl))
                     localClipboard.setText(AnnotatedString(key.accessKey.accessUrl))
-                    Toast.makeText(localContext, "Copied", LENGTH_SHORT).show()
+                    localContext.showToast("Copied")
                     coroutineScope.launch {
                     coroutineScope.launch {
                         sheetState.hide()
                         sheetState.hide()
                     }.invokeOnCompletion {
                     }.invokeOnCompletion {