Skip to content

Commit 0ada3ce

Browse files
authored
Merge pull request #29 from it5prasoon/dev/read-msg-logs-from-db
added logic for reading message logs from roomDB
2 parents 257ef45 + d00f4d7 commit 0ada3ce

22 files changed

+520
-409
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,7 @@
9595
</intent-filter>
9696
</receiver>
9797

98-
<service
99-
android:name=".services.NotificationListener"
100-
android:label="Auto Reply Msg Log"
101-
android:exported="true"
102-
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
103-
<intent-filter>
104-
<action android:name="android.service.notification.NotificationListenerService"/>
105-
</intent-filter>
106-
</service>
107-
108-
<activity android:name="com.matrix.autoreply.ui.activity.MsgLogViewerActivity"/>
98+
<activity android:name="com.matrix.autoreply.ui.activity.logsViewer.MsgLogViewerActivity"/>
10999
</application>
110100

111101
</manifest>

app/src/main/java/com/matrix/autoreply/services/NotificationListener.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

app/src/main/java/com/matrix/autoreply/store/data/AppPackage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.room.Entity
55
import androidx.room.PrimaryKey
66

77
@Entity(tableName = "app_packages")
8-
class AppPackage(@field:ColumnInfo(name = "package_name") var packageName: String) {
8+
data class AppPackage(@field:ColumnInfo(name = "package_name") var packageName: String) {
99
@PrimaryKey(autoGenerate = true)
1010
var index = 0
1111
}

app/src/main/java/com/matrix/autoreply/store/repository/MessageLogsDao.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ interface MessageLogsDao {
2121
@get:Query("SELECT COUNT(id) FROM MESSAGE_LOGS")
2222
val numReplies: Long
2323

24+
// retrieve the distinct notif_title values from the message_logs
25+
@Query("SELECT DISTINCT notif_title FROM message_logs")
26+
fun getDistinctNotificationTitles(): List<String>
27+
28+
// retrieve the notif_message items for a given notif_title
29+
@Query("SELECT * FROM message_logs WHERE notif_title = :notifTitle")
30+
fun getMessageLogsWithTitle(notifTitle: String): List<MessageLog>
31+
2432
//https://stackoverflow.com/questions/11771580/deleting-android-sqlite-rows-older-than-x-days
2533
@Query("DELETE FROM message_logs WHERE notif_reply_time <= strftime('%s', datetime('now', '-30 days'));")
2634
fun purgeMessageLogs()

app/src/main/java/com/matrix/autoreply/ui/activity/MsgLogViewerActivity.kt

Lines changed: 0 additions & 133 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.matrix.autoreply.ui.activity.logsViewer
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
import androidx.recyclerview.widget.LinearLayoutManager
9+
import com.matrix.autoreply.R
10+
import com.matrix.autoreply.databinding.FragmentContactNameBinding
11+
import com.matrix.autoreply.store.database.MessageLogsDB
12+
import com.matrix.autoreply.ui.adapters.ContactNameAdapter
13+
14+
class ContactNameFragment : Fragment(), RefreshListener {
15+
16+
private var _binding: FragmentContactNameBinding? = null
17+
private val binding get() = _binding!!
18+
private val adapter: ContactNameAdapter by lazy {
19+
ContactNameAdapter { contactName ->
20+
// Handle the click event here
21+
// Create and show the MessageListFragment with the selected contact name
22+
val fragment = MessageListFragment.newInstance(contactName)
23+
parentFragmentManager.beginTransaction()
24+
.replace(R.id.fragment_container, fragment)
25+
.addToBackStack(null)
26+
.commit()
27+
}
28+
}
29+
private lateinit var contactNameList: List<String>
30+
31+
override fun onCreateView(
32+
inflater: LayoutInflater,
33+
container: ViewGroup?,
34+
savedInstanceState: Bundle?
35+
): View {
36+
_binding = FragmentContactNameBinding.inflate(inflater, container, false)
37+
return binding.root
38+
}
39+
40+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
41+
super.onViewCreated(view, savedInstanceState)
42+
43+
// Getting all notification titles i.e. WhatsApp user-name from room DB
44+
updateUserNameList()
45+
46+
// Set up RecyclerView to display notification titles
47+
binding.contactNameRv.adapter = adapter
48+
binding.contactNameRv.layoutManager = LinearLayoutManager(requireContext())
49+
binding.contactNameRv.setHasFixedSize(true)
50+
adapter.submitList(contactNameList)
51+
52+
// Set up SwipeRefreshLayout for refreshing the message log
53+
binding.swipeRefreshLayout.setOnRefreshListener {
54+
this.onRefresh()
55+
binding.swipeRefreshLayout.isRefreshing = false
56+
}
57+
}
58+
59+
private fun updateUserNameList() {
60+
// TODO: 1. To make all the calls using suspend function and coroutines to make database IO calls
61+
// TODO: 2. To use Dagger Dependency Injection to get singleton of DB
62+
contactNameList = MessageLogsDB.getInstance(requireContext())!!.logsDao()!!.getDistinctNotificationTitles()
63+
}
64+
65+
override fun onDestroyView() {
66+
super.onDestroyView()
67+
_binding = null
68+
}
69+
70+
// Refresh the message log RecyclerView with the updated contents of the file
71+
override fun onRefresh() {
72+
updateUserNameList()
73+
adapter.submitList(contactNameList)
74+
}
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.matrix.autoreply.ui.activity.logsViewer
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
import androidx.recyclerview.widget.LinearLayoutManager
9+
import com.matrix.autoreply.databinding.FragmentMessageListBinding
10+
import com.matrix.autoreply.store.database.MessageLogsDB
11+
import com.matrix.autoreply.ui.adapters.ContactMessageAdapter
12+
13+
class MessageListFragment : Fragment(), RefreshListener {
14+
15+
private var _binding: FragmentMessageListBinding? = null
16+
private val binding get() = _binding!!
17+
private lateinit var adapter: ContactMessageAdapter
18+
private lateinit var userMessageList: List<Pair<String?, Long>>
19+
20+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
21+
_binding = FragmentMessageListBinding.inflate(inflater, container, false)
22+
return binding.root
23+
}
24+
25+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
26+
adapter = ContactMessageAdapter()
27+
binding.messageListRecyclerView.adapter = adapter
28+
binding.messageListRecyclerView.layoutManager =
29+
LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, true)
30+
31+
// Call a method to fetch and set the list of messages based on the selected notification title
32+
val selectedTitle = arguments?.getString(ARG_SELECTED_TITLE)
33+
if (selectedTitle != null) {
34+
fetchAndSetMessages(selectedTitle)
35+
}
36+
}
37+
38+
private fun fetchAndSetMessages(contactName: String) {
39+
userMessageList = getMessageLogsWithTitle(contactName)
40+
adapter.submitList(userMessageList)
41+
}
42+
43+
private fun getMessageLogsWithTitle(contactName: String): List<Pair<String?, Long>> {
44+
// TODO: 1. To make all the calls using suspend function and coroutines to make database IO calls
45+
// TODO: 2. To use Dagger Dependency Injection to get singleton of DB
46+
val messageLogWithTitleList =
47+
MessageLogsDB.getInstance(requireContext())!!.logsDao()!!.getMessageLogsWithTitle(contactName)
48+
49+
return messageLogWithTitleList.map { messageLog ->
50+
val message = messageLog.notifMessage
51+
val timestamp = messageLog.notifArrivedTime
52+
53+
// Return the extracted items as a pair or any other desired structure
54+
Pair(message, timestamp)
55+
}.reversed()
56+
}
57+
58+
companion object {
59+
private const val ARG_SELECTED_TITLE = "selected_title"
60+
61+
fun newInstance(selectedTitle: String): MessageListFragment {
62+
val fragment = MessageListFragment()
63+
val args = Bundle().apply {
64+
putString(ARG_SELECTED_TITLE, selectedTitle)
65+
}
66+
fragment.arguments = args
67+
return fragment
68+
}
69+
}
70+
71+
override fun onRefresh() {
72+
userMessageList = getMessageLogsWithTitle(arguments?.getString(ARG_SELECTED_TITLE)!!)
73+
adapter.submitList(userMessageList)
74+
}
75+
}

0 commit comments

Comments
 (0)