Skip to content

Commit

Permalink
[feature]支持多选图片从系统分享页面添加
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Oct 3, 2023
1 parent ca19415 commit f9ca2ef
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 69 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>

<receiver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.skyd.rays.model.bean

import android.net.Uri
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class UriWithStickerUuidBean(
val uri: Uri? = null,
val stickerUuid: String = "",
) : BaseBean, Parcelable {
fun isEmpty(): Boolean = uri == null
}

val EmptyUriWithStickerUuidBean = UriWithStickerUuidBean()
63 changes: 45 additions & 18 deletions app/src/main/java/com/skyd/rays/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.skyd.rays.ext.navigate
import com.skyd.rays.model.bean.UriWithStickerUuidBean
import com.skyd.rays.model.preference.SettingsProvider
import com.skyd.rays.ui.local.LocalDarkMode
import com.skyd.rays.ui.local.LocalNavController
Expand All @@ -43,6 +43,7 @@ import com.skyd.rays.ui.screen.about.license.LicenseScreen
import com.skyd.rays.ui.screen.about.update.UpdateDialog
import com.skyd.rays.ui.screen.add.ADD_SCREEN_ROUTE
import com.skyd.rays.ui.screen.add.AddScreen
import com.skyd.rays.ui.screen.add.openAddScreen
import com.skyd.rays.ui.screen.minitool.MINI_TOOL_SCREEN_ROUTE
import com.skyd.rays.ui.screen.minitool.MiniToolScreen
import com.skyd.rays.ui.screen.minitool.styletransfer.STYLE_TRANSFER_SCREEN_ROUTE
Expand Down Expand Up @@ -154,12 +155,13 @@ class MainActivity : AppCompatActivity() {
MainScreen()
}
composable(
route = "$ADD_SCREEN_ROUTE?stickerUuid={stickerUuid}",
arguments = listOf(navArgument("stickerUuid") { defaultValue = "" })
route = "$ADD_SCREEN_ROUTE?isEdit={isEdit}",
arguments = listOf(navArgument("isEdit") { defaultValue = false })
) {
AddScreen(
initStickerUuid = it.arguments?.getString("stickerUuid").orEmpty(),
sticker = it.arguments?.getParcelable("sticker")
initStickers = it.arguments?.getParcelableArrayList("stickers")
?: mutableListOf(),
isEdit = it.arguments?.getBoolean("isEdit") ?: false,
)
}
composable(route = SETTINGS_SCREEN_ROUTE) {
Expand Down Expand Up @@ -231,21 +233,46 @@ class MainActivity : AppCompatActivity() {
}

private fun initIntent() {
val sticker: Uri = if (Intent.ACTION_SEND == intent.action &&
intent.type.orEmpty().startsWith("image/")
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
} else {
intent.getParcelableExtra(Intent.EXTRA_STREAM)
val stickers: MutableList<UriWithStickerUuidBean> = when (intent?.action) {
Intent.ACTION_SEND -> {
val data = mutableListOf<UriWithStickerUuidBean>()
if (intent.type?.startsWith("image/") == true) {
val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
} else {
intent.getParcelableExtra(Intent.EXTRA_STREAM)
}
if (uri != null) {
data.add(UriWithStickerUuidBean(uri = uri))
}
data
} else data
}

Intent.ACTION_SEND_MULTIPLE -> {
val data = mutableListOf<UriWithStickerUuidBean>()
if (intent.type?.startsWith("image/") == true) {
val uris = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java)
} else {
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
}?.map { UriWithStickerUuidBean(uri = it) }
if (uris != null) {
data.addAll(uris)
}
data
} else data
}
} else {
null
} ?: return

navController.navigate(
ADD_SCREEN_ROUTE,
Bundle().apply { putParcelable("sticker", sticker) }
else -> mutableListOf()
}

if (stickers.isEmpty()) return

openAddScreen(
navController = navController,
stickers = stickers,
isEdit = false,
)
}
}
Loading

0 comments on commit f9ca2ef

Please sign in to comment.