Skip to content

Commit

Permalink
Use CourseRequestHandler to handle API call to mark all notifications…
Browse files Browse the repository at this point in the history
… as read (#354)
  • Loading branch information
JustAGhost23 authored Feb 25, 2024
1 parent b849ebf commit f98aac8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
24 changes: 2 additions & 22 deletions app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ 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.*
Expand All @@ -53,9 +49,6 @@ class MyCoursesFragment : Fragment() {
private var courses: MutableList<Course> = 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()) {
Expand Down Expand Up @@ -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<Boolean> {
override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
if (response.isSuccessful) {
Log.i(TAG, "Marked all as read on website")
}
}
override fun onFailure(call: Call<Boolean>, t: Throwable) {
Log.wtf(TAG, t)
}
})
CoroutineScope(Dispatchers.Default).launch {
courseRequestHandler = CourseRequestHandler()
courseRequestHandler.markAllNotificationsAsRead()
val realm = Realm.getDefaultInstance()
val courseDataHandler = CourseDataHandler(realm)
courseDataHandler.markAllAsRead()
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/crux/bphc/cms/helper/CourseRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ public void onFailure(@NotNull Call<List<CourseSection>> call, @NotNull Throwabl
});
}

public Boolean markAllNotificationsAsRead() throws IOException, RuntimeException {
Call<Boolean> notificationCall = moodleServices.markAllNotificationsAsRead(userAccount.getToken(), userAccount.getUserID());

try {
Response<Boolean> 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<Discussion> getForumDiscussions(int moduleId) {
Call<ForumData> callForumData = moodleServices.getForumDiscussions(userAccount.getToken(), moduleId, 0, 0);
Expand Down

0 comments on commit f98aac8

Please sign in to comment.