From e674f701550fb2c1318e6230f61ca50bd2ec4174 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 22 Jun 2021 16:23:47 +1000 Subject: [PATCH] Drafts --- .../conversation/v2/ConversationActivityV2.kt | 29 ++++++++++++++++++- .../conversation/v2/input_bar/InputBar.kt | 5 ++++ .../v2/input_bar/InputBarEditText.kt | 7 +++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index b8acd95630..26f6a0689c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -6,7 +6,6 @@ import android.content.res.Resources import android.database.Cursor import android.graphics.Rect import android.os.Bundle -import android.util.Log import android.view.ActionMode import android.view.Menu import android.view.MenuItem @@ -23,6 +22,7 @@ import kotlinx.android.synthetic.main.view_input_bar_recording.* import kotlinx.android.synthetic.main.view_input_bar_recording.view.* import network.loki.messenger.R import org.session.libsession.messaging.mentions.MentionsManager +import org.session.libsession.utilities.Util import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.conversation.v2.input_bar.InputBarButton import org.thoughtcrime.securesms.conversation.v2.input_bar.InputBarDelegate @@ -32,6 +32,8 @@ import org.thoughtcrime.securesms.conversation.v2.menus.ConversationActionModeCa import org.thoughtcrime.securesms.conversation.v2.menus.ConversationMenuHelper import org.thoughtcrime.securesms.conversation.v2.messages.VisibleMessageView import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.database.DraftDatabase +import org.thoughtcrime.securesms.database.DraftDatabase.Drafts import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.loki.utilities.toPx import org.thoughtcrime.securesms.mms.GlideApp @@ -95,6 +97,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe setUpRecyclerView() setUpToolBar() setUpInputBar() + restoreDraftIfNeeded() } private fun setUpRecyclerView() { @@ -144,11 +147,24 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe cameraButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT) } + private fun restoreDraftIfNeeded() { + val draftDB = DatabaseFactory.getDraftDatabase(this) + val drafts = draftDB.getDrafts(threadID) + draftDB.clearDrafts(threadID) + val text = drafts.find { it.type == DraftDatabase.Draft.TEXT }?.value ?: return + inputBar.text = text + } + override fun onPrepareOptionsMenu(menu: Menu): Boolean { ConversationMenuHelper.onPrepareOptionsMenu(menu, menuInflater, thread, this) { onOptionsItemSelected(it) } super.onPrepareOptionsMenu(menu) return true } + + override fun onDestroy() { + saveDraft() + super.onDestroy() + } // endregion // region Updating & Animation @@ -379,4 +395,15 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe return hitRect.contains(x, y) } // endregion + + // region General + private fun saveDraft() { + val text = inputBar.text.trim() + if (text.isEmpty()) { return } + val drafts = Drafts() + drafts.add(DraftDatabase.Draft(DraftDatabase.Draft.TEXT, text)) + val draftDB = DatabaseFactory.getDraftDatabase(this) + draftDB.insertDrafts(threadID, drafts) + } + // endregion } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt index dc47e4ff6a..915dbd4a98 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt @@ -1,6 +1,7 @@ package org.thoughtcrime.securesms.conversation.v2.input_bar import android.content.Context +import android.text.Editable import android.util.AttributeSet import android.util.Log import android.view.LayoutInflater @@ -24,6 +25,10 @@ class InputBar : RelativeLayout, InputBarEditTextDelegate, QuoteViewDelegate { var delegate: InputBarDelegate? = null var additionalContentHeight = 0 + var text: String + get() { return inputBarEditText.text.toString() } + set(value) { inputBarEditText.setText(value) } + private val attachmentsButton by lazy { InputBarButton(context, R.drawable.ic_plus_24) } private val microphoneButton by lazy { InputBarButton(context, R.drawable.ic_microphone) } private val sendButton by lazy { InputBarButton(context, R.drawable.ic_arrow_up, true) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarEditText.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarEditText.kt index 16c4df83cf..9fb480aeb9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarEditText.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarEditText.kt @@ -1,9 +1,11 @@ package org.thoughtcrime.securesms.conversation.v2.input_bar import android.content.Context +import android.content.res.Resources import android.text.Layout import android.text.StaticLayout import android.util.AttributeSet +import android.util.Log import android.widget.RelativeLayout import androidx.appcompat.widget.AppCompatEditText import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities @@ -13,6 +15,7 @@ import kotlin.math.min import kotlin.math.roundToInt class InputBarEditText : AppCompatEditText { + private val screenWidth get() = Resources.getSystem().displayMetrics.widthPixels var delegate: InputBarEditTextDelegate? = null private val snMinHeight = toPx(40.0f, resources) @@ -25,6 +28,10 @@ class InputBarEditText : AppCompatEditText { override fun onTextChanged(text: CharSequence, start: Int, lengthBefore: Int, lengthAfter: Int) { super.onTextChanged(text, start, lengthBefore, lengthAfter) delegate?.inputBarEditTextContentChanged(text) + // Calculate the width manually to get it right even before layout has happened (i.e. + // when restoring a draft) + val width = (screenWidth - 2 * toPx(64.0f, resources)).roundToInt() + if (width < 0) { return } // screenWidth initially evaluates to 0 val height = TextUtilities.getIntrinsicHeight(text, paint, width).toFloat() val constrainedHeight = min(max(height, snMinHeight), snMaxHeight) if (constrainedHeight.roundToInt() == this.height) { return }