-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from terrakok/dev
Huge refactoring of the authorization flow to make the app as single activity application.
- Loading branch information
Showing
55 changed files
with
1,826 additions
and
1,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
app/src/androidMain/kotlin/com/daniebeler/pfpixelix/AppActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.daniebeler.pfpixelix | ||
|
||
import android.content.ContentResolver | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.WindowManager | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.window.DialogWindowProvider | ||
import co.touchlab.kermit.Logger | ||
import com.daniebeler.pfpixelix.utils.LocalKmpContext | ||
import com.daniebeler.pfpixelix.utils.Navigate | ||
import kotlinx.serialization.json.Json | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.InputStream | ||
|
||
class AppActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
enableEdgeToEdge() | ||
setContent { | ||
CompositionLocalProvider( | ||
LocalKmpContext provides this | ||
) { | ||
App(MyApplication.appComponent) { finish() } | ||
} | ||
} | ||
if (savedInstanceState == null) { | ||
handleNewIntent(intent) | ||
} | ||
} | ||
|
||
override fun onNewIntent(intent: Intent) { | ||
super.onNewIntent(intent) | ||
handleNewIntent(intent) | ||
} | ||
|
||
private fun handleNewIntent(intent: Intent) { | ||
when (intent.action) { | ||
Intent.ACTION_VIEW -> { | ||
intent.dataString?.let { onExternalUrl(it) } | ||
} | ||
Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE -> { | ||
val imageUris = handleSharePhotoIntent(intent, contentResolver, cacheDir) | ||
if (imageUris.isNotEmpty()) { | ||
imageUris.forEach { uri -> | ||
try { | ||
contentResolver.takePersistableUriPermission( | ||
uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
) | ||
} catch (e: SecurityException) { | ||
e.printStackTrace() // Handle permission denial gracefully | ||
} | ||
} | ||
onExternalFileShare(imageUris) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun onExternalUrl(url: String) { | ||
val systemUrlHandler = MyApplication.appComponent.systemUrlHandler | ||
systemUrlHandler.onRedirect(url) | ||
} | ||
|
||
private fun onExternalFileShare(uris: List<Uri>) { | ||
val systemFileShare = MyApplication.appComponent.systemFileShare | ||
systemFileShare.share(uris) | ||
} | ||
} | ||
|
||
@Composable | ||
actual fun SetUpEdgeToEdgeDialog() { | ||
val parentView = LocalView.current.parent as View | ||
val window = (parentView as DialogWindowProvider).window | ||
|
||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) | ||
|
||
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
window.attributes.fitInsetsTypes = 0 | ||
window.attributes.fitInsetsSides = 0 | ||
} | ||
} | ||
|
||
private fun saveUriToCache(uri: Uri, contentResolver: ContentResolver, cacheDir: File): Uri? { | ||
try { | ||
val inputStream: InputStream? = contentResolver.openInputStream(uri) | ||
inputStream?.use { input -> | ||
val file = File(cacheDir, "shared_image_${System.currentTimeMillis()}.jpg") | ||
FileOutputStream(file).use { output -> | ||
input.copyTo(output) | ||
} | ||
return Uri.fromFile(file) // Return the new cached URI | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
return null | ||
} | ||
|
||
private fun handleSharePhotoIntent( | ||
intent: Intent, contentResolver: ContentResolver, cacheDir: File | ||
): List<Uri> { | ||
val action = intent.action | ||
val type = intent.type | ||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
|
||
var imageUris: List<Uri> = emptyList() | ||
when { | ||
Intent.ACTION_SEND == action && type != null -> { | ||
if (type.startsWith("image/") || type.startsWith("video/")) { | ||
val singleUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
intent.getParcelableExtra( | ||
Intent.EXTRA_STREAM, Uri::class.java | ||
) | ||
} else { | ||
@Suppress("DEPRECATION") intent.getParcelableExtra( | ||
Intent.EXTRA_STREAM | ||
) as? Uri | ||
} | ||
singleUri?.let { uri -> | ||
val cachedUri = saveUriToCache(uri, contentResolver, cacheDir) | ||
imageUris = | ||
cachedUri?.let { listOf(it) } ?: emptyList() // Wrap single image in a list | ||
} | ||
} | ||
} | ||
|
||
Intent.ACTION_SEND_MULTIPLE == action && type != null -> { | ||
val receivedUris = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
intent.getParcelableArrayListExtra( | ||
Intent.EXTRA_STREAM, Uri::class.java | ||
) | ||
} else { | ||
@Suppress("DEPRECATION") intent.getParcelableArrayListExtra( | ||
Intent.EXTRA_STREAM | ||
) | ||
} | ||
imageUris = receivedUris?.mapNotNull { | ||
saveUriToCache( | ||
it, contentResolver, cacheDir | ||
) | ||
} ?: emptyList() | ||
} | ||
} | ||
return imageUris | ||
} |
Oops, something went wrong.