mirror of
https://github.com/rebelonion/Dantotsu.git
synced 2026-01-30 10:11:02 +00:00
feat(social): like button fix
This commit is contained in:
@@ -3,10 +3,8 @@ package ani.dantotsu.home.status
|
||||
import android.animation.Animator
|
||||
import android.animation.ObjectAnimator
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Rect
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.MotionEvent
|
||||
@@ -21,17 +19,17 @@ import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.constraintlayout.widget.ConstraintSet
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.webkit.internal.ApiFeature.T
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.blurImage
|
||||
import ani.dantotsu.connections.anilist.Anilist
|
||||
import ani.dantotsu.home.status.data.StoryItem
|
||||
import ani.dantotsu.connections.anilist.api.Activity
|
||||
import ani.dantotsu.home.status.listener.StoriesCallback
|
||||
import ani.dantotsu.loadImage
|
||||
import ani.dantotsu.media.MediaDetailsActivity
|
||||
import ani.dantotsu.profile.ProfileActivity
|
||||
import ani.dantotsu.profile.User
|
||||
import ani.dantotsu.profile.UsersDialogFragment
|
||||
import ani.dantotsu.profile.activity.ActivityItemBuilder
|
||||
import ani.dantotsu.snackString
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -39,14 +37,14 @@ import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
|
||||
|
||||
class Stories @JvmOverloads
|
||||
constructor(
|
||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
|
||||
) : ConstraintLayout(context, attrs, defStyleAttr), View.OnTouchListener {
|
||||
|
||||
private lateinit var storiesList: List<StoryItem>
|
||||
private lateinit var activityList: List<Activity>
|
||||
private lateinit var activ: FragmentActivity
|
||||
private lateinit var loadingViewLayout: ConstraintLayout
|
||||
private lateinit var leftTouchPanel: FrameLayout
|
||||
@@ -56,6 +54,7 @@ constructor(
|
||||
private lateinit var loadingView: ProgressBar
|
||||
private lateinit var activityLikeCount: TextView
|
||||
private lateinit var activityLike: ImageView
|
||||
private lateinit var activityLikeContainer: LinearLayout
|
||||
private lateinit var userName: TextView
|
||||
private lateinit var userAvatar: ImageView
|
||||
private lateinit var time: TextView
|
||||
@@ -66,14 +65,12 @@ constructor(
|
||||
private var storyIndex: Int = 1
|
||||
private var userClicked: Boolean = false
|
||||
private lateinit var storiesListener: StoriesCallback
|
||||
private var oldStoryItem = StoryItem()
|
||||
|
||||
init {
|
||||
initLayout()
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private fun initLayout() {
|
||||
fun initLayout() {
|
||||
val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||
val view = inflater.inflate(R.layout.fragment_status, this, false)
|
||||
addView(view)
|
||||
@@ -95,6 +92,7 @@ constructor(
|
||||
infoText = findViewById(R.id.infoText)
|
||||
activityLikeCount = findViewById(R.id.activityLikeCount)
|
||||
activityLike = findViewById(R.id.activityLike)
|
||||
activityLikeContainer = findViewById(R.id.statusUserActions)
|
||||
|
||||
leftTouchPanel.setOnTouchListener(this)
|
||||
rightTouchPanel.setOnTouchListener(this)
|
||||
@@ -102,15 +100,16 @@ constructor(
|
||||
}
|
||||
|
||||
|
||||
fun setStoriesList(storiesList: List<StoryItem>, activity: FragmentActivity) {
|
||||
this.storiesList = storiesList
|
||||
fun setStoriesList(activityList: List<Activity>, activity: FragmentActivity) {
|
||||
this.activityList = activityList
|
||||
this.activ = activity
|
||||
addLoadingViews(storiesList)
|
||||
addLoadingViews(activityList)
|
||||
}
|
||||
|
||||
private fun addLoadingViews(storiesList: List<StoryItem>) {
|
||||
private fun addLoadingViews(storiesList: List<Activity>) {
|
||||
var idCounter = 1
|
||||
for (story in storiesList) {
|
||||
loadingViewLayout.removeView(findViewWithTag<ProgressBar>("story${idCounter}"))
|
||||
val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal)
|
||||
progressBar.visibility = View.VISIBLE
|
||||
progressBar.id = idCounter
|
||||
@@ -231,15 +230,14 @@ constructor(
|
||||
animation.duration = secondsToMillis(storyDuration)
|
||||
animation.interpolator = LinearInterpolator()
|
||||
animation.addListener(object : Animator.AnimatorListener {
|
||||
override fun onAnimationStart(animator: Animator) {
|
||||
}
|
||||
override fun onAnimationStart(animator: Animator) {}
|
||||
|
||||
override fun onAnimationEnd(animator: Animator) {
|
||||
if (storyIndex - 1 <= storiesList.size) {
|
||||
if (storyIndex - 1 <= activityList.size) {
|
||||
if (userClicked) {
|
||||
userClicked = false
|
||||
} else {
|
||||
if (storyIndex < storiesList.size) {
|
||||
if (storyIndex < activityList.size) {
|
||||
storyIndex += 1
|
||||
showStory()
|
||||
} else {
|
||||
@@ -261,7 +259,7 @@ constructor(
|
||||
|
||||
override fun onAnimationRepeat(animator: Animator) {}
|
||||
})
|
||||
loadStory(storiesList[storyIndex - 1])
|
||||
loadStory(activityList[storyIndex - 1])
|
||||
}
|
||||
|
||||
private fun getId(tag: String): Int {
|
||||
@@ -321,19 +319,23 @@ constructor(
|
||||
}
|
||||
|
||||
private fun rightPanelTouch() {
|
||||
if (storyIndex == storiesList.size) {
|
||||
if (storyIndex == activityList.size) {
|
||||
completeProgressBar(storyIndex)
|
||||
onStoriesCompleted()
|
||||
return
|
||||
}
|
||||
userClicked = true
|
||||
animation.end()
|
||||
if (storyIndex < storiesList.size)
|
||||
if (storyIndex < activityList.size)
|
||||
storyIndex += 1
|
||||
showStory()
|
||||
}
|
||||
|
||||
private fun leftPanelTouch() {
|
||||
if (storyIndex == 1) {
|
||||
onStoriesPrevious()
|
||||
return
|
||||
}
|
||||
userClicked = true
|
||||
animation.end()
|
||||
resetProgressBar(storyIndex)
|
||||
@@ -343,8 +345,19 @@ constructor(
|
||||
}
|
||||
|
||||
private fun onStoriesCompleted() {
|
||||
if (::storiesListener.isInitialized)
|
||||
if (::storiesListener.isInitialized){
|
||||
storyIndex = 1
|
||||
storiesListener.onStoriesEnd()
|
||||
resetProgressBar(storyIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onStoriesPrevious() {
|
||||
if (::storiesListener.isInitialized) {
|
||||
storyIndex = 1
|
||||
storiesListener.onStoriesStart()
|
||||
resetProgressBar(storyIndex)
|
||||
}
|
||||
}
|
||||
fun pause() {
|
||||
animation.pause()
|
||||
@@ -352,40 +365,54 @@ constructor(
|
||||
fun resume() {
|
||||
animation.resume()
|
||||
}
|
||||
private fun loadStory(story: StoryItem) {
|
||||
private fun loadStory(story: Activity) {
|
||||
loadingView.visibility = View.GONE
|
||||
animation.start()
|
||||
blurImage(imageContentView, story.banner)
|
||||
userAvatar.loadImage(story.userAvatar)
|
||||
coverImage.loadImage(story.cover)
|
||||
userName.text = story.userName
|
||||
time.text = story.time
|
||||
infoText.text = story.info
|
||||
blurImage(imageContentView, story.media?.bannerImage ?: story.media?.coverImage?.extraLarge)
|
||||
userAvatar.loadImage(story.user?.avatar?.large)
|
||||
coverImage.loadImage(story.media?.coverImage?.extraLarge)
|
||||
userName.text = story.user?.name
|
||||
time.text = ActivityItemBuilder.getDateTime(story.createdAt)
|
||||
val text = "${story.status?.replaceFirstChar {
|
||||
if (it.isLowerCase()) {
|
||||
it.titlecase(Locale.ROOT)
|
||||
}
|
||||
else {
|
||||
it.toString()
|
||||
}
|
||||
}} ${story.progress ?: story.media?.title?.userPreferred}"
|
||||
infoText.text = text
|
||||
|
||||
statusUserContainer.setOnClickListener {
|
||||
ContextCompat.startActivity(context, Intent(context, ProfileActivity::class.java).putExtra("userId", story.id), null)
|
||||
ContextCompat.startActivity(context, Intent(context, ProfileActivity::class.java)
|
||||
.putExtra("userId", story.userId),
|
||||
null)
|
||||
}
|
||||
|
||||
coverImage.setOnClickListener{
|
||||
ContextCompat.startActivity(context, Intent(context, MediaDetailsActivity::class.java).putExtra("mediaId", story.mediaId), null)
|
||||
ContextCompat.startActivity(context, Intent(context, MediaDetailsActivity::class.java)
|
||||
.putExtra("mediaId", story.media?.id),
|
||||
null)
|
||||
}
|
||||
|
||||
val likeColor = ContextCompat.getColor(context, R.color.yt_red)
|
||||
val notLikeColor = ContextCompat.getColor(context, R.color.bg_opp)
|
||||
activityLikeCount.text = story.likes.toString()
|
||||
activityLike.setColorFilter(if (story.isLiked) likeColor else notLikeColor)
|
||||
activityLike.setOnClickListener {
|
||||
activityLikeCount.text = story.likeCount.toString()
|
||||
activityLike.setColorFilter(if (story.isLiked == true) likeColor else notLikeColor)
|
||||
activityLikeContainer.setOnClickListener {
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
scope.launch {
|
||||
val res = Anilist.query.toggleLike(story.activityId!!, "ACTIVITY")
|
||||
val res = Anilist.query.toggleLike(story.id, "ACTIVITY")
|
||||
withContext(Dispatchers.Main) {
|
||||
if (res != null) {
|
||||
|
||||
if (story.isLiked) {
|
||||
story.likes = story.likes.minus(1)
|
||||
if (story.isLiked == true) {
|
||||
story.likeCount = story.likeCount?.minus(1)
|
||||
} else {
|
||||
story.likes = story.likes.plus(1)
|
||||
story.likeCount = story.likeCount?.plus(1)
|
||||
}
|
||||
activityLikeCount.text = (story.likes ?: 0).toString()
|
||||
story.isLiked = !story.isLiked
|
||||
activityLike.setColorFilter(if (story.isLiked) likeColor else notLikeColor)
|
||||
activityLikeCount.text = (story.likeCount ?: 0).toString()
|
||||
story.isLiked = !story.isLiked!!
|
||||
activityLike.setColorFilter(if (story.isLiked == true) likeColor else notLikeColor)
|
||||
|
||||
} else {
|
||||
snackString("Failed to like activity")
|
||||
@@ -393,21 +420,17 @@ constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val userList = arrayListOf<User>()
|
||||
story.likedBy?.forEach { i ->
|
||||
story.likes?.forEach { i ->
|
||||
userList.add(User(i.id, i.name.toString(), i.avatar?.medium, i.bannerImage))
|
||||
}
|
||||
|
||||
activityLike.setOnLongClickListener {
|
||||
activityLikeContainer.setOnLongClickListener {
|
||||
UsersDialogFragment().apply {
|
||||
userList(userList)
|
||||
show(activ.supportFragmentManager, "dialog")
|
||||
}
|
||||
true
|
||||
}
|
||||
oldStoryItem = story
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package ani.dantotsu.home.status.data
|
||||
|
||||
import ani.dantotsu.connections.anilist.api.User
|
||||
|
||||
data class StoryItem(
|
||||
val id : Int? = null,
|
||||
val activityId : Int? = null,
|
||||
val mediaId: Int? = null,
|
||||
val userName: String? = null,
|
||||
val userAvatar: String? = null,
|
||||
val time: String? = null,
|
||||
val info: String? = null,
|
||||
val cover: String? = null,
|
||||
val banner: String? = null,
|
||||
var likes : Int = 0,
|
||||
val likedBy: List<User>? = null,
|
||||
var isLiked : Boolean = false
|
||||
)
|
||||
|
||||
|
||||
@@ -3,4 +3,5 @@ package ani.dantotsu.home.status.listener
|
||||
interface StoriesCallback {
|
||||
|
||||
fun onStoriesEnd()
|
||||
fun onStoriesStart()
|
||||
}
|
||||
Reference in New Issue
Block a user