diff --git a/basicSample/src/androidTest/java/info/mqtt/java/example/ConnectTest.kt b/basicSample/src/androidTest/java/info/mqtt/java/example/ConnectTest.kt index 530da64e..5d989419 100644 --- a/basicSample/src/androidTest/java/info/mqtt/java/example/ConnectTest.kt +++ b/basicSample/src/androidTest/java/info/mqtt/java/example/ConnectTest.kt @@ -2,12 +2,12 @@ package info.mqtt.java.example import android.Manifest import androidx.test.espresso.matcher.ViewMatchers.* -import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.GrantPermissionRule import com.moka.lib.assertions.MatchOperator import com.moka.lib.assertions.WaitingAssertion import com.moka.utils.Screenshot +import com.moka.utils.ScreenshotActivityRule import org.junit.Ignore import org.junit.Rule import org.junit.Test @@ -17,7 +17,7 @@ import org.junit.runner.RunWith class ConnectTest { @get:Rule - var mActivityTestRule = ActivityScenarioRule(MQTTExampleActivity::class.java) + var mActivityTestRule = ScreenshotActivityRule(MQTTExampleActivity::class.java) @get:Rule val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant( diff --git a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedMainTest.kt b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedMainTest.kt index af69d6d2..61a6273b 100644 --- a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedMainTest.kt +++ b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedMainTest.kt @@ -2,7 +2,6 @@ package info.mqtt.android.extsample.activity import android.Manifest import androidx.test.espresso.matcher.ViewMatchers.* -import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.GrantPermissionRule import org.junit.Rule @@ -16,6 +15,7 @@ import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.contrib.DrawerMatchers.isClosed import com.moka.utils.Screenshot +import com.moka.utils.ScreenshotActivityRule import info.mqtt.android.extsample.MainActivity import info.mqtt.android.extsample.R @@ -24,7 +24,7 @@ import info.mqtt.android.extsample.R class ExtendedMainTest { @get:Rule - var mActivityTestRule = ActivityScenarioRule(MainActivity::class.java) + var mActivityTestRule = ScreenshotActivityRule(MainActivity::class.java) @get:Rule val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant( diff --git a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishTest.kt b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishTest.kt index faf1c901..5811e031 100644 --- a/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishTest.kt +++ b/extendedSample/src/androidTest/java/info/mqtt/android/extsample/activity/ExtendedPublishTest.kt @@ -2,7 +2,6 @@ package info.mqtt.android.extsample.activity import android.Manifest import androidx.test.espresso.matcher.ViewMatchers.* -import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.GrantPermissionRule import org.junit.Rule @@ -18,6 +17,7 @@ import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.contrib.DrawerMatchers.isClosed import com.moka.lib.assertions.WaitingAssertion import com.moka.utils.Screenshot +import com.moka.utils.ScreenshotActivityRule import info.mqtt.android.extsample.MainActivity import info.mqtt.android.extsample.R @@ -26,7 +26,7 @@ import info.mqtt.android.extsample.R class ExtendedPublishTest { @get:Rule - var mActivityTestRule = ActivityScenarioRule(MainActivity::class.java) + var mActivityTestRule = ScreenshotActivityRule(MainActivity::class.java) @get:Rule val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant( diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/HistoryListItemAdapter.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/HistoryListItemAdapter.kt index eca09c2f..0bc615a6 100644 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/HistoryListItemAdapter.kt +++ b/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/HistoryListItemAdapter.kt @@ -9,7 +9,7 @@ import android.widget.ArrayAdapter import info.mqtt.android.extsample.R import info.mqtt.android.extsample.databinding.MessageListItemBinding -class HistoryListItemAdapter(context: Context, var history: List) : +class HistoryListItemAdapter(context: Context, private var history: List) : ArrayAdapter(context, R.layout.message_list_item, history) { @SuppressLint("ViewHolder", "SimpleDateFormat") @@ -18,8 +18,7 @@ class HistoryListItemAdapter(context: Context, var history: List) : var row = convertView if (row == null) { - val inflater = - context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater binding = MessageListItemBinding.inflate(inflater, parent, false) row = binding.root } else { diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/MessageListItemAdapter.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/MessageListItemAdapter.kt index 614fee99..1736e674 100644 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/MessageListItemAdapter.kt +++ b/extendedSample/src/main/java/info/mqtt/android/extsample/adapter/MessageListItemAdapter.kt @@ -12,7 +12,7 @@ import info.mqtt.android.extsample.model.ReceivedMessage import info.mqtt.android.service.QoS import java.text.SimpleDateFormat -class MessageListItemAdapter(context: Context, var messages: List) : +class MessageListItemAdapter(context: Context, private var messages: List) : ArrayAdapter(context, R.layout.message_list_item, messages) { @SuppressLint("ViewHolder", "SimpleDateFormat", "SetTextI18n") @@ -21,8 +21,7 @@ class MessageListItemAdapter(context: Context, var messages: List + historyListItemAdapter.notifyDataSetChanged() }) + return binding.root } diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/fragments/MessagesFragment.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/fragments/MessagesFragment.kt index 7a228385..6d90ee06 100644 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/fragments/MessagesFragment.kt +++ b/extendedSample/src/main/java/info/mqtt/android/extsample/fragments/MessagesFragment.kt @@ -1,8 +1,6 @@ package info.mqtt.android.extsample.fragments import android.os.Bundle -import android.os.Handler -import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -12,8 +10,6 @@ import info.mqtt.android.extsample.adapter.MessageListItemAdapter import info.mqtt.android.extsample.databinding.FragmentConnectionHistoryBinding import info.mqtt.android.extsample.internal.Connection import info.mqtt.android.extsample.internal.Connections -import info.mqtt.android.extsample.internal.IReceivedMessageListener -import info.mqtt.android.extsample.model.ReceivedMessage import timber.log.Timber class MessagesFragment : Fragment() { @@ -38,24 +34,15 @@ class MessagesFragment : Fragment() { setHasOptionsMenu(true) Timber.d("CONNECTION_KEY=${requireArguments().getString(ActivityConstants.CONNECTION_KEY)} '${connection.id}'") setHasOptionsMenu(true) - connection.addReceivedMessageListener(object : IReceivedMessageListener { - override var identifer: String = MessagesFragment::class.java.simpleName - - override fun onMessageReceived(message: ReceivedMessage?) { - Timber.d("Message in history ${String(message?.message?.payload!!)} ${connection.messages.size} ${Thread.currentThread().name}") - messageListAdapter.messages = listOf(*connection.messages.toTypedArray()) - messageListAdapter.notifyDataSetChanged() - } + connection.messages.observe(this.viewLifecycleOwner, { + messageListAdapter.notifyDataSetChanged() }) - val tempList = listOf(*connection.messages.toTypedArray()) - messageListAdapter = MessageListItemAdapter(requireContext(), tempList) + messageListAdapter = MessageListItemAdapter(requireContext(), connection.messages.value!!) binding.historyListView.adapter = messageListAdapter binding.historyClearButton.setOnClickListener { - Handler(Looper.getMainLooper()).run { - connection.messages.clear() - messageListAdapter.notifyDataSetChanged() - } + connection.messageList.clear() + messageListAdapter.notifyDataSetChanged() } } diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt index a42b4a79..fe7792da 100644 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt +++ b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt @@ -3,9 +3,10 @@ package info.mqtt.android.extsample.internal import android.annotation.SuppressLint import android.content.Context import android.content.Intent -import info.mqtt.android.extsample.R +import androidx.lifecycle.MutableLiveData import info.mqtt.android.extsample.ActivityConstants import info.mqtt.android.extsample.MainActivity +import info.mqtt.android.extsample.R import info.mqtt.android.extsample.internal.Notify.notification import info.mqtt.android.extsample.model.ReceivedMessage import info.mqtt.android.extsample.model.Subscription @@ -34,14 +35,16 @@ class Connection private constructor( ) { private val listeners = ArrayList() private val subscriptions: MutableMap = HashMap() - val messages = ArrayList() - val history: ArrayList = ArrayList() - private val receivedMessageListeners = ArrayList() - private val historyListeners = ArrayList() + val messageList =ArrayList() + val historyList = ArrayList() + val messages = MutableLiveData>() + val history = MutableLiveData>() private var status = ConnectionStatus.NONE init { + messages.postValue(arrayListOf()) + history.postValue(arrayListOf()) addHistory("Client: $id created") } @@ -61,10 +64,8 @@ class Connection private constructor( @SuppressLint("SimpleDateFormat") fun addHistory(action: String) { val timestamp = SimpleDateFormat("HH:mm.ss.SSS").format(Date(System.currentTimeMillis())) - history.add(action + timestamp) - for (listener in historyListeners) { - listener.onHistoryReceived(action + timestamp) - } + historyList.add(action + timestamp) + history.postValue(historyList) notifyListeners(PropertyChangeEvent(this, ActivityConstants.historyProperty, null, null)) } @@ -166,20 +167,11 @@ class Connection private constructor( } } - fun addReceivedMessageListener(listener: IReceivedMessageListener) { - if (receivedMessageListeners.firstOrNull { it.identifer == listener.identifer } == null) - receivedMessageListeners.add(listener) - } - - fun addHistoryListener(listener: IHistoryListener) { - if (historyListeners.firstOrNull { it.identifer == listener.identifer } == null) - historyListeners.add(listener) - } - @SuppressLint("SimpleDateFormat") fun addMessage(topic: String, message: MqttMessage) { val msg = ReceivedMessage(topic, message) - messages.add(0, msg) + messageList.add(0, msg) + messages.postValue(messageList) if (subscriptions.containsKey(topic)) { if (subscriptions[topic]!!.isEnableNotifications) { //create intent to start activity @@ -191,9 +183,6 @@ class Connection private constructor( notification(context, context.getString(R.string.notification, String(message.payload), topic), intent, R.string.notifyTitle) } } - for (listener in receivedMessageListeners) { - listener.onMessageReceived(msg) - } } override fun hashCode(): Int { diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IHistoryListener.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IHistoryListener.kt deleted file mode 100644 index 18111023..00000000 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IHistoryListener.kt +++ /dev/null @@ -1,6 +0,0 @@ -package info.mqtt.android.extsample.internal - -interface IHistoryListener { - var identifer : String - fun onHistoryReceived(history: String) -} diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IReceivedMessageListener.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IReceivedMessageListener.kt deleted file mode 100644 index abd8f197..00000000 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/IReceivedMessageListener.kt +++ /dev/null @@ -1,8 +0,0 @@ -package info.mqtt.android.extsample.internal - -import info.mqtt.android.extsample.model.ReceivedMessage - -interface IReceivedMessageListener { - var identifer : String - fun onMessageReceived(message: ReceivedMessage?) -}