From 6aabee5e63ba082e921a09e6e1c492ce6ef55f14 Mon Sep 17 00:00:00 2001 From: JustAGhost23 Date: Sun, 25 Feb 2024 01:16:40 +0530 Subject: [PATCH] Add API call to mark notifications as read --- .../bphc/cms/fragments/MyCoursesFragment.kt | 25 +++++++++++++++++++ .../crux/bphc/cms/network/MoodleServices.java | 12 +++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt b/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt index f32a5f2f..65d55025 100644 --- a/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt +++ b/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt @@ -27,11 +27,17 @@ import crux.bphc.cms.helper.CourseDataHandler import crux.bphc.cms.helper.CourseDownloader import crux.bphc.cms.helper.CourseRequestHandler import crux.bphc.cms.interfaces.ClickListener +import crux.bphc.cms.models.UserAccount import crux.bphc.cms.models.course.Course import crux.bphc.cms.models.course.CourseSection +import crux.bphc.cms.network.APIClient +import crux.bphc.cms.network.MoodleServices import crux.bphc.cms.utils.UserUtils import io.realm.Realm import kotlinx.coroutines.* +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response import java.io.IOException import java.text.DecimalFormat import java.util.* @@ -40,12 +46,16 @@ import kotlin.collections.ArrayList class MyCoursesFragment : Fragment() { private lateinit var realm: Realm private lateinit var courseDataHandler: CourseDataHandler + private lateinit var courseRequestHandler: CourseRequestHandler private lateinit var binding: FragmentMyCoursesBinding private var coursesUpdated = 0 private var courses: MutableList = ArrayList() private lateinit var mAdapter: Adapter + private val moodleServices: MoodleServices = APIClient.getRetrofitInstance().create( + MoodleServices::class.java) + // Activity result launchers private val courseDetailActivityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { @@ -81,6 +91,21 @@ class MyCoursesFragment : Fragment() { override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.mark_all_as_read -> { + courseRequestHandler = CourseRequestHandler() + val call = moodleServices.markAllNotificationsAsRead( + UserAccount.token, + UserAccount.userID + ) + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + Log.i(TAG, "Marked all as read on website") + } + } + override fun onFailure(call: Call, t: Throwable) { + Log.wtf(TAG, t) + } + }) CoroutineScope(Dispatchers.Default).launch { val realm = Realm.getDefaultInstance() val courseDataHandler = CourseDataHandler(realm) diff --git a/app/src/main/java/crux/bphc/cms/network/MoodleServices.java b/app/src/main/java/crux/bphc/cms/network/MoodleServices.java index a7aebce6..702e2c68 100644 --- a/app/src/main/java/crux/bphc/cms/network/MoodleServices.java +++ b/app/src/main/java/crux/bphc/cms/network/MoodleServices.java @@ -1,9 +1,7 @@ package crux.bphc.cms.network; import org.jetbrains.annotations.NotNull; - import java.util.List; - import crux.bphc.cms.models.core.UserDetail; import okhttp3.ResponseBody; import retrofit2.Call; @@ -93,6 +91,16 @@ Call getForumDiscussions(@Query("wstoken") String token, @Query("page") int page, @Query("perpage") int perpage); + /** + * Mark all notifications as read for a user + * + * @param token A valid Moodle Web Service token + * @param userid Id of the user whose number of unread notifications are needed + */ + + @GET("webservice/rest/server.php?wsfunction=core_message_mark_all_notifications_as_read&moodlewsrestformat=json") + Call markAllNotificationsAsRead(@Query("wstoken") String token, @Query("useridto") int userid); + @GET("webservice/rest/server.php?wsfunction=core_user_add_user_device&moodlewsrestformat=json") Call registerUserDevice(@Query("wstoken") @NotNull String token, @Query("appid") @NotNull String appid,