Skip to content

Commit

Permalink
feat: comment placement | tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Feb 26, 2024
1 parent 8a922bd commit 7f943d3
Show file tree
Hide file tree
Showing 20 changed files with 548 additions and 293 deletions.
6 changes: 2 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@
<activity
android:name=".others.imagesearch.ImageSearchActivity"
android:parentActivityName=".MainActivity" />
<activity
android:name=".media.comments.CommentsActivity"
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name=".media.SearchActivity"
android:parentActivityName=".MainActivity" />
Expand All @@ -130,7 +127,8 @@
<activity
android:name=".media.MediaDetailsActivity"
android:parentActivityName=".MainActivity"
android:theme="@style/Theme.Dantotsu.NeverCutout" />
android:theme="@style/Theme.Dantotsu.NeverCutout"
android:windowSoftInputMode="adjustPan|stateHidden"/>
<activity android:name=".media.CharacterDetailsActivity" />
<activity android:name=".home.NoInternet" />
<activity
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/ani/dantotsu/connections/comments/CommentsAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ object CommentsAPI {
var isMod: Boolean = false
var totalVotes: Int = 0

suspend fun getCommentsForId(id: Int, page: Int = 1): CommentResponse? {
val url = "$address/comments/$id/$page"
suspend fun getCommentsForId(id: Int, page: Int = 1, tag: Int?): CommentResponse? {
var url = "$address/comments/$id/$page"
val request = requestBuilder()
tag?.let {
url += "?tag=$it"
}
val json = try {
request.get(url)
} catch (e: IOException) {
Expand Down Expand Up @@ -90,12 +93,15 @@ object CommentsAPI {
return res
}

suspend fun comment(mediaId: Int, parentCommentId: Int?, content: String): Comment? {
suspend fun comment(mediaId: Int, parentCommentId: Int?, content: String, tag: Int?): Comment? {
val url = "$address/comments"
val body = FormBody.Builder()
.add("user_id", userId ?: return null)
.add("media_id", mediaId.toString())
.add("content", content)
if (tag != null) {
body.add("tag", tag.toString())
}
parentCommentId?.let {
body.add("parent_comment_id", it.toString())
}
Expand Down Expand Up @@ -125,6 +131,7 @@ object CommentsAPI {
parsed.content,
parsed.timestamp,
parsed.deleted,
parsed.tag,
0,
0,
null,
Expand Down Expand Up @@ -224,7 +231,7 @@ object CommentsAPI {
}
val url = "$address/authenticate"
val token = PrefManager.getVal(PrefName.AnilistToken, null as String?) ?: return
repeat(MAX_RETRIES) { // Define MAX_RETRIES as a constant
repeat(MAX_RETRIES) {
try {
val json = authRequest(token, url)
if (json.code == 200) {
Expand Down Expand Up @@ -252,7 +259,6 @@ object CommentsAPI {
snackString("Failed to login to comments API")
return
}
// Wait for 1 minute before retrying
kotlinx.coroutines.delay(60000)
}
snackString("Failed to login after multiple attempts")
Expand Down Expand Up @@ -367,6 +373,8 @@ data class Comment(
@SerialName("deleted")
@Serializable(with = NumericBooleanSerializer::class)
val deleted: Boolean?,
@SerialName("tag")
val tag: Int?,
@SerialName("upvotes")
var upvotes: Int,
@SerialName("downvotes")
Expand Down Expand Up @@ -408,6 +416,8 @@ data class ReturnedComment(
@SerialName("deleted")
@Serializable(with = NumericBooleanSerializer::class)
val deleted: Boolean?,
@SerialName("tag")
val tag: Int?,
)

object NumericBooleanSerializer : KSerializer<Boolean> {
Expand Down
27 changes: 22 additions & 5 deletions app/src/main/java/ani/dantotsu/media/MediaDetailsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import ani.dantotsu.databinding.ActivityMediaBinding
import ani.dantotsu.initActivity
import ani.dantotsu.loadImage
import ani.dantotsu.media.anime.AnimeWatchFragment
import ani.dantotsu.media.comments.CommentsFragment
import ani.dantotsu.media.manga.MangaReadFragment
import ani.dantotsu.media.novel.NovelReadFragment
import ani.dantotsu.navBarHeight
Expand Down Expand Up @@ -318,13 +319,14 @@ class MediaDetailsActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedLi
tabLayout.menu.clear()
if (media.anime != null) {
viewPager.adapter =
ViewPagerAdapter(supportFragmentManager, lifecycle, SupportedMedia.ANIME)
ViewPagerAdapter(supportFragmentManager, lifecycle, SupportedMedia.ANIME, media)
tabLayout.inflateMenu(R.menu.anime_menu_detail)
} else if (media.manga != null) {
viewPager.adapter = ViewPagerAdapter(
supportFragmentManager,
lifecycle,
if (media.format == "NOVEL") SupportedMedia.NOVEL else SupportedMedia.MANGA
if (media.format == "NOVEL") SupportedMedia.NOVEL else SupportedMedia.MANGA,
media
)
if (media.format == "NOVEL") {
tabLayout.inflateMenu(R.menu.novel_menu_detail)
Expand Down Expand Up @@ -378,17 +380,23 @@ class MediaDetailsActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedLi
R.id.watch, R.id.read -> {
selected = 1
}

R.id.comment -> {
selected = 2
}
}
}

private fun idFromSelect(): Int {
if (anime) when (selected) {
0 -> return R.id.info
1 -> return R.id.watch
2 -> return R.id.comment
}
else when (selected) {
0 -> return R.id.info
1 -> return R.id.read
2 -> return R.id.comment
}
return R.id.info
}
Expand All @@ -408,19 +416,28 @@ class MediaDetailsActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedLi
private class ViewPagerAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle,
private val media: SupportedMedia
private val mediaType: SupportedMedia,
private val media: Media
) :
FragmentStateAdapter(fragmentManager, lifecycle) {

override fun getItemCount(): Int = 2
override fun getItemCount(): Int = 3

override fun createFragment(position: Int): Fragment = when (position) {
0 -> MediaInfoFragment()
1 -> when (media) {
1 -> when (mediaType) {
SupportedMedia.ANIME -> AnimeWatchFragment()
SupportedMedia.MANGA -> MangaReadFragment()
SupportedMedia.NOVEL -> NovelReadFragment()
}
2 -> {
val fragment = CommentsFragment()
val bundle = Bundle()
bundle.putInt("mediaId", media.id)
bundle.putString("mediaName", media.mainName())
fragment.arguments = bundle
fragment
}

else -> MediaInfoFragment()
}
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import ani.dantotsu.connections.comments.CommentsAPI
import ani.dantotsu.databinding.DialogLayoutBinding
import ani.dantotsu.databinding.ItemAnimeWatchBinding
import ani.dantotsu.databinding.ItemChipBinding
import ani.dantotsu.media.comments.CommentsActivity
import ani.dantotsu.media.Media
import ani.dantotsu.media.MediaDetailsActivity
import ani.dantotsu.media.SourceSearchDialogFragment
Expand Down Expand Up @@ -60,17 +59,6 @@ class AnimeWatchAdapter(
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.binding
_binding = binding
//CommentsAPI
binding.animeComments.visibility = if (CommentsAPI.userId == null) View.GONE else View.VISIBLE
binding.animeComments.setOnClickListener {
startActivity(
fragment.requireContext(),
Intent(fragment.requireContext(), CommentsActivity::class.java)
.putExtra("mediaId", media.id)
.putExtra("mediaName", media.mainName()),
null
)
}

binding.faqbutton.setOnClickListener {
startActivity(
Expand Down
22 changes: 13 additions & 9 deletions app/src/main/java/ani/dantotsu/media/comments/CommentItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import ani.dantotsu.currActivity
import ani.dantotsu.databinding.ItemCommentsBinding
import ani.dantotsu.loadImage
import ani.dantotsu.openLinkInBrowser
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString
import com.xwray.groupie.GroupieAdapter
import com.xwray.groupie.Section
Expand All @@ -33,7 +31,7 @@ import kotlin.math.sqrt
class CommentItem(val comment: Comment,
private val markwon: Markwon,
val parentSection: Section,
private val commentsActivity: CommentsActivity,
private val commentsFragment: CommentsFragment,
private val backgroundColor: Int,
val commentDepth: Int
) : BindableItem<ItemCommentsBinding>() {
Expand Down Expand Up @@ -62,6 +60,12 @@ class CommentItem(val comment: Comment,
viewBinding.commentDelete.visibility = if (isUserComment || CommentsAPI.isAdmin || CommentsAPI.isMod) View.VISIBLE else View.GONE
viewBinding.commentBanUser.visibility = if ((CommentsAPI.isAdmin || CommentsAPI.isMod) && !isUserComment) View.VISIBLE else View.GONE
viewBinding.commentEdit.visibility = if (isUserComment) View.VISIBLE else View.GONE
if (comment.tag == null) {
viewBinding.commentUserTagLayout.visibility = View.GONE
} else {
viewBinding.commentUserTagLayout.visibility = View.VISIBLE
viewBinding.commentUserTag.text = comment.tag.toString()
}
replying(isReplying) //sets default text
editing(isEditing)
if ((comment.replyCount ?: 0) > 0) {
Expand All @@ -83,7 +87,7 @@ class CommentItem(val comment: Comment,
} else {
viewBinding.commentTotalReplies.text = "Hide Replies"
repliesSection.clear()
commentsActivity.viewReplyCallback(this)
commentsFragment.viewReplyCallback(this)
repliesVisible = true
}
}
Expand All @@ -98,12 +102,12 @@ class CommentItem(val comment: Comment,

viewBinding.commentEdit.setOnClickListener {
editing(!isEditing)
commentsActivity.editCallback(this)
commentsFragment.editCallback(this)
}
viewBinding.commentReply.setOnClickListener {
replying(!isReplying)
commentsActivity.replyTo(this, comment.username)
commentsActivity.replyCallback(this)
commentsFragment.replyTo(this, comment.username)
commentsFragment.replyCallback(this)
}
viewBinding.modBadge.visibility = if (comment.isMod == true) View.VISIBLE else View.GONE
viewBinding.adminBadge.visibility = if (comment.isAdmin == true) View.VISIBLE else View.GONE
Expand Down Expand Up @@ -134,7 +138,7 @@ class CommentItem(val comment: Comment,
dialogBuilder("Report Comment", "Only report comments that violate the rules. Are you sure you want to report this comment?") {
val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
scope.launch {
val success = CommentsAPI.reportComment(comment.commentId, comment.username, commentsActivity.mediaName)
val success = CommentsAPI.reportComment(comment.commentId, comment.username, commentsFragment.mediaName)
if (success) {
snackString("Comment Reported")
}
Expand Down Expand Up @@ -347,7 +351,7 @@ class CommentItem(val comment: Comment,
* @param callback the callback to call when the user clicks yes
*/
private fun dialogBuilder(title: String, message: String, callback: () -> Unit) {
val alertDialog = android.app.AlertDialog.Builder(commentsActivity, R.style.MyPopup)
val alertDialog = android.app.AlertDialog.Builder(commentsFragment.activity, R.style.MyPopup)
.setTitle(title)
.setMessage(message)
.setPositiveButton("Yes") { dialog, _ ->
Expand Down
Loading

0 comments on commit 7f943d3

Please sign in to comment.