From e1f7018b1902b86188fd00e2173b6466adfd4598 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 12 Oct 2022 15:00:46 +0200 Subject: [PATCH] WIP bookmark buttons --- .../pixeldroid/app/posts/StatusViewHolder.kt | 59 +++++++++++++++++++ .../pixeldroid/app/utils/api/PixelfedAPI.kt | 11 ++++ app/src/main/res/menu/post_more_menu.xml | 1 - app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt index d23e7f7f..ae7a7d54 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt @@ -317,6 +317,48 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold } } + private suspend fun bookmarkPost(api: PixelfedAPI) { + //Call the api function + status?.id?.let { id -> + try { + api.bookmarkStatus(id) + } catch (exception: HttpException) { + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.bookmark_post_failed_error, exception.code()), + Toast.LENGTH_SHORT + ).show() + } catch (exception: IOException) { + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.bookmark_post_failed_io_except), + Toast.LENGTH_SHORT + ).show() + } + } + } + + private suspend fun undoBookmarkPost(api: PixelfedAPI) { + //Call the api function + status?.id?.let { id -> + try { + api.undoBookmarkStatus(id) + } catch (exception: HttpException) { + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.undobookmark_post_failed_error, exception.code()), + Toast.LENGTH_SHORT + ).show() + } catch (exception: IOException) { + Toast.makeText( + binding.root.context, + binding.root.context.getString(R.string.undobookmark_post_failed_io_except), + Toast.LENGTH_SHORT + ).show() + } + } + } + private fun activateMoreButton(apiHolder: PixelfedAPIHolder, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope){ binding.statusMore.setOnClickListener { PopupMenu(it.context, it).apply { @@ -341,6 +383,18 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold true } + R.id.post_more_menu_bookmark -> { + lifecycleScope.launch { + bookmarkPost(apiHolder.api ?: apiHolder.setToCurrentUser()) + } + true + } + R.id.post_more_menu_unbookmark -> { + lifecycleScope.launch { + undoBookmarkPost(apiHolder.api ?: apiHolder.setToCurrentUser()) + } + true + } R.id.post_more_menu_save_to_gallery -> { Dexter.withContext(binding.root.context) .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) @@ -429,6 +483,11 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold } } inflate(R.menu.post_more_menu) + if(status?.bookmarked == true) { + menu.setGroupVisible(R.id.post_more_menu_group_bookmark, false) + } else { + menu.setGroupVisible(R.id.post_more_menu_group_unbookmark, false) + } if(status?.media_attachments.isNullOrEmpty()) { //make sure to disable image-related things if there aren't any menu.setGroupVisible(R.id.post_more_group_picture, false) diff --git a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt index 08e46124..50417531 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/api/PixelfedAPI.kt @@ -183,6 +183,17 @@ interface PixelfedAPI { @Path("id") statusId: String, ) : Status + @POST("/api/v1/statuses/{id}/bookmark") + suspend fun bookmarkStatus( + @Path("id") statusId: String + ) : Status + + @POST("/api/v1/statuses/{id}/undobookmark") + suspend fun undoBookmarkStatus( + @Path("id") statusId: String + ) : Status + + //Used in our case to retrieve comments for a given status @GET("/api/v1/statuses/{id}/context") suspend fun statusComments( diff --git a/app/src/main/res/menu/post_more_menu.xml b/app/src/main/res/menu/post_more_menu.xml index 5db626ba..b46bf28c 100644 --- a/app/src/main/res/menu/post_more_menu.xml +++ b/app/src/main/res/menu/post_more_menu.xml @@ -19,7 +19,6 @@ android:title="@string/unbookmark" /> - Image showing a red panda, Pixelfed\'s mascot, using a phone Could not delete the post, error %1$d Could not delete the post, check your connection? + Could not bookmark the post, error %1$d + Could not bookmark the post, check your connection? + Could not unbookmark the post, error %1$d + Could not unbookmark the post, check your connection? File %1$s was not found