use EventBus for triggering sms list refreshes

This commit is contained in:
tibbi 2020-04-04 21:23:28 +02:00
parent d3eee79d0e
commit 29c0f928ca
5 changed files with 62 additions and 5 deletions

View File

@ -38,4 +38,5 @@ android {
dependencies {
implementation 'com.simplemobiletools:commons:5.24.15'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'org.greenrobot:eventbus:3.2.0'
}

View File

@ -10,6 +10,7 @@ import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.appLaunched
import com.simplemobiletools.commons.extensions.checkAppSideloading
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_READ_SMS
import com.simplemobiletools.commons.helpers.isQPlus
@ -22,13 +23,19 @@ import com.simplemobiletools.smsmessenger.extensions.getMessages
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.models.Events
import com.simplemobiletools.smsmessenger.models.Message
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
class MainActivity : SimpleActivity() {
private var storedTextColor = 0
private val MAKE_DEFAULT_APP_REQUEST = 1
private var storedTextColor = 0
private var bus: EventBus? = null
@SuppressLint("InlinedApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -70,6 +77,11 @@ class MainActivity : SimpleActivity() {
storeStateVariables()
}
override fun onDestroy() {
super.onDestroy()
bus?.unregister(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
@ -104,6 +116,8 @@ class MainActivity : SimpleActivity() {
handlePermission(PERMISSION_READ_SMS) {
if (it) {
handlePermission(PERMISSION_READ_CONTACTS) {
bus = EventBus.getDefault()
bus!!.register(this)
initMessenger()
}
} else {
@ -132,7 +146,7 @@ class MainActivity : SimpleActivity() {
}
private fun launchAbout() {
val licenses = 0
val licenses = LICENSE_EVENT_BUS
val faqItems = arrayListOf(
FAQItem(R.string.faq_2_title_commons, R.string.faq_2_text_commons),
@ -141,4 +155,9 @@ class MainActivity : SimpleActivity() {
startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun refreshMessages(event: Events.RefreshMessages) {
initMessenger()
}
}

View File

@ -14,27 +14,44 @@ import com.simplemobiletools.smsmessenger.extensions.markSMSRead
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.models.Events
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
import com.simplemobiletools.smsmessenger.models.ThreadError
import com.simplemobiletools.smsmessenger.models.ThreadItem
import kotlinx.android.synthetic.main.activity_thread.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
class ThreadActivity : SimpleActivity() {
private val MIN_DATE_TIME_DIFF_SECS = 300
private var targetNumber = ""
private var bus: EventBus? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_thread)
title = intent.getStringExtra(THREAD_NAME) ?: getString(R.string.app_launcher_name)
val threadID = intent.getIntExtra(THREAD_ID, 0)
targetNumber = intent.getStringExtra(THREAD_NUMBER)!!
bus = EventBus.getDefault()
bus!!.register(this)
setupAdapter()
setupButtons()
}
override fun onDestroy() {
super.onDestroy()
bus?.unregister(this)
}
private fun setupAdapter() {
val threadID = intent.getIntExtra(THREAD_ID, 0)
val items = getThreadItems(threadID)
val adapter = ThreadAdapter(this, items, thread_messages_list, thread_messages_fastscroller) {}
thread_messages_list.adapter = adapter
setupButtons()
}
private fun setupButtons() {
@ -63,6 +80,7 @@ class ThreadActivity : SimpleActivity() {
val items = ArrayList<ThreadItem>()
var prevDateTime = 0
var hadUnreadItems = false
messages.forEach {
// do not show the date/time above every message, only if the difference between the 2 messages is at least MIN_DATE_TIME_DIFF_SECS
if (it.date - prevDateTime > MIN_DATE_TIME_DIFF_SECS) {
@ -76,10 +94,20 @@ class ThreadActivity : SimpleActivity() {
}
if (!it.read) {
hadUnreadItems = true
markSMSRead(it.id)
}
}
if (hadUnreadItems) {
bus?.post(Events.RefreshMessages())
}
return items
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun refreshMessages(event: Events.RefreshMessages) {
setupAdapter()
}
}

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.smsmessenger.models
class Events {
class RefreshMessages
}

View File

@ -5,6 +5,8 @@ import android.content.Context
import android.content.Intent
import android.provider.Telephony
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
import com.simplemobiletools.smsmessenger.models.Events
import org.greenrobot.eventbus.EventBus
class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
@ -16,5 +18,7 @@ class SmsReceiver : BroadcastReceiver() {
val date = it.timestampMillis
context.insertNewSMS(address, subject, body, date)
}
EventBus.getDefault().post(Events.RefreshMessages())
}
}