moving some more functions from NewMessage to Thread activity

This commit is contained in:
tibbi 2020-04-06 21:10:19 +02:00
parent 3f2b88785a
commit c4f209b8ff
5 changed files with 91 additions and 95 deletions

View File

@ -6,12 +6,7 @@ import android.os.Bundle
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.smsmessenger.R
@ -22,11 +17,9 @@ import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.models.Contact
import kotlinx.android.synthetic.main.activity_new_message.*
import kotlinx.android.synthetic.main.item_selected_contact.view.*
class NewMessageActivity : SimpleActivity() {
private var allContacts = ArrayList<Contact>()
private var selectedContacts = ArrayList<Contact>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -97,86 +90,6 @@ class NewMessageActivity : SimpleActivity() {
}
}
private fun addSelectedContact(contact: Contact) {
new_message_to.setText("")
if (selectedContacts.map { it.id }.contains(contact.id)) {
return
}
selectedContacts.add(contact)
showSelectedContacts()
}
private fun showSelectedContacts() {
selected_contacts.beVisibleIf(selectedContacts.isNotEmpty())
message_divider_one.beVisibleIf(selectedContacts.isNotEmpty())
val views = ArrayList<View>()
selectedContacts.forEach {
val contact = it
layoutInflater.inflate(R.layout.item_selected_contact, null).apply {
selected_contact_name.text = contact.name
selected_contact_remove.setOnClickListener {
removeSelectedContact(contact.id)
}
views.add(this)
}
}
showSelectedContact(views)
}
// show selected contacts, properly split to new lines when appropriate
// based on https://stackoverflow.com/a/13505029/1967672
private fun showSelectedContact(views: ArrayList<View>) {
selected_contacts.removeAllViews()
var newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
val sideMargin = (selected_contacts.layoutParams as RelativeLayout.LayoutParams).leftMargin
val parentWidth = realScreenSize.x - sideMargin * 2
val mediumMargin = resources.getDimension(R.dimen.medium_margin).toInt()
var widthSoFar = 0
var isFirstRow = true
for (i in views.indices) {
val LL = LinearLayout(this)
LL.orientation = LinearLayout.HORIZONTAL
LL.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM
LL.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
views[i].measure(0, 0)
var params = LayoutParams(views[i].measuredWidth, LayoutParams.WRAP_CONTENT)
params.setMargins(0, 0, mediumMargin, 0)
LL.addView(views[i], params)
LL.measure(0, 0)
widthSoFar += views[i].measuredWidth + mediumMargin
if (widthSoFar >= parentWidth) {
isFirstRow = false
selected_contacts.addView(newLinearLayout)
newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
params = LayoutParams(LL.measuredWidth, LL.measuredHeight)
params.topMargin = mediumMargin
newLinearLayout.addView(LL, params)
widthSoFar = LL.measuredWidth
} else {
if (!isFirstRow) {
(LL.layoutParams as LayoutParams).topMargin = mediumMargin
}
newLinearLayout.addView(LL)
}
}
selected_contacts.addView(newLinearLayout)
}
private fun removeSelectedContact(id: Int) {
selectedContacts = selectedContacts.filter { it.id != id }.toMutableList() as ArrayList<Contact>
showSelectedContacts()
}
private fun getNames(): List<Contact> {
val contacts = ArrayList<Contact>()
val uri = ContactsContract.Data.CONTENT_URI

View File

@ -5,13 +5,13 @@ import android.content.Intent
import android.os.Bundle
import android.provider.Telephony
import android.telephony.SmsManager
import android.util.Log
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.onTextChangeListener
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
@ -23,6 +23,7 @@ import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.SmsSentReceiver
import kotlinx.android.synthetic.main.activity_thread.*
import kotlinx.android.synthetic.main.item_selected_contact.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
@ -33,6 +34,7 @@ class ThreadActivity : SimpleActivity() {
private var targetNumber = ""
private var threadId = 0
private var bus: EventBus? = null
private var selectedContacts = ArrayList<Contact>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -63,6 +65,8 @@ class ThreadActivity : SimpleActivity() {
targetNumber = thread.address
bus = EventBus.getDefault()
bus!!.register(this)
val contact = Contact(0, thread.title, "", targetNumber, false)
selectedContacts.add(contact)
ensureBackgroundThread {
setupAdapter()
@ -127,7 +131,33 @@ class ThreadActivity : SimpleActivity() {
}
private fun addPerson() {
showSelectedContacts()
thread_add_contacts.beVisible()
}
private fun showSelectedContacts() {
val views = ArrayList<View>()
selectedContacts.forEach {
val contact = it
layoutInflater.inflate(R.layout.item_selected_contact, null).apply {
selected_contact_name.text = contact.name
selected_contact_remove.setOnClickListener {
removeSelectedContact(contact.id)
}
views.add(this)
}
}
showSelectedContact(views)
}
private fun addSelectedContact(contact: Contact) {
new_message_to.setText("")
if (selectedContacts.map { it.id }.contains(contact.id)) {
return
}
selectedContacts.add(contact)
showSelectedContacts()
}
private fun getThreadItems(threadID: Int): ArrayList<ThreadItem> {
@ -162,6 +192,59 @@ class ThreadActivity : SimpleActivity() {
return items
}
// show selected contacts, properly split to new lines when appropriate
// based on https://stackoverflow.com/a/13505029/1967672
private fun showSelectedContact(views: ArrayList<View>) {
selected_contacts.removeAllViews()
var newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams =
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
val sideMargin = (selected_contacts.layoutParams as RelativeLayout.LayoutParams).leftMargin
val parentWidth = realScreenSize.x - sideMargin * 2
val mediumMargin = resources.getDimension(R.dimen.medium_margin).toInt()
var widthSoFar = 0
var isFirstRow = true
for (i in views.indices) {
val LL = LinearLayout(this)
LL.orientation = LinearLayout.HORIZONTAL
LL.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM
LL.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
views[i].measure(0, 0)
var params = LinearLayout.LayoutParams(views[i].measuredWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
params.setMargins(0, 0, mediumMargin, 0)
LL.addView(views[i], params)
LL.measure(0, 0)
widthSoFar += views[i].measuredWidth + mediumMargin
if (widthSoFar >= parentWidth) {
isFirstRow = false
selected_contacts.addView(newLinearLayout)
newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams =
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
params = LinearLayout.LayoutParams(LL.measuredWidth, LL.measuredHeight)
params.topMargin = mediumMargin
newLinearLayout.addView(LL, params)
widthSoFar = LL.measuredWidth
} else {
if (!isFirstRow) {
(LL.layoutParams as LinearLayout.LayoutParams).topMargin = mediumMargin
}
newLinearLayout.addView(LL)
}
}
selected_contacts.addView(newLinearLayout)
}
private fun removeSelectedContact(id: Int) {
selectedContacts = selectedContacts.filter { it.id != id }.toMutableList() as ArrayList<Contact>
showSelectedContacts()
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun refreshMessages(event: Events.RefreshMessages) {
setupAdapter()

View File

@ -31,7 +31,7 @@
android:layout_marginEnd="@dimen/activity_margin"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:hint="@string/send_to"
android:hint="@string/add_contact_or_number"
android:inputType="textCapWords"
android:textSize="@dimen/big_text_size" />

View File

@ -37,7 +37,7 @@
android:background="@android:color/transparent"
android:completionThreshold="2"
android:gravity="center_vertical"
android:hint="@string/send_to"
android:hint="@string/add_contact_or_number"
android:inputType="textCapWords"
android:textSize="@dimen/big_text_size" />

View File

@ -7,7 +7,7 @@
<!-- New message -->
<string name="create_new_message">Create new message</string>
<string name="send_to">To</string>
<string name="add_contact_or_number">Add Contact or Phone Number</string>
<!-- Notifications -->
<string name="channel_received_sms">Received SMS</string>