Created
October 9, 2019 01:47
-
-
Save yudikarma/c4b2de4614f1cd0b36bd93a86b5408c1 to your computer and use it in GitHub Desktop.
implement kohii in recycleerview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:background="@color/grey_10" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<com.google.android.material.card.MaterialCardView | |
android:id="@+id/card_view_top" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:cardCornerRadius="8dp" | |
app:cardElevation="4dp" | |
app:cardMaxElevation="6dp" | |
app:strokeWidth="4dp" | |
app:cardBackgroundColor="@color/white" | |
tools:ignore="MissingConstraints" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentBottom="true" | |
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" | |
android:layout_marginStart="4dp" android:layout_marginBottom="6dp" | |
android:layout_marginEnd="4dp" app:layout_constraintHorizontal_bias="1.0" | |
app:layout_constraintVertical_bias="1.0" android:layout_marginTop="1dp"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:id="@+id/container" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:layout_width="32dp" | |
android:src="@drawable/avatar_profil2" | |
app:civ_border_color="@color/white" | |
app:civ_border_width="1dp" | |
app:civ_circle_background_color="@color/black" | |
android:layout_height="32dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginTop="8dp" | |
android:layout_marginStart="16dp" | |
android:id="@+id/feed_imageProfil"/> | |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" | |
android:id="@+id/feed_profilName" | |
android:textAppearance="@style/TextAppearance.AppCompat.Medium" | |
android:textColor="@color/grey_900" | |
android:layout_marginStart="8dp" | |
app:layout_constraintStart_toEndOf="@id/feed_imageProfil" | |
android:text="@string/rainy_bunny" app:layout_constraintTop_toTopOf="parent" | |
android:layout_marginTop="8dp"/> | |
<TextView android:layout_width="wrap_content" | |
android:id="@+id/feed_time" | |
android:textColor="@color/grey_round" | |
android:text="@string/_2_minute_ago" | |
android:textSize="12sp" | |
android:layout_height="wrap_content" android:layout_marginTop="1dp" | |
app:layout_constraintTop_toBottomOf="@+id/feed_profilName" | |
app:layout_constraintStart_toEndOf="@+id/feed_imageProfil" | |
android:layout_marginStart="8dp" | |
app:layout_constraintBottom_toBottomOf="@+id/feed_imageProfil"/> | |
<ImageView android:layout_width="32dp" android:layout_height="32dp" | |
android:id="@+id/feed_icon_report" | |
app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginEnd="8dp" | |
app:layout_constraintTop_toTopOf="parent" | |
android:layout_marginTop="8dp" | |
android:src="@drawable/ic_expand_more_black_24dp"/> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
app:layout_constraintDimensionRatio="H,1:1" | |
android:adjustViewBounds="true" | |
android:gravity="center" | |
android:id="@+id/container_video" | |
android:scaleType="center" | |
android:background="@color/black" | |
app:layout_constraintTop_toBottomOf="@+id/feed_imageProfil" | |
android:layout_marginTop="8dp" app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="4dp" app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginEnd="4dp" | |
tools:ignore="MissingConstraints" | |
android:layout_width="0dp" | |
android:layout_height="0dp"> | |
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout | |
android:id="@+id/mediaContainer" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:adjustViewBounds="true" | |
android:background="@android:color/black" | |
android:elevation="6dp" | |
android:gravity="center" | |
android:scaleType="center" | |
app:cardElevation="6dp" | |
android:visibility="visible" | |
app:cardMaxElevation="10dp" | |
app:layout_constraintDimensionRatio="H,1:1" | |
app:surface_type="texture_view" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:ignore="MissingConstraints"> | |
<ImageView | |
android:id="@+id/feed_post_image" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:adjustViewBounds="true" | |
android:scaleType="centerCrop" | |
android:background="@color/white" | |
app:layout_constraintDimensionRatio="H,1:1" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
/> | |
<!--<com.google.android.exoplayer2.ui.PlayerView | |
android:id="@+id/player" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:resize_mode="fixed_width" | |
app:shutter_background_color="?attr/colorButtonNormal" | |
app:surface_type="surface_view" | |
app:use_controller="false" | |
/>--> | |
<ProgressBar | |
android:id="@+id/progressBar" | |
style="?android:attr/progressBarStyle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:visibility="gone" /> | |
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
<FrameLayout | |
android:id="@+id/container_volume" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_gravity="bottom|end" | |
android:background="@android:color/transparent" | |
app:layout_constraintBottom_toBottomOf="@+id/container_video" | |
app:layout_constraintEnd_toEndOf="@+id/container_video"> | |
<ImageView | |
android:id="@+id/ivVolumeControl" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="end|bottom" | |
android:layout_marginEnd="15dp" | |
android:layout_marginBottom="15dp" | |
android:animateLayoutChanges="true" | |
android:scaleType="centerCrop" | |
android:src="@drawable/ic_volume_on" | |
android:visibility="visible" /> | |
</FrameLayout> | |
<TextView android:layout_width="0dp" | |
android:id="@+id/feed_post_caption" | |
android:text="@string/sample_caption" | |
app:layout_constraintTop_toBottomOf="@id/container_video" | |
android:layout_marginTop="8dp" | |
app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="16dp" | |
android:textAppearance="@style/TextAppearance.AppCompat.Small" | |
android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginEnd="8dp"/> | |
<!--Likes count--> | |
<ImageView android:layout_width="wrap_content" | |
android:id="@+id/feed_post_like_icon" | |
android:src="@drawable/likee" | |
app:layout_constraintTop_toBottomOf="@id/feed_post_caption" | |
android:layout_marginTop="8dp" | |
app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="16dp" | |
android:layout_height="wrap_content"/> | |
<!--Comment count--> | |
<ImageView android:layout_width="wrap_content" | |
android:id="@+id/feed_post_comment_icon" | |
android:src="@drawable/comment_icon" | |
android:layout_height="wrap_content" | |
app:layout_constraintStart_toEndOf="@+id/layout_like" | |
android:layout_marginStart="32dp" | |
app:layout_constraintTop_toTopOf="@+id/layout_like" | |
app:layout_constraintBottom_toBottomOf="@+id/layout_like"/> | |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" | |
android:id="@+id/feed_post_commnet_count" | |
android:text="@string/_105_000" | |
android:layout_marginStart="8dp" | |
app:layout_constraintStart_toEndOf="@id/feed_post_comment_icon" | |
app:layout_constraintTop_toTopOf="@+id/feed_post_comment_icon" | |
app:layout_constraintBottom_toBottomOf="@+id/feed_post_comment_icon"/> | |
<TextView android:layout_width="0dp" android:layout_height="wrap_content" | |
android:id="@+id/feed_post_comment_txt" | |
android:text="@string/comments" | |
app:layout_constraintStart_toEndOf="@id/feed_post_commnet_count" | |
android:layout_marginStart="4dp" | |
app:layout_constraintTop_toTopOf="@+id/feed_post_comment_icon" | |
app:layout_constraintBottom_toBottomOf="@+id/feed_post_comment_icon"/> | |
<de.hdodenhof.circleimageview.CircleImageView | |
android:layout_width="24dp" | |
android:id="@+id/feed_imageProfil_current_user" | |
app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="16dp" | |
android:src="@drawable/avatar_profil" | |
app:civ_border_width="1dp" | |
app:civ_border_color="@color/white" | |
android:layout_height="24dp" | |
app:layout_constraintTop_toBottomOf="@+id/feed_post_like_icon" | |
android:layout_marginTop="8dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:layout_marginBottom="8dp"/> | |
<com.google.android.material.textfield.TextInputLayout | |
android:layout_width="0dp" | |
app:layout_constraintStart_toEndOf="@+id/feed_imageProfil_current_user" | |
android:layout_marginStart="4dp" | |
app:layout_constraintTop_toBottomOf="@+id/feed_post_like_icon" | |
android:layout_height="wrap_content" | |
app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginEnd="8dp" | |
android:id="@+id/textInputLayout" | |
android:layout_marginTop="8dp"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:id="@+id/contrain" | |
android:layout_height="wrap_content"> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:layout_width="0dp" | |
android:id="@+id/feed_post_column_comment" | |
android:hint="@string/write_a_comment" | |
app:layout_constraintStart_toStartOf="parent" | |
android:layout_marginStart="8dp" | |
android:textAppearance="@style/TextAppearance.AppCompat.Small.Inverse" | |
app:layout_constraintTop_toTopOf="parent" | |
android:layout_height="wrap_content" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</com.google.android.material.textfield.TextInputLayout> | |
<LinearLayout | |
android:id="@+id/layout_like" | |
android:orientation="horizontal" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintStart_toEndOf="@+id/feed_post_like_icon" | |
app:layout_constraintBottom_toBottomOf="@+id/feed_post_like_icon" | |
app:layout_constraintTop_toTopOf="@+id/feed_post_like_icon" | |
android:gravity="center_vertical" | |
android:paddingTop="8dp" | |
android:paddingEnd="8dp" | |
android:paddingBottom="8dp" | |
android:layout_marginStart="8dp" | |
tools:ignore="RtlHardcoded,RtlSymmetry"> | |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" | |
android:id="@+id/feed_post_like_count" | |
android:textColor="@color/gray_light" | |
android:text="@string/_105_000"/> | |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" | |
android:id="@+id/feed_post_like_txt" | |
android:textColor="@color/gray_light" | |
android:layout_marginStart="4dp" | |
android:text="@string/likes"/> | |
</LinearLayout> | |
<!--<ImageView android:layout_width="24dp" | |
android:id="@+id/feed_icon_photo" | |
android:src="@drawable/ic_photo_camera" | |
android:layout_height="0dp" | |
android:background="@android:color/transparent" | |
card_view:layout_constraintEnd_toEndOf="@+id/containerView" | |
android:layout_marginEnd="16dp" | |
android:layout_marginTop="2dp" | |
card_view:layout_constraintTop_toTopOf="@+id/feed_post_column_comment" | |
android:layout_marginBottom="2dp" | |
card_view:layout_constraintBottom_toBottomOf="@+id/feed_post_column_comment"/> | |
<ImageView android:layout_width="24dp" | |
android:id="@+id/feed_icon_like_feel" | |
android:src="@drawable/ic_in_love" | |
android:layout_height="0dp" | |
android:background="@android:color/transparent" | |
card_view:layout_constraintEnd_toStartOf="@+id/feed_icon_photo" | |
android:layout_marginEnd="8dp" | |
android:layout_marginTop="2dp" | |
card_view:layout_constraintTop_toTopOf="@+id/feed_post_column_comment" | |
android:layout_marginBottom="2dp" | |
card_view:layout_constraintBottom_toBottomOf="@+id/feed_post_column_comment"/>--> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</com.google.android.material.card.MaterialCardView> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona | |
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.ActivityInfo | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.WindowManager | |
import androidx.fragment.app.FragmentActivity | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.ViewModelProviders | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.bumptech.glide.Glide | |
import com.bumptech.glide.RequestManager | |
import com.bumptech.glide.request.RequestOptions | |
import es.hyrax.zonapets.R | |
import es.hyrax.zonapets.data.local.EventBusModel | |
import es.hyrax.zonapets.data.network.Status | |
import es.hyrax.zonapets.data.network.model.post.Post | |
import es.hyrax.zonapets.ui.base.BaseFragment | |
import es.hyrax.zonapets.ui.dialog.comment.InsertCommentDialog | |
import es.hyrax.zonapets.ui.dialog.comment.InsertCommentListener | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.DashboarHomeFragment | |
import es.hyrax.zonapets.ui.user_private.other_user_profil.OtherUserProfilActivity | |
import es.hyrax.zonapets.ui.user_private.post.edit_post.EditPostActivity | |
import es.hyrax.zonapets.ui.user_private.post.list_user_like_post.ListUserLikesPostActivity | |
import es.hyrax.zonapets.ui.user_private.post.show_post.FeedPostDetailActivity | |
import es.hyrax.zonapets.utils.SharedViewModels | |
import com.google.common.collect.Iterables | |
import es.hyrax.zonapets.data.network.model.post.UserTag | |
import es.hyrax.zonapets.ui.user_private.PlayerInfoHolder | |
import es.hyrax.zonapets.utils.AppConstants | |
import es.hyrax.zonapets.utils.PostUtils | |
import es.hyrax.zonapets.utils.Validation | |
import kotlinx.android.synthetic.main.fragment_my_zona.view.* | |
import org.greenrobot.eventbus.EventBus | |
import org.greenrobot.eventbus.Subscribe | |
import org.jetbrains.anko.intentFor | |
import org.jetbrains.anko.toast | |
import timber.log.Timber | |
import kohii.media.VolumeInfo | |
import kohii.v1.* | |
import kohii.v1.Kohii | |
import java.util.ArrayList | |
class MyZonaFragment : BaseFragment(), | |
MyZonaFragmentAdapter.OnItemClickListener, | |
InsertCommentListener { | |
private lateinit var model: MyZonaFragmentViewModel | |
private lateinit var mview: View | |
private lateinit var myZonaFragmentAdapter: MyZonaFragmentAdapter | |
private lateinit var mActivity: FragmentActivity | |
private var commentDialogFragment: InsertCommentDialog? = null | |
private var position = -1 | |
private lateinit var sharedViewModels: SharedViewModels | |
private var playerInfoHolder: PlayerInfoHolder? = null | |
lateinit var kohii: Kohii | |
var rvHost: TargetHost? = null | |
companion object { | |
const val ID_POST = "ID_POST" | |
const val ACTIVITY_NAME = "MyZonaFragment" | |
} | |
override fun setupViewModel() { | |
model = ViewModelProviders.of(this, viewModelFactory).get(MyZonaFragmentViewModel::class.java) | |
model.networkState.observe(this, Observer { | |
if (it.status == Status.FAILED) { context.toast("${it.msg}") } | |
if (it.status == Status.NOTCONNECTED) { (parentFragment as DashboarHomeFragment).checkConnecttion() } | |
if (it.status == Status.RUNNING) loadingDialog.show() else loadingDialog.dismiss() | |
}) | |
model.networkinitialRequest.observe(this, Observer { | |
mview.swipeRefreshLayout.isRefreshing = false | |
//adapter.setNetworkState(it) | |
if (it.status == Status.RUNNING){ | |
visibleShimmer() | |
}else{ | |
unVisibleShimmer() | |
} | |
}) | |
model.networkLoadAfterInitialRequest .observe(this, Observer { | |
mview.swipeRefreshLayout.isRefreshing = false | |
myZonaFragmentAdapter.setNetworkState(it) | |
}) | |
model.data.observe(this, Observer { | |
showEmptyList(it.size == 0) | |
myZonaFragmentAdapter.submitList(it) | |
}) | |
model.like.observe(this, Observer { | |
val common = it.common | |
if (common?.status == 1) { | |
Timber.d("like %s", it) | |
// no need update using adapter.isLiked(it.position, true) | |
// already handled using Database PostLike | |
} else { | |
context.toast("${common?.message}") | |
} | |
}) | |
model.dislike.observe(this, Observer { | |
val common = it.common | |
if (common?.status == 1) { | |
Timber.d("dislike %s", it) | |
// no need update using adapter.isLiked(it.position, true) | |
// already handled using Database PostLike | |
} else { | |
context.toast("${common?.message}") | |
} | |
}) | |
model.loadPostLike() | |
model.postLike.observe(this, Observer { list -> | |
Timber.d("MyZonaFragment postLike: $list") | |
myZonaFragmentAdapter.currentList?.let { | |
list?.forEach { item -> | |
// item: List<PostLike> | |
// find: List<Post> | |
var postFind: Post? = null | |
val find = Iterables.tryFind(it) { post -> | |
post?.id == item.idPost && post.islike != item.isLiked | |
} | |
if (find.isPresent) { | |
postFind = find.get() | |
} | |
val index = Iterables.indexOf(it) { post -> | |
post?.id == item.idPost && post.islike != item.isLiked | |
} | |
postFind?.let { current -> | |
current.islike = item.isLiked | |
myZonaFragmentAdapter.isLiked(index, current) | |
} | |
} | |
} | |
}) | |
model.loadPostComment() | |
model.postComment.observe(this, Observer { list -> | |
Timber.d("MyZonaFragment postLike: $list") | |
myZonaFragmentAdapter.currentList?.let { | |
list?.forEach { item -> | |
// item: List<PostComment> | |
// find: List<Post> | |
var postFind: Post? = null | |
val find = Iterables.tryFind(it) { post -> | |
post?.id == item.idPost && post.commentcount != item.count | |
} | |
if (find.isPresent) { | |
postFind = find.get() | |
} | |
val index = Iterables.indexOf(it) { post -> | |
post?.id == item.idPost && post.commentcount != item.count | |
} | |
postFind?.let { current -> | |
myZonaFragmentAdapter.isCommented(index) | |
} | |
} | |
} | |
}) | |
model.delete.observe(this, Observer { | |
Timber.d("delete %s", it) | |
if (it?.status == 1) { | |
Timber.d("delete %s", it) | |
myZonaFragmentAdapter.remove() | |
//tell in sharedviewmodel for global zona update adapter item remove | |
sharedViewModels.deleteItem.postValue(this.position) | |
} else { | |
context.toast("${it?.message}") | |
} | |
}) | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
Validation.setLanguageSettings(model.getLanguageSettings(),context) | |
mview = inflater.inflate(R.layout.fragment_my_zona, container, false) | |
mActivity = activity ?: return mview | |
mview.swipeRefreshLayout.setOnRefreshListener { refresh() } | |
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) | |
// Fragment locked in portrait screen orientation | |
getActivity()?.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
//ScrollAwareFABBehavior(recyclerView = mview.feedHome, floatingActionButton = mview.fab).start() | |
return mview | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
activity?.let { | |
sharedViewModels = ViewModelProviders.of(it).get(SharedViewModels::class.java) | |
sharedViewModels.deleteItem.observe(this, Observer { | |
myZonaFragmentAdapter.remove() | |
}) | |
} | |
setupAdapter() | |
} | |
override fun onAttach(context: Context) { | |
super.onAttach(context) | |
playerInfoHolder = context as PlayerInfoHolder? | |
} | |
override fun onDetach() { | |
super.onDetach() | |
playerInfoHolder = null | |
} | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
if (savedInstanceState == null){ | |
model.getAllPostMyZona() | |
} | |
} | |
override fun onItemCommentClick(v: View, position: Int, containerView: View) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
data?.id?.let { f1 -> | |
data.commentcount?.let { f2 -> | |
showInputComment(f1, f2) | |
mview.feed_home.scrollToPosition(position) | |
} | |
} | |
} | |
override fun onItemAvatarClick(v: View, position: Int, containerView: View) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
data?.author?.let { | |
Timber.d(it.name) | |
if (it.id != model.myID()) { | |
startActivity( | |
context.intentFor<OtherUserProfilActivity>( | |
OtherUserProfilActivity.USER_ID to it.id, | |
OtherUserProfilActivity.USER_NAME to it.name | |
) | |
) | |
} | |
} | |
} | |
override fun onItemImageClick(v: View, position: Int,contentPosition:Long) { | |
showDetailPost(position,contentPosition) | |
} | |
override fun onItemLike(v: View, position: Int) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
data?.id?.let { | |
if (data.islike == 1) { | |
model.dislikePost(position, it,0) | |
} else { | |
model.likePost(position, it,0) | |
} | |
} | |
} | |
override fun onItemLikeCountClick(v: View, position: Int) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
if (!data?.likescount.equals("0")) { | |
data?.id?.let { | |
startActivity( | |
context.intentFor<ListUserLikesPostActivity>( | |
ListUserLikesPostActivity.ID_POST to it | |
) | |
) | |
} | |
} | |
} | |
private fun setupAdapter() { | |
kohii = Kohii[this] | |
rvHost = kohii.register(this).registerTargetHost(TargetHost.Builder(mview.feed_home)) | |
myZonaFragmentAdapter = | |
MyZonaFragmentAdapter( | |
context = context, | |
model = model, | |
listener = this, | |
kohii = kohii, | |
/*eventClickListener = this,*/ | |
volumeControl = { | |
val current = it.volumeInfo | |
kohii.applyVolumeInfo( | |
VolumeInfo(!current.mute, current.volume), it, | |
Scope.PLAYBACK | |
) | |
} | |
) { | |
model.retryAllPostMyZona() | |
} | |
mview.feed_home.layoutManager = LinearLayoutManager(activity) | |
mview.feed_home.adapter = myZonaFragmentAdapter | |
} | |
private fun showEmptyList(show: Boolean) { | |
if (show) { | |
mview.no_data.visibility = View.VISIBLE | |
mview.feed_home.visibility = View.GONE | |
} else { | |
mview.no_data.visibility = View.GONE | |
mview.feed_home.visibility = View.VISIBLE | |
} | |
mview.shimmer_view_container.stopShimmer() | |
mview.shimmer_view_container.visibility = View.GONE | |
} | |
fun visibleShimmer(){ | |
mview.shimmer_view_container.startShimmer() | |
mview.shimmer_view_container.visibility = View.VISIBLE | |
mview.no_data.visibility = View.GONE | |
mview.feed_home.visibility = View.GONE | |
} | |
fun unVisibleShimmer(){ | |
mview.shimmer_view_container.stopShimmer() | |
mview.shimmer_view_container.visibility = View.GONE | |
} | |
private fun refresh() { | |
mview.feed_home.visibility = View.GONE | |
mview.shimmer_view_container.startShimmer() | |
model.refreshAllPostMyZona() | |
} | |
private fun showInputComment(id_post: Int, count: String) { | |
commentDialogFragment = InsertCommentDialog(id_post, count) | |
commentDialogFragment?.show(childFragmentManager, commentDialogFragment?.tag) | |
commentDialogFragment?.isCancelable = true | |
} | |
private fun showDetailPost(position: Int,contentPosition: Long) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
data?.id?.let { | |
val isvideo = PostUtils.isVideo(data) | |
if (isvideo) { | |
FeedPostDetailActivity.getStartIntent(context, it, contentPosition.toInt()) | |
}else{ | |
FeedPostDetailActivity.getStartIntent(context, it, 0) | |
} | |
} | |
} | |
private fun editPost(position: Int) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
data?.let { | |
context.let { | |
EditPostActivity.getStartIntentForResultFragment( | |
it, | |
data.id, | |
data.imagepost?.get(0)?.image, | |
data.captionpost, | |
data.location, | |
ACTIVITY_NAME,this | |
) | |
} | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == AppConstants.REQUEST_EDIT_POST && resultCode == Activity.RESULT_OK){ | |
//update | |
refresh() | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
EventBus.getDefault().register(this) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
EventBus.getDefault().unregister(this) | |
} | |
override fun onResume() { | |
super.onResume() | |
// Fragment locked in portrait screen orientation | |
getActivity()?.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
} | |
override fun onPause() { | |
super.onPause() | |
// Fragment locked in portrait screen orientation | |
getActivity()?.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
} | |
@Subscribe | |
fun onMessageEvent(eventBusModel: EventBusModel) { | |
Timber.d("EventBusModel: %s", eventBusModel) | |
if (eventBusModel.response == "update_photo_profile") { | |
myZonaFragmentAdapter.notifyDataSetChanged() | |
} | |
if (eventBusModel.response == "succesUploadNewPost"){ | |
model.refreshAllPostMyZona() | |
} | |
} | |
override fun onCommentSuccess(status: Boolean, id_post: Int, comment: String) { | |
commentDialogFragment?.dismiss() | |
} | |
override fun onItemEditClick(v: View, position: Int, containerView: View) { | |
editPost(position) | |
} | |
override fun onItemDeleteClick(v: View, position: Int, containerView: View) { | |
val data = myZonaFragmentAdapter.getItemData(position) | |
this.position = position | |
data?.id?.let { | |
model.deletePost(it,0) | |
} | |
} | |
override fun onItemTagCaptionClick(v: View, usertag: UserTag) { | |
//val data = adapter.getItemData(position) | |
usertag?.let { | |
Timber.d(it.name) | |
if (it.id != model.myID()) { | |
startActivity( | |
context.intentFor<OtherUserProfilActivity>( | |
OtherUserProfilActivity.USER_ID to it.id, | |
OtherUserProfilActivity.USER_NAME to it.name | |
) | |
) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona | |
import android.content.Context | |
import android.view.MenuItem | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.animation.OvershootInterpolator | |
import android.widget.PopupMenu | |
import androidx.core.content.ContextCompat | |
import androidx.paging.PagedListAdapter | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.RecyclerView | |
import com.ablanco.zoomy.Zoomy | |
import com.bumptech.glide.RequestManager | |
import com.google.android.exoplayer2.ui.PlayerView | |
import es.hyrax.zonapets.R | |
import es.hyrax.zonapets.data.network.NetworkState | |
import es.hyrax.zonapets.data.network.model.post.Post | |
import es.hyrax.zonapets.data.network.model.post.UserTag | |
import es.hyrax.zonapets.ui.user_private.MainPrivateActivity | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.viewholder_myzona.ViewHolderImages | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.viewholder_myzona.ViewHolderMultipleImages | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.viewholder_myzona.ViewHolderVideos | |
import es.hyrax.zonapets.utils.AppConstants | |
import es.hyrax.zonapets.utils.PostUtils | |
import es.hyrax.zonapets.utils.listener.OnDoubleClickListener | |
import es.hyrax.zonapets.utils.paging.LoadMoreViewHolder | |
import kohii.v1.Kohii | |
import kohii.v1.Playback | |
import timber.log.Timber | |
class MyZonaFragmentAdapter( | |
var context: Context, | |
val model: MyZonaFragmentViewModel, | |
val listener: OnItemClickListener, | |
private val kohii: Kohii, | |
private val volumeControl: (Playback<PlayerView>) -> Unit, | |
private val retryCallback: () -> Unit | |
) : PagedListAdapter<Post, RecyclerView.ViewHolder>(comparator) { | |
init { | |
setHasStableIds(true) | |
} | |
override fun getItemId(position: Int): Long { | |
return position.toLong() | |
} | |
companion object { | |
val comparator = object : DiffUtil.ItemCallback<Post>() { | |
override fun areItemsTheSame(oldItem: Post, newItem: Post): Boolean = oldItem.id == newItem.id | |
override fun areContentsTheSame(oldItem: Post, newItem: Post): Boolean = oldItem == newItem | |
} | |
} | |
private lateinit var popupMenu: PopupMenu | |
private var networkState: NetworkState? = null | |
private fun hasExtraRow() = networkState != null && networkState != NetworkState.LOADED | |
override fun getItemViewType(position: Int): Int { | |
return if (hasExtraRow() && position == itemCount - 1) { | |
R.layout.network_state_item | |
} else { | |
if (getItem(position)?.imagepost?.get(0)?.typefile.equals(AppConstants.TYPE_FILE_VIDEO)){ //video post | |
R.layout.item_feed_home_video | |
}else { | |
if (getItem(position)?.imagepost?.size?:0 > 1){ //multiple image post | |
R.layout.item_feed_home_multiple_image | |
}else { | |
R.layout.item_feed_home | |
} | |
} | |
} | |
} | |
override fun getItemCount(): Int { | |
return super.getItemCount() + if (hasExtraRow()) 1 else 0 | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return when (viewType) { | |
R.layout.item_feed_home -> ViewHolderImages.create( | |
parent | |
) | |
R.layout.item_feed_home_video -> ViewHolderVideos.create( | |
parent,kohii | |
) | |
R.layout.item_feed_home_multiple_image -> ViewHolderMultipleImages.create( | |
parent | |
) | |
R.layout.network_state_item -> LoadMoreViewHolder.create(parent, retryCallback) | |
else -> throw IllegalArgumentException("unknown view type $viewType") | |
} | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
when (getItemViewType(position)) { | |
R.layout.item_feed_home -> showViewImages((holder as ViewHolderImages), position) | |
R.layout.item_feed_home_multiple_image -> showViewMultipleImage((holder as ViewHolderMultipleImages), position) | |
R.layout.item_feed_home_video -> showViewVideos((holder as ViewHolderVideos), position) | |
R.layout.network_state_item -> (holder as LoadMoreViewHolder).bindTo(networkState) | |
} | |
} | |
override fun onViewRecycled(holder: RecyclerView.ViewHolder) { | |
super.onViewRecycled(holder) | |
} | |
fun setNetworkState(newNetworkState: NetworkState?) { | |
val previousState = this.networkState | |
val hadExtraRow = hasExtraRow() | |
this.networkState = newNetworkState | |
val hasExtraRow = hasExtraRow() | |
if (hadExtraRow != hasExtraRow) { | |
if (hadExtraRow) { | |
notifyItemRemoved(super.getItemCount()) | |
} else { | |
notifyItemInserted(super.getItemCount()) | |
} | |
} else if (hasExtraRow && previousState != newNetworkState) { | |
notifyItemChanged(itemCount - 1) | |
} | |
} | |
private fun showViewImages(holder: ViewHolderImages, position: Int) { | |
val item = getItem(position) | |
val contentPosition = 0L //this is only for videoholder | |
holder.bind(item, context, model,listener) | |
holder.textInputLayout.setOnClickListener { | |
listener.onItemCommentClick(it, position, holder.itemView) | |
} | |
holder.avatarProfil.setOnClickListener { | |
listener.onItemAvatarClick(it, position, holder.itemView) | |
} | |
holder.nameProfil.setOnClickListener { | |
listener.onItemAvatarClick(it,position,holder.itemView) | |
} | |
holder.feedIconreport.setOnClickListener { | |
Timber.d("%s & %s", item?.author?.id, model.myID()) | |
if (item?.author?.id!!.equals(model.myID())) { | |
showPopUpMenuEdit(holder.feedIconreport,position,holder.itemView) | |
} else { | |
//menu report not yet in V1 app | |
/*showPopUpMenuReport(holder.feedIconreport)*/ | |
} | |
} | |
holder.imgLove.setOnClickListener { | |
if (item?.islike == 1) { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
listener.onItemLike(it, position) | |
} | |
holder.layoutLike.setOnClickListener { | |
listener.onItemLikeCountClick(it, position) | |
} | |
holder.countComment.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition) | |
} | |
holder.feed_post_comment_txt.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition) | |
} | |
val builder = Zoomy.Builder(context as MainPrivateActivity) | |
.target(holder.img_post) | |
.interpolator(OvershootInterpolator()) | |
.tapListener { v -> | |
listener.onItemImageClick(v, position,contentPosition) | |
} | |
.doubleTapListener { v -> | |
if (item?.islike == 1) { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
listener.onItemLike(v, position) | |
} | |
builder.register() | |
} | |
private fun showViewVideos(holder: ViewHolderVideos, position: Int) { | |
val item = getItem(position) | |
holder.bind(item, context, model, position) | |
Timber.d("bind all") | |
val contentPosition:Long? = 0 | |
holder.textInputLayout.setOnClickListener { | |
listener.onItemCommentClick(it, position, holder.itemView) | |
} | |
holder.avatarProfil.setOnClickListener { | |
listener.onItemAvatarClick(it, position, holder.itemView) | |
} | |
holder.nameProfil.setOnClickListener { | |
listener.onItemAvatarClick(it,position,holder.itemView) | |
} | |
holder.feedIconreport.setOnClickListener { | |
Timber.d("%s & %s", item?.author?.id, model.myID()) | |
if (item?.author?.id!!.equals(model.myID())) { | |
showPopUpMenuEdit(holder.feedIconreport,position,holder.itemView) | |
} else { | |
//menu report not yet in V1 app | |
/*showPopUpMenuReport(holder.feedIconreport)*/ | |
} | |
} | |
holder.imgLove.setOnClickListener { | |
if (item?.islike == 1) { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
listener.onItemLike(it, position) | |
} | |
holder.layoutLike.setOnClickListener { | |
listener.onItemLikeCountClick(it, position) | |
} | |
holder.countComment.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition?:0) | |
} | |
holder.feed_post_comment_txt.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition?:0) | |
} | |
holder.mediaContainer.setOnClickListener(object :OnDoubleClickListener(){ | |
override fun onDoubleClick(v: View?) { | |
v?.let { | |
listener.onItemLike(it,position) | |
} | |
} | |
override fun onSingleClick(v: View?) { | |
v?.let { | |
val contentPosition:Long? = 0 | |
listener.onItemImageClick(it,position,contentPosition?:0L) | |
} | |
} | |
}) | |
holder.container_volume.setOnClickListener{ | |
holder?.playback?.let{ | |
volumeControl(it) | |
} | |
} | |
} | |
private fun showViewMultipleImage(holder: ViewHolderMultipleImages, position: Int) { | |
val item = getItem(position) | |
holder.bind(item, context, model,listener,position) | |
val contentPosition = 0L //this is only for videoholder | |
holder.textInputLayout.setOnClickListener { | |
listener.onItemCommentClick(it, position, holder.itemView) | |
} | |
holder.avatarProfil.setOnClickListener { | |
listener.onItemAvatarClick(it, position, holder.itemView) | |
} | |
holder.nameProfil.setOnClickListener { | |
listener.onItemAvatarClick(it,position,holder.itemView) | |
} | |
holder.feedIconreport.setOnClickListener { | |
Timber.d("%s & %s", item?.author?.id, model.myID()) | |
if (item?.author?.id!!.equals(model.myID())) { | |
showPopUpMenuEdit(holder.feedIconreport,position,holder.itemView) | |
} else { | |
//menu report not yet in V1 app | |
/*showPopUpMenuReport(holder.feedIconreport)*/ | |
} | |
} | |
holder.imgLove.setOnClickListener { | |
if (item?.islike == 1) { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
holder.imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
listener.onItemLike(it, position) | |
} | |
holder.layoutLike.setOnClickListener { | |
listener.onItemLikeCountClick(it, position) | |
} | |
holder.countComment.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition) | |
} | |
holder.feed_post_comment_txt.setOnClickListener { | |
listener.onItemImageClick(it,position,contentPosition) | |
} | |
} | |
fun getItemData(position: Int) = getItem(position) | |
fun remove() { | |
currentList?.dataSource?.invalidate() | |
} | |
fun showPopUpMenuEdit(view: View,position: Int, containerView: View) { | |
Timber.d("showPopUpMenuEdit") | |
popupMenu = PopupMenu(context, view) | |
popupMenu.inflate(R.menu.menu_detail) | |
popupMenu.setOnMenuItemClickListener { item -> | |
when(item?.itemId){ | |
R.id.menu_edit -> listener.onItemEditClick(view,position,containerView) | |
else -> listener.onItemDeleteClick(view,position,containerView) | |
} | |
true | |
} | |
popupMenu.show() | |
} | |
fun showPopUpMenuReport(view: View) { | |
Timber.d("showPopUpMenuReport") | |
popupMenu = PopupMenu(context, view) | |
popupMenu.inflate(R.menu.menu_report) | |
popupMenu.setOnMenuItemClickListener(object : PopupMenu.OnMenuItemClickListener { | |
override fun onMenuItemClick(item: MenuItem?): Boolean { | |
/* when(item?.itemId){ | |
R.id.gender_male -> settext_gender("Male") | |
else -> settext_gender("Female") | |
}*/ | |
return true | |
} | |
}) | |
popupMenu.show() | |
} | |
fun isLiked(position: Int, post: Post) { | |
val item = getItem(position) | |
item?.islike = post.islike | |
item?.likescount?.let { | |
item.likescount = if (post.islike == 1) { | |
PostUtils.updateLikesCount(it, true) | |
} else { | |
PostUtils.updateLikesCount(it, false) | |
} | |
} | |
notifyItemChanged(position) | |
} | |
fun isCommented(position: Int) { | |
val item = getItem(position) | |
item?.commentcount?.let { | |
item.commentcount = PostUtils.updateCommentCount(it) | |
} | |
notifyItemChanged(position) | |
} | |
interface OnItemClickListener { | |
fun onItemCommentClick(v: View, position: Int, containerView: View) | |
fun onItemAvatarClick(v: View, position: Int, containerView: View) | |
fun onItemLike(v: View, position: Int) | |
fun onItemLikeCountClick(v: View, position: Int) | |
fun onItemEditClick(v: View, position: Int, containerView: View) | |
fun onItemDeleteClick(v: View, position: Int, containerView: View) | |
fun onItemImageClick(v: View, position: Int,contentPosition:Long) | |
fun onItemTagCaptionClick(v: View,usertag: UserTag) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona | |
import android.util.SparseArray | |
import androidx.lifecycle.MediatorLiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.Transformations | |
import es.hyrax.zonapets.data.database.room.PostComment | |
import es.hyrax.zonapets.data.database.room.PostLike | |
import es.hyrax.zonapets.data.network.NetworkState | |
import es.hyrax.zonapets.data.network.model.common.Common | |
import es.hyrax.zonapets.data.network.model.post.Like | |
import es.hyrax.zonapets.data.network.model.post.Post | |
import es.hyrax.zonapets.data.network.model.tag_comment.TagCommentResponse | |
import es.hyrax.zonapets.data.preference.AppPreferenceHelper | |
import es.hyrax.zonapets.ui.base.BaseViewModel | |
import es.hyrax.zonapets.utils.AppConstants | |
import kohii.media.VolumeInfo | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.GlobalScope | |
import kotlinx.coroutines.launch | |
import java.io.IOException | |
import javax.inject.Inject | |
class MyZonaFragmentViewModel @Inject constructor() : BaseViewModel() { | |
val delete = MutableLiveData<Common>() | |
val like = MutableLiveData<Like>() | |
val dislike = MutableLiveData<Like>() | |
var postLike: MediatorLiveData<List<PostLike>> = MediatorLiveData() | |
var postComment: MediatorLiveData<List<PostComment>> = MediatorLiveData() | |
val tagComment = MutableLiveData<TagCommentResponse>() | |
val volumeInfoStore = SparseArray<VolumeInfo>() | |
fun saveVolumeInfo( | |
key: Int, | |
volumeInfo: VolumeInfo | |
) { | |
volumeInfoStore.put(key, volumeInfo) | |
} | |
init { | |
postLike.value = null | |
postComment.value = null | |
} | |
private val query = MutableLiveData<String>() | |
val itemResult = Transformations.map(query) { | |
repository.getAllPostMyZona() | |
} | |
// networkState.addSource(it.networkState) { network -> | |
// networkState.setValue(network) | |
// } | |
val data = Transformations.switchMap(itemResult) { | |
it.pagedList | |
} | |
val networkinitialRequest = Transformations.switchMap(itemResult) { | |
it.networkStateInitialRequest | |
} | |
val networkLoadAfterInitialRequest = Transformations.switchMap(itemResult){ | |
it.networkStateLoadAfterInitialRequest | |
} | |
fun getAllPostMyZona() { | |
query.postValue("") | |
} | |
fun retryAllPostMyZona() { | |
val listing = itemResult.value | |
listing?.retry?.invoke() | |
} | |
fun refreshAllPostMyZona() { | |
repository.clearDatabase { | |
itemResult.value?.refresh?.invoke() | |
} | |
} | |
fun getUserPhoto() = repository.getPrefrenceString(AppPreferenceHelper.PREF_KEY_PHOTO) | |
fun likePost(position: Int, id_post: Int,retryCount:Int) { | |
networkState.postValue(NetworkState.LOADING) | |
var myretryCount = retryCount | |
GlobalScope.launch(Dispatchers.Main) { | |
try { | |
val request = repository.likePost(id_post) | |
val response = request.await() | |
if (response.isSuccessful) { | |
repository.insertLikesDatabase(PostLike(id_post, 1)) { | |
networkState.postValue(NetworkState.LOADED) | |
like.postValue( | |
Like( | |
position, | |
response.body() | |
) | |
) | |
} | |
} else { | |
networkState.postValue(NetworkState.LOADING) | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
likePost(position,id_post,myretryCount) | |
}else { | |
networkState.postValue(NetworkState.error(response.message())) | |
} | |
} | |
} catch (e: Exception) { | |
networkState.postValue(NetworkState.LOADING) | |
if (e is IOException) { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
likePost(position,id_post,myretryCount) | |
}else { | |
// this is an actual network failure :( inform the user and possibly retry | |
// logging probably not necessary | |
networkState.postValue(NetworkState.failure(AppConstants.CONNECTION_FAILED)) | |
} | |
} else { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
likePost(position,id_post,myretryCount) | |
}else { | |
// conversion issue! big problems :( | |
networkState.postValue(NetworkState.error(e.localizedMessage)) | |
} | |
} | |
} | |
} | |
} | |
fun dislikePost(position: Int, id_post: Int,retryCount:Int) { | |
networkState.postValue(NetworkState.LOADING) | |
var myretryCount = retryCount | |
GlobalScope.launch(Dispatchers.Main) { | |
try { | |
val request = repository.dislikePost(id_post) | |
val response = request.await() | |
if (response.isSuccessful) { | |
repository.insertLikesDatabase(PostLike(id_post, 0)) { | |
networkState.postValue(NetworkState.LOADED) | |
dislike.postValue( | |
Like( | |
position, | |
response.body() | |
) | |
) | |
} | |
} else { | |
networkState.postValue(NetworkState.LOADING) | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
dislikePost(position,id_post,myretryCount) | |
}else { | |
networkState.postValue(NetworkState.error(response.message())) | |
} | |
} | |
} catch (e: Exception) { | |
networkState.postValue(NetworkState.LOADING) | |
if (e is IOException) { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
dislikePost(position,id_post,myretryCount) | |
}else { | |
// this is an actual network failure :( inform the user and possibly retry | |
// logging probably not necessary | |
networkState.postValue(NetworkState.failure(AppConstants.CONNECTION_FAILED)) | |
} | |
} else { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
dislikePost(position,id_post,myretryCount) | |
}else { | |
// conversion issue! big problems :( | |
networkState.postValue(NetworkState.error(e.localizedMessage)) | |
} | |
} | |
} | |
} | |
} | |
fun deletePost(id_post: Int,retryCount:Int) { | |
networkState.postValue(NetworkState.LOADING) | |
var myretryCount = retryCount | |
GlobalScope.launch(Dispatchers.Main) { | |
try { | |
val request = repository.deletePost(id_post) | |
val response = request.await() | |
if (response.isSuccessful) { | |
networkState.postValue(NetworkState.LOADED) | |
delete.value = response.body() | |
} else { | |
networkState.postValue(NetworkState.LOADING) | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
deletePost(id_post,myretryCount) | |
}else { | |
networkState.postValue(NetworkState.error(response.message())) | |
} | |
} | |
} catch (e: Exception) { | |
networkState.postValue(NetworkState.LOADING) | |
if (e is IOException) { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
deletePost(id_post,myretryCount) | |
}else { | |
// this is an actual network failure :( inform the user and possibly retry | |
// logging probably not necessary | |
networkState.postValue(NetworkState.failure(AppConstants.CONNECTION_FAILED)) | |
} | |
} else { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
deletePost(id_post,myretryCount) | |
}else { | |
// conversion issue! big problems :( | |
networkState.postValue(NetworkState.error(e.localizedMessage)) | |
} | |
} | |
} | |
} | |
} | |
fun getUserInTaComment(id_post: Int,name:String?,retryCount:Int) { | |
//networkState.postValue(NetworkState.LOADING) | |
var myretryCount = retryCount | |
GlobalScope.launch(Dispatchers.Main) { | |
try { | |
val request = repository.getTagComment(id_post,name) | |
val response = request.await() | |
if (response.isSuccessful) { | |
networkState.postValue(NetworkState.LOADED) | |
tagComment.value = response.body() | |
} else { | |
// networkState.postValue(NetworkState.LOADING) | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
getUserInTaComment(id_post,name,myretryCount) | |
}else { | |
networkState.postValue(NetworkState.error(response.message())) | |
} | |
} | |
} catch (e: Exception) { | |
// networkState.postValue(NetworkState.LOADING) | |
if (e is IOException) { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
getUserInTaComment(id_post,name,myretryCount) | |
}else { | |
// this is an actual network failure :( inform the user and possibly retry | |
// logging probably not necessary | |
networkState.postValue(NetworkState.failure(AppConstants.CONNECTION_FAILED)) | |
} | |
} else { | |
if (myretryCount < AppConstants.RETRY_COUNT){ | |
myretryCount = myretryCount +1 | |
getUserInTaComment(id_post,name,myretryCount) | |
}else { | |
// conversion issue! big problems :( | |
networkState.postValue(NetworkState.error(e.localizedMessage)) | |
} | |
} | |
} | |
} | |
} | |
fun loadPostLike() = postLike.addSource(repository.loadPostLike()) { | |
postLike.setValue(it) | |
} | |
fun loadPostComment() = postComment.addSource(repository.loadPostComment()) { | |
postComment.setValue(it) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.viewholder_myzona | |
import android.content.Context | |
import android.text.SpannableStringBuilder | |
import android.text.Spanned | |
import android.text.TextPaint | |
import android.text.method.LinkMovementMethod | |
import android.text.style.ClickableSpan | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.ImageView | |
import android.widget.LinearLayout | |
import android.widget.TextView | |
import androidx.core.content.ContextCompat | |
import androidx.core.text.bold | |
import androidx.recyclerview.widget.RecyclerView | |
import com.google.android.material.textfield.TextInputLayout | |
import es.hyrax.zonapets.R | |
import es.hyrax.zonapets.data.network.model.post.Post | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.MyZonaFragmentAdapter | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.MyZonaFragmentViewModel | |
import es.hyrax.zonapets.utils.extension.loadImage | |
import timber.log.Timber | |
class ViewHolderImages internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
internal var avatarProfil: ImageView | |
internal var nameProfil: TextView | |
internal var timestamp: TextView | |
internal var img_post: ImageView | |
internal var caption: TextView | |
internal var layoutLike: LinearLayout | |
internal var countLove: TextView | |
internal var imgLove: ImageView | |
internal var countComment: TextView | |
internal var editPost: ImageView | |
internal var feedIconreport: ImageView | |
internal var miniAvatarProfile: ImageView | |
internal var feed_post_like_txt: TextView | |
internal var feed_post_comment_txt: TextView | |
internal var textInputLayout: TextInputLayout //add a commment | |
companion object { | |
fun create(parent: ViewGroup): ViewHolderImages { | |
val view = LayoutInflater.from(parent.context) | |
val inflate = view.inflate(R.layout.item_feed_home, parent, false) | |
return ViewHolderImages( | |
inflate | |
) | |
} | |
} | |
init { | |
avatarProfil = itemView.findViewById(R.id.feed_imageProfil) | |
nameProfil = itemView.findViewById(R.id.feed_profilName) | |
timestamp = itemView.findViewById(R.id.feed_time) | |
img_post = itemView.findViewById(R.id.feed_post_image) | |
caption = itemView.findViewById(R.id.feed_post_caption) | |
layoutLike = itemView.findViewById(R.id.layout_like) | |
countLove = itemView.findViewById(R.id.feed_post_like_count) | |
imgLove = itemView.findViewById(R.id.feed_post_like_icon) | |
countComment = itemView.findViewById(R.id.feed_post_commnet_count) | |
editPost = itemView.findViewById(R.id.feed_icon_report) | |
textInputLayout = itemView.findViewById(R.id.textInputLayout) | |
feedIconreport = itemView.findViewById(R.id.feed_icon_report) | |
miniAvatarProfile = itemView.findViewById(R.id.feed_imageProfil_current_user) | |
feed_post_like_txt = itemView.findViewById(R.id.feed_post_like_txt) | |
feed_post_comment_txt = itemView.findViewById(R.id.feed_post_comment_txt) | |
} | |
fun bind(item: Post?, context: Context, model: MyZonaFragmentViewModel, listener: MyZonaFragmentAdapter.OnItemClickListener) { | |
item?.let { | |
miniAvatarProfile.loadImage(model.getUserPhoto()) | |
if(item.author?.id == model.myID()) { | |
avatarProfil.loadImage(model.getUserPhoto()) | |
} else { | |
avatarProfil.loadImage(item.author?.photo) | |
} | |
img_post.loadImage(item.imagepost?.get(0)?.image) | |
if (item.islike == 1) { | |
imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
if (item.author?.id!!.equals(model.myID())) { | |
feedIconreport.visibility = View.VISIBLE | |
} else { | |
feedIconreport.visibility = View.INVISIBLE | |
} | |
item.likescount.let { | |
if (it.equals("0")) countLove.visibility = View.GONE | |
else countLove.visibility = View.VISIBLE | |
} | |
item.commentcount.let { | |
if (it.equals("0")) countComment.visibility = View.GONE | |
else countComment.visibility = View.VISIBLE | |
} | |
nameProfil.text = "${item.author?.name}" | |
timestamp.text = "${item.timestamp}" | |
countLove.text = "${item.likescount}" | |
countComment.text = "${item.commentcount}" | |
//setup for caption | |
var tag = "" | |
var i = 0 | |
item.usertag?.forEach { | |
if (i == 0) tag += "" + it.name else { | |
tag += ", " + it.name | |
} | |
i++ | |
} | |
if (tag != "") { | |
tag = " $tag" | |
} | |
//if have tag | |
val bold = SpannableStringBuilder() | |
.append(item.captionpost) | |
.append(" — with ") | |
.bold { append(tag) } | |
val otherStringBuilder = SpannableStringBuilder(bold) | |
item?.usertag?.forEach { | |
val itemclick = object : ClickableSpan(){ | |
override fun onClick(widget: View) { | |
val textview = widget as TextView | |
val text = textview.text | |
val name = text.subSequence(text.indexOf(it.name),text.indexOf(it.name)+ it.name.length) | |
if (it.name.trim().equals(name.trim().toString(),true)){ | |
listener.onItemTagCaptionClick(widget,it) | |
} | |
} | |
override fun updateDrawState(ds: TextPaint) { | |
super.updateDrawState(ds) | |
ds.isUnderlineText = false | |
} | |
} | |
otherStringBuilder.setSpan(itemclick,bold.indexOf(it.name),bold.indexOf(it.name)+ it.name.length, | |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | |
} | |
if (item?.usertag?.size != null ) { | |
caption.text = /*bold*/ otherStringBuilder | |
caption.movementMethod = LinkMovementMethod.getInstance() | |
} else caption.text = item.captionpost | |
// caption.text = "${item.captionpost}" | |
Timber.d("value $item") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.viewholder_myzona | |
import android.content.Context | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.* | |
import androidx.core.content.ContextCompat | |
import androidx.core.util.contains | |
import androidx.recyclerview.widget.RecyclerView | |
import com.bumptech.glide.Glide | |
import com.bumptech.glide.RequestManager | |
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout | |
import com.google.android.exoplayer2.ui.PlayerView | |
import com.google.android.material.textfield.TextInputLayout | |
import es.hyrax.zonapets.R | |
import es.hyrax.zonapets.data.network.model.post.Post | |
import es.hyrax.zonapets.data.network.model.post_detail.CustomPost | |
import es.hyrax.zonapets.ui.user_private.home.Dashboard.my_zona.MyZonaFragmentViewModel | |
import es.hyrax.zonapets.utils.extension.loadImage | |
import kohii.media.VolumeInfo | |
import kohii.v1.* | |
class ViewHolderVideos internal constructor(itemView: View,var kohii: Kohii) : | |
RecyclerView.ViewHolder(itemView),Playback.Callback, | |
VolumeChangedListener { | |
internal var avatarProfil: ImageView | |
internal var nameProfil: TextView | |
internal var timestamp: TextView | |
lateinit var img_post: ImageView | |
internal var caption: TextView | |
internal var layoutLike: LinearLayout | |
internal var countLove: TextView | |
internal var imgLove: ImageView | |
internal var countComment: TextView | |
internal var editPost: ImageView | |
internal var feedIconreport: ImageView | |
internal var miniAvatarProfile: ImageView | |
internal var feed_post_like_txt: TextView | |
internal var feed_post_comment_txt: TextView | |
internal var textInputLayout: TextInputLayout //add a commment | |
public var mediaContainer: AspectRatioFrameLayout = itemView.findViewById(R.id.mediaContainer) | |
lateinit var volumeControl: ImageView | |
lateinit var progressBar: ProgressBar | |
lateinit var container_volume:FrameLayout | |
lateinit var viewmodel: MyZonaFragmentViewModel | |
var videoSources: CustomPost? = null | |
var playback:Playback<PlayerView>? = null | |
val tagKey: String? | |
get() = this.videoSources?.let { "${javaClass.canonicalName}::${it.imagepost?.get(0)?.video}::$adapterPosition" } | |
companion object { | |
fun create(parent: ViewGroup,kohii: Kohii): ViewHolderVideos { | |
val view = LayoutInflater.from(parent.context) | |
val inflate = view.inflate(R.layout.item_feed_home_video, parent, false) | |
return ViewHolderVideos( | |
inflate, | |
kohii | |
) | |
} | |
} | |
init { | |
volumeControl = itemView.findViewById(R.id.ivVolumeControl) | |
progressBar = itemView.findViewById(R.id.progressBar) | |
avatarProfil = itemView.findViewById(R.id.feed_imageProfil) | |
nameProfil = itemView.findViewById(R.id.feed_profilName) | |
timestamp = itemView.findViewById(R.id.feed_time) | |
img_post = itemView.findViewById(R.id.feed_post_image) | |
caption = itemView.findViewById(R.id.feed_post_caption) | |
layoutLike = itemView.findViewById(R.id.layout_like) | |
countLove = itemView.findViewById(R.id.feed_post_like_count) | |
imgLove = itemView.findViewById(R.id.feed_post_like_icon) | |
countComment = itemView.findViewById(R.id.feed_post_commnet_count) | |
editPost = itemView.findViewById(R.id.feed_icon_report) | |
textInputLayout = itemView.findViewById(R.id.textInputLayout) | |
feedIconreport = itemView.findViewById(R.id.feed_icon_report) | |
miniAvatarProfile = itemView.findViewById(R.id.feed_imageProfil_current_user) | |
feed_post_like_txt = itemView.findViewById(R.id.feed_post_like_txt) | |
feed_post_comment_txt = itemView.findViewById(R.id.feed_post_comment_txt) | |
container_volume = itemView.findViewById(R.id.container_volume) | |
} | |
fun bind(item: Post?, context: Context, model: MyZonaFragmentViewModel, position:Int) { | |
this.viewmodel = model | |
itemView.setTag(this) | |
item?.let { | |
img_post.loadImage(it.imagepost?.get(0)?.video) | |
kohii.setUp(it.imagepost?.get(0)?.video?:"").with { | |
tag = tagKey | |
//preLoad = true | |
repeatMode =Playable.REPEAT_MODE_ONE | |
cover = Glide.with(itemView.context.applicationContext) | |
.asBitmap() | |
.load(item.imagepost?.get(0)?.video) | |
.submit() | |
//callback = this@ViewHolderVideos | |
} | |
.bind(ViewTarget(mediaContainer)){playback -> | |
playback.addVolumeChangeListener(this@ViewHolderVideos) | |
if (viewmodel.volumeInfoStore.contains(this.adapterPosition)){ | |
playback.volumeInfo = viewmodel.volumeInfoStore.get(this.adapterPosition) | |
}else{ | |
viewmodel.saveVolumeInfo(this.adapterPosition,playback.volumeInfo) //save first | |
} | |
setupVolumeIcon(volumeInfo = playback.volumeInfo) | |
[email protected] = playback | |
//listener.onItemLoaded(itemView,adapterPosition) | |
} | |
miniAvatarProfile.loadImage(model.getUserPhoto()) | |
if(item.author?.id == model.myID()) { | |
avatarProfil.loadImage(model.getUserPhoto()) | |
} else { | |
avatarProfil.loadImage(item.author?.photo) | |
} | |
//img_post.visibility = View.GONE | |
if (item.islike == 1) { | |
imgLove.setColorFilter(ContextCompat.getColor(context, R.color.green_dark)) | |
} else { | |
imgLove.setColorFilter(ContextCompat.getColor(context, R.color.black)) | |
} | |
if (item.author?.id!!.equals(model.myID())) { | |
feedIconreport.visibility = View.VISIBLE | |
} else { | |
feedIconreport.visibility = View.INVISIBLE | |
} | |
item.likescount.let { | |
if (it.equals("0")) countLove.visibility = View.GONE | |
else countLove.visibility = View.VISIBLE | |
} | |
item.commentcount.let { | |
if (it.equals("0")) countComment.visibility = View.GONE | |
else countComment.visibility = View.VISIBLE | |
} | |
nameProfil.text = "${item.author?.name}" | |
timestamp.text = "${item.timestamp}" | |
caption.text = "${item.captionpost}" | |
countLove.text = "${item.likescount}" | |
countComment.text = "${item.commentcount}" | |
// mediaContainer.setAspectRatio(100F / defaultRatio) | |
} | |
} | |
override fun onVolumeChanged(volumeInfo: VolumeInfo) { | |
setupVolumeIcon(volumeInfo) | |
viewmodel.saveVolumeInfo(this.adapterPosition,volumeInfo) | |
} | |
fun setupVolumeIcon(volumeInfo: VolumeInfo){ | |
if (volumeInfo.mute == true){ | |
volumeControl.setImageResource(R.drawable.ic_volume_off) | |
}else{ | |
volumeControl.setImageResource(R.drawable.ic_volume_on) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment