From e2bb38d7307ea55466842a75530ec96163ef5d6b Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 1 Oct 2024 20:39:27 -0600 Subject: [PATCH] Fix opening files from external apps I somehow missed this when migrating to compose and worse yet, didn't have any tests covering it. That's been remedied now so hopefully it continues to work well into the future Closes #90 --- .../wbrawner/simplemarkdown/MarkdownTests.kt | 23 +++++++++++++++++++ .../simplemarkdown/MarkdownViewModel.kt | 4 ---- .../wbrawner/simplemarkdown/ui/MainScreen.kt | 5 ++++ .../simplemarkdown/utility/Extensions.kt | 22 ++++++++++++------ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt b/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt index 6502c20..6bf015a 100644 --- a/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt +++ b/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt @@ -182,6 +182,29 @@ class MarkdownTests { } } + @Test + fun launchWithContentUriTest() = runTest { + val markdownText = "# UI Testing\n\nThe quick brown fox jumped over the lazy dog." + file.outputStream().writer().use { it.write(markdownText) } + val fileUri = FileProvider.getUriForFile( + getApplicationContext(), + "${BuildConfig.APPLICATION_ID}.fileprovider", + file + ) + ActivityScenario.launch( + Intent( + Intent.ACTION_VIEW, + fileUri, + getInstrumentation().targetContext, + MainActivity::class.java + ) + ) + onMainScreen(composeRule) { + awaitIdle() + checkMarkdownEquals(markdownText) + checkTitleEquals("temp.md") + } + } @Test fun openEditAndSaveMarkdownTest() = runTest { diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt index d3465b9..161de7b 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt @@ -14,7 +14,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext @@ -50,9 +49,6 @@ class MarkdownViewModel( private val saveMutex = Mutex() init { - viewModelScope.launch { - load(null) - } preferenceHelper.observe(Preference.LOCK_SWIPING) .onEach { _state.value = _state.value.copy(lockSwiping = it) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt b/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt index 5cef4d2..fd5a5e6 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt @@ -62,6 +62,7 @@ import com.wbrawner.simplemarkdown.MarkdownViewModel import com.wbrawner.simplemarkdown.ParameterizedText import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.Route +import com.wbrawner.simplemarkdown.utility.activity import kotlinx.coroutines.delay import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map @@ -87,6 +88,10 @@ fun MainScreen( val alert by viewModel.collectAsState(EditorState::alert, null) val saveCallback by viewModel.collectAsState(EditorState::saveCallback, null) val lockSwiping by viewModel.collectAsState(EditorState::lockSwiping, false) + val intentData = LocalContext.current.activity?.intent?.data + LaunchedEffect(intentData) { + viewModel.load(intentData?.toString()) + } LaunchedEffect(enableAutosave) { if (!enableAutosave) return@LaunchedEffect while (isActive) { diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/utility/Extensions.kt b/app/src/main/java/com/wbrawner/simplemarkdown/utility/Extensions.kt index 2b9ba0a..7be1b1b 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/utility/Extensions.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/utility/Extensions.kt @@ -1,11 +1,11 @@ package com.wbrawner.simplemarkdown.utility +import android.app.Activity import android.content.Context +import android.content.ContextWrapper import android.content.res.AssetManager import android.net.Uri import android.provider.OpenableColumns -import android.view.View -import android.view.inputmethod.InputMethodManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.Reader @@ -22,11 +22,11 @@ suspend fun Uri.getName(context: Context): String { if ("content" == scheme) { withContext(Dispatchers.IO) { context.contentResolver.query( - this@getName, - null, - null, - null, - null + this@getName, + null, + null, + null, + null )?.use { val nameIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) it.moveToFirst() @@ -41,3 +41,11 @@ suspend fun Uri.getName(context: Context): String { } return fileName ?: "Untitled.md" } + +@Suppress("RecursivePropertyAccessor") +val Context.activity: Activity? + get() = when (this) { + is Activity -> this + is ContextWrapper -> baseContext.activity + else -> null + } \ No newline at end of file