Skip to content

Commit

Permalink
Add API call to mark notifications as read
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAGhost23 committed Feb 24, 2024
1 parent 59dad7d commit 6aabee5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/crux/bphc/cms/fragments/MyCoursesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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<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 @@ -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<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 {
val realm = Realm.getDefaultInstance()
val courseDataHandler = CourseDataHandler(realm)
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/crux/bphc/cms/network/MoodleServices.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -93,6 +91,16 @@ Call<ForumData> 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<Boolean> markAllNotificationsAsRead(@Query("wstoken") String token, @Query("useridto") int userid);

@GET("webservice/rest/server.php?wsfunction=core_user_add_user_device&moodlewsrestformat=json")
Call<ResponseBody> registerUserDevice(@Query("wstoken") @NotNull String token,
@Query("appid") @NotNull String appid,
Expand Down

0 comments on commit 6aabee5

Please sign in to comment.