Skip to content

Commit

Permalink
Convert notification detail page to compose (#1822)
Browse files Browse the repository at this point in the history
* Convert notification detail page to compose

* Switch to fragment instead of preference

* Pass in the data to compose for recomposition
  • Loading branch information
dshokouhi authored Oct 28, 2021
1 parent 38dd8b4 commit 27f8c18
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.URL
import java.util.Locale
import javax.inject.Inject
Expand Down Expand Up @@ -178,9 +179,11 @@ class MessagingService : FirebaseMessagingService() {
// Check if message contains a data payload.
remoteMessage.data.let {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
val jsonData: Map<String, String> = it
val jsonObject = JSONObject(jsonData)
val notificationDao = AppDatabase.getInstance(applicationContext).notificationDao()
val now = System.currentTimeMillis()
val notificationRow = NotificationItem(0, now, it[MESSAGE].toString(), it.toString())
val notificationRow = NotificationItem(0, now, it[MESSAGE].toString(), jsonObject.toString())
notificationDao.add(notificationRow)

when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,40 @@ package io.homeassistant.companion.android.settings.notification
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import android.text.Html.fromHtml
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.android.material.composethemeadapter.MdcTheme
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.notification.NotificationItem
import kotlinx.android.parcel.Parcelize
import java.util.Calendar
import java.util.GregorianCalendar

class NotificationDetailFragment(
private val notification: NotificationItem
) :
PreferenceFragmentCompat() {
Fragment() {

companion object {
fun newInstance(
Expand Down Expand Up @@ -50,22 +69,80 @@ class NotificationDetailFragment(
return super.onOptionsItemSelected(item)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

addPreferencesFromResource(R.xml.notification_detail)

findPreference<Preference>("received_at")?.let {
val cal: Calendar = GregorianCalendar()
cal.timeInMillis = notification.received
it.summary = cal.time.toString()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
MdcTheme {
LoadNotification(notification)
}
}
}
}

@Parcelize
data class NotificationData(val received: Long, val message: String, val data: String) : Parcelable

findPreference<Preference>("message")?.let {
it.summary = fromHtml(notification.message)
@Composable
private fun LoadNotification(notification: NotificationItem) {
val notificationItem = rememberSaveable {
NotificationData(notification.received, notification.message, notification.data)
}

findPreference<Preference>("data")?.let {
it.summary = notification.data
Column {
Text(
text = getString(R.string.notification_received_at),
fontWeight = FontWeight.ExtraBold,
fontSize = 20.sp,
modifier = Modifier
.padding(top = 30.dp, bottom = 20.dp, start = 10.dp)
)
val cal: Calendar = GregorianCalendar()
cal.timeInMillis = notificationItem.received
Text(
text = cal.time.toString(),
modifier = Modifier
.padding(start = 20.dp)
)
Text(
text = getString(R.string.notification_message),
fontWeight = FontWeight.ExtraBold,
fontSize = 20.sp,
modifier = Modifier
.padding(top = 20.dp, bottom = 20.dp, start = 10.dp)
)
AndroidView(factory = { context ->
TextView(context).apply {
text = fromHtml(notificationItem.message)
setPadding(80, 0, 0, 0)
textSize = 16f
}
})
Text(
text = getString(R.string.notification_data),
fontWeight = FontWeight.ExtraBold,
fontSize = 20.sp,
modifier = Modifier
.padding(top = 20.dp, bottom = 20.dp, start = 10.dp)
)
val notifData =
try {
val mapper = ObjectMapper()
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
val jsonObject = mapper.readValue(notificationItem.data, Object::class.java)
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject)
} catch (e: Exception) {
notificationItem.data
}

Text(
text = notifData,
modifier = Modifier
.padding(start = 20.dp)
)
}
}

Expand Down
23 changes: 0 additions & 23 deletions app/src/main/res/xml/notification_detail.xml

This file was deleted.

0 comments on commit 27f8c18

Please sign in to comment.