Skip to content

Commit

Permalink
Block until instance info is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
captainepoch committed Dec 8, 2024
1 parent eaf9d8a commit bc19c9b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ import io.reactivex.disposables.Disposable
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.last
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import retrofit2.Response

open class CommonComposeViewModel(
Expand Down Expand Up @@ -92,16 +95,30 @@ open class CommonComposeViewModel(
private fun getInstanceConfig() {
job?.cancelIfActive()
job = viewModelScope.launch(Dispatchers.IO) {
instanceRepository.getInstanceInfo().collectLatest { instanceInfo ->
emoji.postValue(instanceInfo.asRight().emojiList)
instance.postValue(instanceInfo.asRight())
}
val instanceInfo = getInstanceInfo()
emoji.postValue(instanceInfo.emojiList)
instance.postValue(instanceInfo)
}
}

suspend fun getInstanceInfo(): InstanceEntity {
return instanceRepository.getInstanceInfo().last().asRight()
}

fun pickMedia(uri: Uri, filename: String?): LiveData<Either<Throwable, QueuedMedia>> {
// We are not calling .toLiveData() here because we don't want to stop the process when
// the Activity goes away temporarily (like on screen rotation).

// Needed to get the instance config before uploading anything
// TODO: nasty hack, improve
if (instance.value == null) {
instance.value = runBlocking {
async {
getInstanceInfo()
}.await()
}
}

val liveData = MutableLiveData<Either<Throwable, QueuedMedia>>()
val imageLimit = instance.value?.imageSizeLimit ?: InstanceConstants.DEFAULT_STATUS_MEDIA_SIZE
val videoLimit = instance.value?.videoSizeLimit ?: InstanceConstants.DEFAULT_STATUS_MEDIA_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,52 +274,47 @@ class ComposeActivity :
}

private fun applyShareIntent(intent: Intent, savedInstanceState: Bundle?) {
if (savedInstanceState == null) {
// Hack: Instance values are needed before sharing content
// TODO: Better way to make this flow
viewModel.instanceParams.observe(this) {
/* Get incoming images being sent through a share action from another app. Only do this
if (savedInstanceState == null) {/* Get incoming images being sent through a share action from another app. Only do this
* when savedInstanceState is null, otherwise both the images from the intent and the
* instance state will be re-queued.
*/
intent.type?.also { type ->
if (type.startsWith("image/") || type.startsWith("video/") || type.startsWith("audio/")) {
when (intent.action) {
Intent.ACTION_SEND -> {
intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.let { uri ->
pickMedia(uri)
}
}

Intent.ACTION_SEND_MULTIPLE -> {
intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
?.forEach { uri ->
pickMedia(uri)
}
intent.type?.also { type ->
if (type.startsWith("image/") || type.startsWith("video/") || type.startsWith("audio/")) {
when (intent.action) {
Intent.ACTION_SEND -> {
intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.let { uri ->
pickMedia(uri)
}
}
} else if (type == "text/plain" && intent.action == Intent.ACTION_SEND) {
val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT)
val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty()
val shareBody = if (!subject.isNullOrBlank() && subject !in text) {
subject + '\n' + text
} else {
text
}

if (shareBody.isNotBlank()) {
val start = binding.composeEditField.selectionStart.coerceAtLeast(0)
val end = binding.composeEditField.selectionEnd.coerceAtLeast(0)
val left = min(start, end)
val right = max(start, end)
binding.composeEditField.text.replace(
left, right, shareBody, 0, shareBody.length
)
// move edittext cursor to first when shareBody parsed
binding.composeEditField.text.insert(0, "\n")
binding.composeEditField.setSelection(0)
Intent.ACTION_SEND_MULTIPLE -> {
intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
?.forEach { uri ->
pickMedia(uri)
}
}
}
} else if (type == "text/plain" && intent.action == Intent.ACTION_SEND) {
val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT)
val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty()
val shareBody = if (!subject.isNullOrBlank() && subject !in text) {
subject + '\n' + text
} else {
text
}

if (shareBody.isNotBlank()) {
val start = binding.composeEditField.selectionStart.coerceAtLeast(0)
val end = binding.composeEditField.selectionEnd.coerceAtLeast(0)
val left = min(start, end)
val right = max(start, end)
binding.composeEditField.text.replace(
left, right, shareBody, 0, shareBody.length
)
// move edittext cursor to first when shareBody parsed
binding.composeEditField.text.insert(0, "\n")
binding.composeEditField.setSelection(0)
}
}
}
}
Expand Down

0 comments on commit bc19c9b

Please sign in to comment.