From f98aac85d804178dd868c170bf038f6ea53d812c Mon Sep 17 00:00:00 2001 From: Jason Goveas <95233082+JustAGhost23@users.noreply.github.com> Date: Sun, 25 Feb 2024 14:20:21 +0530 Subject: [PATCH] Use CourseRequestHandler to handle API call to mark all notifications as read (#354) --- .../bphc/cms/fragments/MyCoursesFragment.kt | 24 ++----------------- .../bphc/cms/helper/CourseRequestHandler.java | 23 ++++++++++++++++++ 2 files changed, 25 insertions(+), 22 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 65d55025..792bd1ae 100644 --- a/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt +++ b/app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt @@ -27,7 +27,6 @@ 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 @@ -35,9 +34,6 @@ 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.* @@ -53,9 +49,6 @@ class MyCoursesFragment : Fragment() { 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()) { @@ -91,22 +84,9 @@ 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 { + courseRequestHandler = CourseRequestHandler() + courseRequestHandler.markAllNotificationsAsRead() val realm = Realm.getDefaultInstance() val courseDataHandler = CourseDataHandler(realm) courseDataHandler.markAllAsRead() diff --git a/app/src/main/java/crux/bphc/cms/helper/CourseRequestHandler.java b/app/src/main/java/crux/bphc/cms/helper/CourseRequestHandler.java index 8314028e..c0f43651 100644 --- a/app/src/main/java/crux/bphc/cms/helper/CourseRequestHandler.java +++ b/app/src/main/java/crux/bphc/cms/helper/CourseRequestHandler.java @@ -229,6 +229,29 @@ public void onFailure(@NotNull Call> call, @NotNull Throwabl }); } + public Boolean markAllNotificationsAsRead() throws IOException, RuntimeException { + Call notificationCall = moodleServices.markAllNotificationsAsRead(userAccount.getToken(), userAccount.getUserID()); + + try { + Response response = notificationCall.execute(); + + if (response.code() != 200) { // Moodle returns 200 for all API calls + HttpException e = new HttpException(response); + Log.e(TAG, "Response code not 200!", e); + throw e; + } + + if (response.body() == null) { + throw new RuntimeException("Response body is null"); + } + + return response.body(); + } catch (IOException e) { + Log.e(TAG, "IOException when marking all notifications as read", e); + throw e; + } + } + public List getForumDiscussions(int moduleId) { Call callForumData = moodleServices.getForumDiscussions(userAccount.getToken(), moduleId, 0, 0);