Guest User

Untitled

a guest
Jun 25th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. class TimelineAdapter(var timeline: TimelineDTO,
  2. var toggleLikeClicked: OnRowClick,
  3. var onCommentClicked: OnRowClick,
  4. var onMediaClick: OnRowClick,
  5. val onUserClicked: OnRowClick,
  6.  
  7. val reportPost: OnRowClick,
  8. val editPost : OnRowClick,
  9. val deletePost: OnRowClick,
  10.  
  11. val contract: TimelineViewContract) : BaseAdapter<RecyclerView.ViewHolder>() {
  12.  
  13.  
  14. init {
  15. setHasStableIds(true)
  16. }
  17.  
  18. private var currentItem: Int = 0
  19.  
  20. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  21. when (PostType.fromInt(viewType)) {
  22. PostType.BASIC -> {
  23. return PostViewHolder(parent.inflate(R.layout.row_post_default_item),
  24. toggleLikeClicked, onCommentClicked, onMediaClick,
  25. onUserClicked, reportPost,
  26. editPost,
  27. deletePost,
  28. FirebaseAnalytics.getInstance(contract.returnContext()))
  29. }
  30. PostType.NEXT_TALKS -> {
  31. return PostNextTalksViewHolder(parent.inflate(R.layout.row_post_next_talks_item),
  32. contract)
  33. }
  34. else -> {
  35. if(!BuildConfig.DEBUG) {
  36. Crashlytics.log("Should not come here")
  37. }
  38. logE("adapter else!!")
  39. return PostViewHolder(parent.inflate(R.layout.row_post_default_item),
  40. toggleLikeClicked, onCommentClicked, onMediaClick,
  41. onUserClicked, reportPost,
  42. editPost,
  43. deletePost,
  44. FirebaseAnalytics.getInstance(contract.returnContext()))
  45. }
  46. }
  47. }
  48.  
  49. override fun getItemCount(): Int {
  50. var count = timeline.posts.size
  51. if(hasValue(timeline.nextTalks.size)){
  52. count++
  53. }
  54. return count
  55. }
  56.  
  57. override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  58. currentItem = position
  59. val alignedPositon = getAlignedPosition(position)
  60.  
  61. when (holder) {
  62. is PostViewHolder -> holder.bind(timeline.posts[alignedPositon])
  63.  
  64. is PostNextTalksViewHolder -> {
  65. holder.bind(timeline.nextTalks)
  66. }
  67. is PostCarousselViewHolder -> {
  68. holder.bind(ArrayList<String>())
  69. }
  70. }
  71. }
  72.  
  73. fun getPostAt(position: Int): PostDTO {
  74. val post: PostDTO
  75. val alignedPositon = getAlignedPosition(position)
  76. post = timeline.posts[alignedPositon]
  77.  
  78. return post
  79. }
  80.  
  81. override fun getItemId(position: Int): Long {
  82. val aligned = getAlignedPosition(position)
  83.  
  84. return aligned.toLong()
  85. }
  86.  
  87. private fun getAlignedPosition(position: Int): Int {
  88. var alignedPositon = position
  89.  
  90. if (hasValue(timeline.nextTalks.size)){
  91. alignedPositon--
  92. }
  93.  
  94. return alignedPositon
  95. }
  96.  
  97. override fun getItemViewType(position: Int): Int {
  98. val hasPinned = timeline.posts.any { it.postType == PostType.PINNED.id }
  99.  
  100. if(hasPinned) {
  101. if(position == 1 && timeline.nextTalks.any()){
  102. return PostType.NEXT_TALKS.id
  103. }
  104. }
  105. else {
  106. if(position == 0 && timeline.nextTalks.any()){
  107. return PostType.NEXT_TALKS.id
  108. }
  109. }
  110.  
  111. return timeline.posts[getAlignedPosition(position)].postType
  112.  
  113. }
  114.  
  115. fun updateItemAt(postLocal: PostLocal, commentIndexPost: Int) {
  116. timeline.posts.removeAt(commentIndexPost)
  117. timeline.posts.add(commentIndexPost, PostDTO(postLocal))
  118. notifyItemChanged(commentIndexPost)
  119. }
  120.  
  121. fun addItems(newPosts: TimelineDTO) {
  122. timeline.posts.addAll(newPosts.posts)
  123. timeline.nextTalks.addAll(newPosts.nextTalks)
  124.  
  125. notifyItemRangeInserted(itemCount, newPosts.posts.size)
  126. }
  127.  
  128. fun resetItems(nextPosts: TimelineDTO) {
  129. timeline.posts.clear()
  130. timeline.nextTalks.clear()
  131.  
  132. timeline.posts.addAll(nextPosts.posts)
  133. timeline.nextTalks.addAll(nextPosts.nextTalks)
  134.  
  135. notifyDataSetChanged()
  136. }
  137.  
  138. fun removeAt(position: Int) {
  139. timeline.posts.removeAt(position)
  140. notifyItemRemoved(position)
  141. notifyItemRangeChanged(position, timeline.posts.size)
  142. }
  143. }
Add Comment
Please, Sign In to add comment