Implement chat message limit. Fixes #993

This commit is contained in:
Phillip Thelen 2018-07-10 17:01:30 +02:00
parent fcc67d05f7
commit 9e7ae94979
3 changed files with 59 additions and 15 deletions

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:id="@+id/chatBarContainer"
android:layout_width="match_parent"
@ -46,17 +47,39 @@
android:paddingLeft="@dimen/spacing_medium"
android:paddingRight="@dimen/spacing_medium"
android:paddingTop="4dp"
android:paddingBottom="4dp"/>
android:paddingBottom="4dp"
tools:height="200dp"/>
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="@+id/sendButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@color/transparent"
android:tint="@color/gray_400"
android:src="@drawable/ic_send_grey_600_24dp"
android:contentDescription="@string/send"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="@dimen/spacing_medium"
android:layout_marginBottom="@dimen/spacing_medium"
android:gravity="bottom">
<TextView
android:id="@+id/text_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="100/150"
android:visibility="gone"
/>
<View
android:id="@+id/indicator_spacing"
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone"/>
<ImageButton
android:id="@+id/sendButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@color/transparent"
android:tint="@color/gray_400"
android:src="@drawable/ic_send_grey_600_24dp"
android:contentDescription="@string/send"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/communityGuidelinesView"

View file

@ -382,7 +382,7 @@ public class MainActivity extends BaseActivity implements TutorialView.OnTutoria
@Override
protected void onPause() {
updateWidgets()
updateWidgets();
super.onPause();
}

View file

@ -9,13 +9,12 @@ import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.extensions.bindView
import com.habitrpg.android.habitica.helpers.RxErrorHandler
import com.habitrpg.android.habitica.ui.helpers.NavbarUtils
class ChatBarView : FrameLayout {
@ -23,9 +22,13 @@ class ChatBarView : FrameLayout {
private val chatBarContainer: LinearLayout by bindView(R.id.chatBarContainer)
private val sendButton: ImageButton by bindView(R.id.sendButton)
private val chatEditText: AppCompatEditText by bindView(R.id.chatEditText)
private val textIndicator: TextView by bindView(R.id.text_indicator)
private val indicatorSpacing: View by bindView(R.id.indicator_spacing)
private var navBarAccountedHeightCalculated = false
private var maxChatLength = 200
var sendAction: ((String) -> Unit)? = null
constructor(context: Context) : super(context) {
@ -47,7 +50,8 @@ class ChatBarView : FrameLayout {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
setSendButtonEnabled(chatEditText.text.isNotEmpty())
setSendButtonEnabled(chatEditText.text.isNotEmpty() && chatEditText.text.length <= maxChatLength)
updateTextIndicator(chatEditText.text.toString())
}
})
@ -56,6 +60,23 @@ class ChatBarView : FrameLayout {
resizeForDrawingUnderNavbar()
}
private fun updateTextIndicator(text: String) {
if (chatEditText.lineCount >= 3) {
textIndicator.visibility = View.VISIBLE
indicatorSpacing.visibility = View.VISIBLE
textIndicator.text = "${text.length}/${maxChatLength}"
val color = when {
text.length > maxChatLength -> R.color.red_50
text.length > (maxChatLength * 0.95) -> R.color.yellow_5
else -> R.color.gray_400
}
textIndicator.setTextColor(ContextCompat.getColor(context, color))
} else {
textIndicator.visibility = View.GONE
indicatorSpacing.visibility = View.GONE
}
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
if (changed) {