ComposeActivity: finalize preview feature, fix preview bar appearing on media change, add proper icon, fix minor bugs

This commit is contained in:
Alibek Omarov 2020-06-25 16:40:00 +03:00
parent 467bfefde6
commit b3259fcfd9
6 changed files with 59 additions and 30 deletions

View File

@ -54,6 +54,7 @@ import androidx.core.view.inputmethod.InputContentInfoCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.lifecycle.Lifecycle
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
@ -97,6 +98,8 @@ import kotlin.collections.ArrayList
import kotlin.math.max
import kotlin.math.min
import me.thanel.markdownedit.MarkdownEdit
import io.reactivex.android.schedulers.AndroidSchedulers
import com.uber.autodispose.android.lifecycle.autoDispose
class ComposeActivity : BaseActivity(),
ComposeOptionsListener,
@ -197,12 +200,14 @@ class ComposeActivity : BaseActivity(),
viewModel.setupComplete.value = true
stickerKeyboard.isSticky = true
eventHub.events.subscribe { event ->
when(event) {
is StatusPreviewEvent -> onStatusPreviewReady(event.status)
}
}
eventHub.events.observeOn(AndroidSchedulers.mainThread())
.autoDispose(this, Lifecycle.Event.ON_DESTROY)
.subscribe { event: Event? ->
when(event) {
is StatusPreviewEvent -> onStatusPreviewReady(event.status)
}
}
}
private fun uriToFilename(uri: Uri): String {
@ -967,9 +972,8 @@ class ComposeActivity : BaseActivity(),
}
private fun onSendClicked(preview: Boolean) {
if(preview && previewBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
if(preview && previewBehavior.state != BottomSheetBehavior.STATE_HIDDEN) {
previewBehavior.state = BottomSheetBehavior.STATE_HIDDEN
return
}
if (verifyScheduledTime()) {
@ -988,8 +992,6 @@ class ComposeActivity : BaseActivity(),
emojiBehavior.state = BottomSheetBehavior.STATE_HIDDEN
scheduleBehavior.state = BottomSheetBehavior.STATE_HIDDEN
stickerBehavior.state = BottomSheetBehavior.STATE_HIDDEN
// Log.d("ComposeActivityPreview", "Preview: " + status.content)
}
/** This is for the fancy keyboards which can insert images and stuff. */
@ -1030,11 +1032,11 @@ class ComposeActivity : BaseActivity(),
this, getString(R.string.dialog_title_finishing_media_upload),
getString(R.string.dialog_message_uploading_media), true, true)
viewModel.sendStatus(contentText, spoilerText, preview).observe(this, Observer {
viewModel.sendStatus(contentText, spoilerText, preview).observeOnce(this) {
finishingUploadDialog?.dismiss()
if(!preview)
deleteDraftAndFinish()
})
}
} else {
composeEditField.error = getString(R.string.error_compose_character_limit)

View File

@ -11,6 +11,7 @@ import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.Parcelable
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
@ -130,8 +131,8 @@ class SendTootService : Service(), Injectable {
tootToSend.retries++
val contentType : String? = if(tootToSend.formattingSyntax.length == 0) null else tootToSend.formattingSyntax
val preview : Boolean? = if(tootToSend.preview) tootToSend.preview else null
val contentType : String? = if(tootToSend.formattingSyntax.isNotEmpty()) tootToSend.formattingSyntax else null
val preview : Boolean? = if(tootToSend.preview) true else null
val newStatus = NewStatus(
tootToSend.text,
@ -167,14 +168,11 @@ class SendTootService : Service(), Injectable {
saveTootHelper.deleteDraft(tootToSend.savedTootUid)
}
if (tootToSend.preview) {
response.body()?.let(::StatusPreviewEvent)?.let(eventHub::dispatch)
} else if (scheduled) {
response.body()?.let(::StatusScheduledEvent)?.let(eventHub::dispatch)
} else {
response.body()?.let(::StatusComposedEvent)?.let(eventHub::dispatch)
when {
tootToSend.preview -> response.body()?.let(::StatusPreviewEvent)?.let(eventHub::dispatch)
scheduled -> response.body()?.let(::StatusScheduledEvent)?.let(eventHub::dispatch)
else -> response.body()?.let(::StatusComposedEvent)?.let(eventHub::dispatch)
}
notificationManager.cancel(tootId)
} else {

View File

@ -27,6 +27,24 @@ inline fun <X, Y> LiveData<X>.switchMap(
crossinline switchMapFunction: (X) -> LiveData<Y>
): LiveData<Y> = Transformations.switchMap(this) { input -> switchMapFunction(input) }
fun <T> LiveData<T>.observeOnce(observer: (T) -> Unit) {
observeForever(object: Observer<T> {
override fun onChanged(value: T) {
removeObserver(this)
observer(value)
}
})
}
fun <T> LiveData<T>.observeOnce(owner: LifecycleOwner, observer: (T) -> Unit) {
observe(owner, object: Observer<T> {
override fun onChanged(value: T) {
removeObserver(this)
observer(value)
}
})
}
inline fun <X> LiveData<X>.filter(crossinline predicate: (X) -> Boolean): LiveData<X> {
val liveData = MediatorLiveData<X>()
liveData.addSource(this) { value ->

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M19,3H5C3.89,3 3,3.9 3,5v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5C21,3.9 20.11,3 19,3zM19,19H5V7h14V19zM13.5,13c0,0.83 -0.67,1.5 -1.5,1.5s-1.5,-0.67 -1.5,-1.5c0,-0.83 0.67,-1.5 1.5,-1.5S13.5,12.17 13.5,13zM12,9c-2.73,0 -5.06,1.66 -6,4c0.94,2.34 3.27,4 6,4s5.06,-1.66 6,-4C17.06,10.66 14.73,9 12,9zM12,15.5c-1.38,0 -2.5,-1.12 -2.5,-2.5c0,-1.38 1.12,-2.5 2.5,-2.5c1.38,0 2.5,1.12 2.5,2.5C14.5,14.38 13.38,15.5 12,15.5z"
android:fillColor="#000000"/>
</vector>

View File

@ -262,7 +262,7 @@
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="60dp"
android:paddingBottom="@dimen/compose_activity_bottom_bar_height"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
@ -276,7 +276,7 @@
android:paddingStart="24dp"
android:paddingTop="12dp"
android:paddingEnd="24dp"
android:paddingBottom="60dp"
android:paddingBottom="@dimen/compose_activity_bottom_bar_height"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
@ -290,7 +290,7 @@
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="52dp"
android:paddingBottom="@dimen/compose_activity_bottom_bar_height"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
@ -301,19 +301,20 @@
android:layout_height="300dp"
android:background="?attr/colorSurface"
android:elevation="12dp"
android:paddingBottom="60dp"
android:paddingBottom="@dimen/compose_activity_bottom_bar_height"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />
<ScrollView
<androidx.core.widget.NestedScrollView
android:id="@+id/previewScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="300dp"
android:background="?attr/colorSurface"
android:elevation="12dp"
android:paddingBottom="60dp"
android:paddingBottom="@dimen/compose_activity_bottom_bar_height"
app:behavior_hideable="true"
app:behavior_skipCollapsed="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
>
@ -323,7 +324,7 @@
android:layout_height="wrap_content"
android:background="?attr/colorSurface"
/>
</ScrollView>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:layout_width="match_parent"
@ -459,7 +460,7 @@
android:layout_width="32dp"
android:layout_height="wrap_content"
android:gravity="center"
app:icon="@drawable/ic_eye_24dp"
app:icon="@drawable/ic_preview_24dp"
android:layout_marginStart="10dp"
android:visibility="gone"
android:layout_toLeftOf="@+id/composeTootButton"

View File

@ -17,6 +17,7 @@
<dimen name="card_image_horizontal_width">100dp</dimen>
<dimen name="compose_activity_snackbar_elevation">16dp</dimen>
<dimen name="compose_activity_bottom_bar_height">60dp</dimen>
<dimen name="account_activity_scroll_title_visible_height">200dp</dimen>
<dimen name="account_activity_avatar_size">100dp</dimen>