Reputation: 195
I'm learning Android development on my own. I thought I had gotten my head around what/how 'liveData.observe' worked. But...
Making a small simple app that listens for input from a Bluetooth device and will display or hide an image based on that bluetooth input.
I have all that working, but now I want to add another feature and i'm running into a wall. The feature is to increase a counter each time the liveData.Observe returns TRUE.
Either I don't understand 'liveData.observe' or i'm trying to use it incorrectly(it is probably both of these things).
I'm working on a function now to increase this counter and this is the spot i'm getting hung up on. My current thinking is to create a seperate function(pigCounter()). But I'm getting nowhere.
onCreate
class MainActivity : AppCompatActivity() {
private var liveData: MutableLiveData<String> = MutableLiveData()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
<SNIP>
...
</SNIP>
liveData.observe(this, androidx.lifecycle.Observer {
imageView_mothership_LED_state.showOrHideImage(it == "1")
})
pigCounter()
}
Below is from activity_main.xml
imageView_mothership_LED_state
<ImageView
android:id="@+id/imageView_mothership_LED_state"
android:layout_width="124dp"
android:layout_height="144dp"
android:scaleType="fitCenter"
android:soundEffectsEnabled="false"
app:layout_constraintBottom_toBottomOf="@+id/imageView_blueYesOrNo"
app:layout_constraintEnd_toEndOf="@+id/imageView_blueYesOrNo"
app:layout_constraintStart_toStartOf="@+id/imageView_blueYesOrNo"
app:layout_constraintTop_toTopOf="@+id/imageView_blueYesOrNo"
app:srcCompat="@drawable/ic_check_black_24dp"
android:contentDescription="Check mark image to indicate if LED is on/off" />
Below is the function that sets the images visibility
showOrHideImage()
// display or don't display check mark image
private fun View.showOrHideImage(imageShow: Boolean) {
visibility = if (imageShow) View.VISIBLE else View.GONE
}
This is the function that handles the incoming bluetooth data.
readBlueToothDataFromMothership()
private fun readBlueToothDataFromMothership(bluetoothSocket: BluetoothSocket) {
Log.i(LOGTAG, Thread.currentThread().name)
val bluetoothSocketInputStream = bluetoothSocket.inputStream
val buffer = ByteArray(1024)
var bytes: Int
//Loop to listen for received bluetooth messages
while (true) {
try {
bytes = bluetoothSocketInputStream.read(buffer)
val readMessage = String(buffer, 0, bytes)
liveData.postValue(readMessage)
} catch (e: IOException) {
e.printStackTrace()
break
}
}
This is the function i'm writing, in that I want to increase a counter each time.
pigCount
private fun pigCount() {
var count = 0
var counterTextView = findViewById<TextView>(R.id.textView_blueCounter)
if(imageView_mothership_LED_state.showOrHideImage(true)) {
counterTextView.text = count.toString()
count++
}
This does not work due to a type mismatch(expecting a Boolean, getting Unit). I've also tried moving this into the liveData.observe function up in onCreate(). But same road block.
Can anyone point me in the right direction. I have a sneaky feeling I am way off on this. And would appreicate a nudge. :)
Upvotes: 0
Views: 207
Reputation: 8467
To count how many times a LiveData
returns true
it is really easy, you could just use a MediatorLiveData
.
Assuming you have a proper LiveData<Boolean>
, you could do as follows:
val trueFalse = MutableLiveData<Boolean>()
val counter by lazy {
MediatorLiveData<Int>().apply {
value = 0 // Initialize the counter
// Add the trueFalse as a source of this live data
addSource(trueFalse) { boolVal ->
if(boolVal == true) {
// If the mediator live data had a value, use it, if null use 0; and add +1 to it because the boolVal was true
value = (value ?: 0) + 1
}
}
}
}
/* Somewhere else in the code */
fun setupTextView() {
counter.observe({lifecycle}) {
val tv = findViewById<TextView>(R.id.tv_something)
tv.text = "$it"
}
}
This creates a MediatorLiveData
that will have as a source the trueFalse live data.
If the value is true
will proceed and post a new value in the counter
live data.
Upvotes: 1