Skip to content

Commit

Permalink
progress on read history
Browse files Browse the repository at this point in the history
  • Loading branch information
rlam20 committed Jul 1, 2024
1 parent 8d21bda commit 28b4ce5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
Empty file.
10 changes: 8 additions & 2 deletions app/src/main/java/org/ole/planet/myplanet/model/Notifications.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.ole.planet.myplanet.model

data class Notifications(var icon: Int, var text: String) {
data class Notifications(var id: Int, var icon: Int, var text: String) {

constructor(icon: Int, text: String, timestamp: String, isRead: Boolean) : this(icon, text) {
constructor(id: Int, icon: Int, text: String, timestamp: String, isRead: Boolean) : this(id, icon, text) {
this.timestamp = timestamp
this.isRead = isRead
}

constructor(icon: Int, text: String) : this(0, icon, text){

}

constructor(id: String?, icon: Int, text: String) : this(0,0,text)

var timestamp: String = ""
var isRead: Boolean = false
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ole.planet.myplanet.ui.dashboard.notification

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -17,9 +18,11 @@ class AdapterNotification(
private val notificationList: MutableList<Notifications>,
private val callback: NotificationCallback,
private val showMarkAsReadButton: Boolean,
private val showImages: Boolean // Add this flag
private val showImages: Boolean
) : RecyclerView.Adapter<AdapterNotification.ViewHolderNotification>() {

private val sharedPrefs = context.getSharedPreferences("notifications_prefs", Context.MODE_PRIVATE)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderNotification {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.row_notification, parent, false)
Expand All @@ -38,14 +41,13 @@ class AdapterNotification(
private val title: TextView = itemView.findViewById(R.id.title)
private val timestamp: TextView = itemView.findViewById(R.id.timestamp)
private val btnMarkAsRead: Button = itemView.findViewById(R.id.btn_mark_as_read)
private val icon: ImageView = itemView.findViewById(R.id.icon) // Reference to ImageView
private val icon: ImageView = itemView.findViewById(R.id.icon)

fun bind(notification: Notifications, showImages: Boolean) {
title.text = notification.text
timestamp.visibility = View.GONE // You can set the timestamp if available
timestamp.visibility = View.GONE
btnMarkAsRead.visibility = if (showMarkAsReadButton) View.VISIBLE else View.GONE

// Conditionally show or hide the icon
if (showImages) {
icon.visibility = View.VISIBLE
icon.setImageResource(notification.icon)
Expand All @@ -54,7 +56,7 @@ class AdapterNotification(
}

btnMarkAsRead.setOnClickListener {
markAsRead(bindingAdapterPosition)
markAsRead(notification.id)
}

itemView.setOnClickListener {
Expand All @@ -70,12 +72,16 @@ class AdapterNotification(
}
}

private fun markAsRead(position: Int) {
notificationList.removeAt(position)
notifyItemRemoved(position)
private fun markAsRead(notificationId: Int) {
sharedPrefs.edit().putBoolean("notification_$notificationId", true).apply()
notificationList.removeAll { it.id == notificationId }
notifyDataSetChanged()
}

fun markAllAsRead() {
notificationList.forEach {
sharedPrefs.edit().putBoolean("notification_${it.id}", true).apply()
}
notificationList.clear()
notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ole.planet.myplanet.ui.dashboard.notification

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -39,13 +41,20 @@ class SeeAllNotificationsFragment : Fragment() {
.equalTo("assignee", model.id)
.findAll()

val sharedPrefs = requireContext().getSharedPreferences("notifications_prefs", Context.MODE_PRIVATE)

val notifications = tasks.map {
Notifications(
id = it.id,
icon = R.drawable.task_pending,
text = "You were assigned a new task: " + "${it.title}"
)
}.filterNot {
sharedPrefs.getBoolean("notification_${it.id}", false)
}.toMutableList()

Log.d("Notifications", "Loaded notifications: $notifications")

val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view_notifications)
recyclerView.layoutManager = LinearLayoutManager(context)
notificationsAdapter = AdapterNotification(requireContext(), notifications, object : NotificationCallback {
Expand All @@ -56,7 +65,7 @@ class SeeAllNotificationsFragment : Fragment() {
override fun downloadDictionary() {}
override fun showTaskListDialog() {}
override fun syncKeyId() {}
}, showMarkAsReadButton = true, false)
}, showMarkAsReadButton = true, showImages = false)

recyclerView.adapter = notificationsAdapter

Expand Down

0 comments on commit 28b4ce5

Please sign in to comment.