Add support for group task notifications

This commit is contained in:
Carl Vuorinen 2019-05-07 21:32:39 +03:00
parent 96dbfd01c9
commit 615dbe03c0
5 changed files with 70 additions and 1 deletions

View file

@ -8,6 +8,8 @@ class Notification {
NEW_STUFF("NEW_STUFF"),
NEW_CHAT_MESSAGE("NEW_CHAT_MESSAGE"),
NEW_MYSTERY_ITEMS("NEW_MYSTERY_ITEMS"),
GROUP_TASK_NEEDS_WORK("GROUP_TASK_NEEDS_WORK"),
GROUP_TASK_APPROVED("GROUP_TASK_APPROVED"),
UNALLOCATED_STATS_POINTS("UNALLOCATED_STATS_POINTS");
}
@ -23,6 +25,8 @@ class Notification {
Type.LOGIN_INCENTIVE.type -> LoginIncentiveData::class.java
Type.NEW_STUFF.type -> NewStuffData::class.java
Type.NEW_CHAT_MESSAGE.type -> NewChatMessageData::class.java
Type.GROUP_TASK_NEEDS_WORK.type -> GroupTaskNeedsWorkData::class.java
Type.GROUP_TASK_APPROVED.type -> GroupTaskApprovedData::class.java
Type.UNALLOCATED_STATS_POINTS.type -> UnallocatedPointsData::class.java
else -> null
}

View file

@ -0,0 +1,7 @@
package com.habitrpg.android.habitica.models.notifications
open class GroupTaskApprovedData : NotificationData {
var groupId: String = ""
var message: String? = null
}

View file

@ -0,0 +1,7 @@
package com.habitrpg.android.habitica.models.notifications
open class GroupTaskNeedsWorkData : NotificationData {
var group: NotificationGroup? = null
var message: String? = null
}

View file

@ -8,6 +8,7 @@ import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProviders
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.components.AppComponent
@ -98,6 +99,8 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
Notification.Type.NEW_STUFF.type -> createNewStuffNotification(it)
Notification.Type.UNALLOCATED_STATS_POINTS.type -> createUnallocatedStatsNotification(it)
Notification.Type.NEW_MYSTERY_ITEMS.type -> createMysteryItemsNotification(it)
Notification.Type.GROUP_TASK_NEEDS_WORK.type -> createGroupTaskNeedsWorkNotification(it)
Notification.Type.GROUP_TASK_APPROVED.type -> createGroupTaskApprovedNotification(it)
//TODO rest of the notification types
else -> null
}
@ -157,7 +160,49 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
)
}
private fun createNotificationItem(notification: Notification, messageText: CharSequence, imageResourceId: Int? = null): View? {
private fun createGroupTaskNeedsWorkNotification(notification: Notification): View? {
val data = notification.data as? GroupTaskNeedsWorkData
val message = convertGroupMessageHtml(data?.message ?: "")
return createNotificationItem(
notification,
fromHtml(message),
null,
R.color.yellow_5
)
}
private fun createGroupTaskApprovedNotification(notification: Notification): View? {
val data = notification.data as? GroupTaskApprovedData
val message = convertGroupMessageHtml(data?.message ?: "")
return createNotificationItem(
notification,
fromHtml(message),
null,
R.color.green_10
)
}
/**
* Group task notifications have the message text in the notification data as HTML
* with <span class="notification-bold"> tags around emphasized words. So we just
* convert the span-tags to strong-tags to display correct parts as bold, since
* Html.fromHtml does not support CSS.
*/
private fun convertGroupMessageHtml(message: String): String {
// Using positive lookbehind to make sure "span" is preceded by "<" or "</"
val pattern = "(?<=</?)span".toRegex()
return message.replace(pattern, "strong")
}
private fun createNotificationItem(
notification: Notification,
messageText: CharSequence,
imageResourceId: Int? = null,
textColor: Int? = null
): View? {
val item = inflater.inflate(R.layout.notification_item, notification_items, false)
val dismissButton = item?.findViewById(R.id.dismiss_button) as? ImageView
@ -172,6 +217,10 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
notificationImage?.visibility = View.VISIBLE
}
if (textColor != null) {
messageTextView?.setTextColor(ContextCompat.getColor(this, textColor))
}
return item
}

View file

@ -24,6 +24,8 @@ open class NotificationsViewModel : BaseViewModel() {
Notification.Type.NEW_STUFF.type,
Notification.Type.NEW_CHAT_MESSAGE.type,
Notification.Type.NEW_MYSTERY_ITEMS.type,
Notification.Type.GROUP_TASK_NEEDS_WORK.type,
Notification.Type.GROUP_TASK_APPROVED.type,
Notification.Type.UNALLOCATED_STATS_POINTS.type
)