Skip to content

Commit

Permalink
Merge pull request #206 from hannesa2/ExampleListLifedata
Browse files Browse the repository at this point in the history
Example lists with LiveData
  • Loading branch information
hannesa2 authored Sep 12, 2021
2 parents d736451 + fe7f587 commit 3932d6f
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) :
class HistoryListItemAdapter(context: Context, private var history: List<String>) :
ArrayAdapter<String>(context, R.layout.message_list_item, history) {

@SuppressLint("ViewHolder", "SimpleDateFormat")
Expand All @@ -18,8 +18,7 @@ class HistoryListItemAdapter(context: Context, var history: List<String>) :
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReceivedMessage>) :
class MessageListItemAdapter(context: Context, private var messages: List<ReceivedMessage>) :
ArrayAdapter<ReceivedMessage>(context, R.layout.message_list_item, messages) {

@SuppressLint("ViewHolder", "SimpleDateFormat", "SetTextI18n")
Expand All @@ -21,8 +21,7 @@ class MessageListItemAdapter(context: Context, var messages: List<ReceivedMessag
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +10,6 @@ import info.mqtt.android.extsample.adapter.HistoryListItemAdapter
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.IHistoryListener
import timber.log.Timber

class HistoryFragment : Fragment() {
Expand All @@ -36,24 +33,17 @@ class HistoryFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentConnectionHistoryBinding.inflate(inflater, container, false)
historyListItemAdapter = HistoryListItemAdapter(requireContext(), listOf(*connection.history.toTypedArray()))
historyListItemAdapter = HistoryListItemAdapter(requireContext(), connection.history.value!!)
binding.historyListView.adapter = historyListItemAdapter
binding.historyClearButton.setOnClickListener {
Handler(Looper.getMainLooper()).run {
connection.history.clear()
historyListItemAdapter.history = listOf(*connection.history.toTypedArray())
historyListItemAdapter.notifyDataSetChanged()
}
connection.historyList.clear()
historyListItemAdapter.notifyDataSetChanged()
}

connection.addHistoryListener(object : IHistoryListener {
override var identifer: String = HistoryFragment::class.java.simpleName

override fun onHistoryReceived(history: String) {
historyListItemAdapter.history = listOf(*connection.history.toTypedArray())
historyListItemAdapter.notifyDataSetChanged()
}
connection.history.observe(this.viewLifecycleOwner, { items ->
historyListItemAdapter.notifyDataSetChanged()
})

return binding.root
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand All @@ -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()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,14 +35,16 @@ class Connection private constructor(
) {
private val listeners = ArrayList<PropertyChangeListener>()
private val subscriptions: MutableMap<String, Subscription> = HashMap()
val messages = ArrayList<ReceivedMessage>()
val history: ArrayList<String> = ArrayList()
private val receivedMessageListeners = ArrayList<IReceivedMessageListener>()
private val historyListeners = ArrayList<IHistoryListener>()
val messageList =ArrayList<ReceivedMessage>()
val historyList = ArrayList<String>()
val messages = MutableLiveData<MutableList<ReceivedMessage>>()
val history = MutableLiveData<MutableList<String>>()

private var status = ConnectionStatus.NONE

init {
messages.postValue(arrayListOf())
history.postValue(arrayListOf())
addHistory("Client: $id created")
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 3932d6f

Please sign in to comment.