Skip to content

Commit

Permalink
Feature/imp 1248/add mosaic carousel (#52)
Browse files Browse the repository at this point in the history
* add mosaic helper clesses

* Add Mosaic

* Add line space helper class

* Replace old sns icons with new ones and add them to the list

* Finish mosaic

* Fix an error

* Add a horizontal scroll

* Remove logs

* Polish the horizontal list

* remove test background

* Polish PXLWidgetView

* Restore guide

* Set the default of autoAnalyticsEnabled as true

* Add examples to the demo

* Test

* Move back to random mosaic cell size
  • Loading branch information
SungjunApp authored May 4, 2022
1 parent 6387a9c commit c9efc8e
Show file tree
Hide file tree
Showing 37 changed files with 1,855 additions and 155 deletions.
3 changes: 3 additions & 0 deletions pixleesdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ dependencies {
implementation "com.google.android.exoplayer:extension-cronet:$exoPlayer"

implementation 'androidx.annotation:annotation:1.3.0'


implementation "androidx.recyclerview:recyclerview:1.2.1"
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PXLClient(val context: Context) {
var regionId: Int? = null

// if this is true, you can delegate the SDK to fire most analytics events for you. for more information, please read README.md
var autoAnalyticsEnabled: Boolean = false
var autoAnalyticsEnabled: Boolean = true

/***
* Must be called before use. Sets the api key.
Expand Down
17 changes: 11 additions & 6 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/data/PXLPhoto.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,27 @@ public String getVideoUrl(){
* Returns a resource ID to an image representing the current photo's source
* @return
*/
public Integer sourceIconImage() {
public int sourceIconImage() {
if(this.source==null)
return 0;

switch (this.source) {
case "tiktok":
return R.drawable.icon_tiktok;
case "instagram":
return R.drawable.instagram_2x;
return R.drawable.icon_instagram;
case "facebook":
return R.drawable.facebook_2x;
return R.drawable.icon_facebook;
case "pinterest":
return R.drawable.pinterest_2x;
return R.drawable.icon_pinterest;
case "tumblr":
return R.drawable.tumblr_2x;
case "twitter":
return R.drawable.twitter_2x;
return R.drawable.icon_twitter;
case "vine":
return R.drawable.vine_2x;
default:
return null;
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ class PXLPhotoAdapter(
var onPhotoClickedListener: ((view: View, photoWithImageScaleType: PhotoWithImageScaleType) -> Unit)? = null,
var onLoadMoreClickedListener: (() -> Unit)? = null,
var infiniteScroll: Boolean = false,
var sourceIconColor: Int? =null,
var showingDebugView: Boolean = false
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
sealed class Item {
class Header(val listHeader: ListHeader) : Item()
class Content(val data: PhotoWithImageScaleType) : Item()
class LoadMore(var loading:Boolean, val loadMoreTextViewStyle: TextViewStyle) : Item()
class Content(val data: PhotoWithImageScaleType, var itemType: ItemType) : Item()
class LoadMore(var loading:Boolean, val loadMoreTextViewStyle: TextViewStyle, var itemType: ItemType, var width: Int? = null) : Item()
}

sealed class ItemType {
object List : ItemType()
object Grid : ItemType()
class Mosaic(var isLarge: Boolean = false) : ItemType()
object Horizontal : ItemType()
}

val list: ArrayList<Item> = ArrayList()
Expand All @@ -37,7 +45,7 @@ class PXLPhotoAdapter(
is Item.Header -> TYPE_HEADER
is Item.Content -> TYPE_ITEM
is Item.LoadMore -> TYPE_LOAD_MORE

else -> TYPE_ITEM
}
}

Expand All @@ -63,12 +71,9 @@ class PXLPhotoAdapter(
holder.itemView.setOnClickListener(null)
}
is PXLPhotoViewHolder -> {
if(holder.itemView.layoutParams is StaggeredGridLayoutManager.LayoutParams){

}

val data = item as Item.Content
holder.setData(data.data, showingDebugView)

holder.setData(data.data, data.itemType, sourceIconColor, showingDebugView)
holder.itemView.setOnClickListener {
onPhotoClickedListener?.also {
it(holder.itemView, data.data)
Expand All @@ -88,10 +93,13 @@ class PXLPhotoAdapter(
is LoadMoreViewHolder -> {
val data = item as Item.LoadMore
holder.setData(data)
holder.binding.tvLoadMore.setOnClickListener {
onLoadMoreClickedListener?.also {
it()
holder.binding.root.setOnClickListener {
if(holder.binding.tvLoadMore.visibility == View.VISIBLE){
onLoadMoreClickedListener?.also {
it()
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pixlee.pixleesdk.util
package com.pixlee.pixleesdk.ui.decoration

import android.graphics.Rect
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pixlee.pixleesdk.ui.decoration

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.pixlee.pixleesdk.ui.widgets.mosaic.InvalidSpanSizeException
import com.pixlee.pixleesdk.ui.widgets.mosaic.RectsHelper
import com.pixlee.pixleesdk.ui.widgets.mosaic.SpanSize
import com.pixlee.pixleesdk.ui.widgets.mosaic.SpannedGridLayoutManager

class HorizontalSpacingItemDecoration(var spacingPx: Int) : ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)

val isTopEnd = position == 0
val centerPX = spacingPx / 2
val edgePX = 0

outRect.left = if(isTopEnd) edgePX else centerPX
outRect.top = 0
outRect.right = centerPX
outRect.bottom = 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pixlee.pixleesdk.ui.decoration

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.pixlee.pixleesdk.ui.widgets.mosaic.InvalidSpanSizeException
import com.pixlee.pixleesdk.ui.widgets.mosaic.RectsHelper
import com.pixlee.pixleesdk.ui.widgets.mosaic.SpanSize
import com.pixlee.pixleesdk.ui.widgets.mosaic.SpannedGridLayoutManager

class SpannedGridSpacingItemDecoration(val layoutManager: SpannedGridLayoutManager, var spanCount: Int, var spacingPx: Int, var includingEdge: Boolean) : ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)
val rect = layoutManager.getItemIndex(position)

val isTopEnd = rect.top == 0
val isLeftEnd = rect.left == 0
val isRightEnd = rect.right == spanCount
val centerPX = spacingPx / 2
val edgePX = if(includingEdge) spacingPx else 0

outRect.left = if(isLeftEnd) edgePX else centerPX
outRect.top = if(isTopEnd) edgePX else centerPX
outRect.right = if(isRightEnd) edgePX else centerPX
outRect.bottom = centerPX
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pixlee.pixleesdk.ui.viewholder

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -16,6 +17,17 @@ class LoadMoreViewHolder(val binding: ItemLoadMoreBinding) :
RecyclerView.ViewHolder(binding.root) {

fun setData(loadMore: PXLPhotoAdapter.Item.LoadMore) {
when (loadMore.itemType) {
is PXLPhotoAdapter.ItemType.Horizontal -> {
binding.root.layoutParams.width = loadMore.width ?: ViewGroup.LayoutParams.WRAP_CONTENT
binding.root.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
else -> {
binding.root.layoutParams.width = loadMore.width ?: ViewGroup.LayoutParams.MATCH_PARENT
binding.root.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
}
}

binding.tvLoadMore.setTextViewStyle(loadMore.loadMoreTextViewStyle)
when(loadMore.loading){
true -> {
Expand All @@ -26,7 +38,7 @@ class LoadMoreViewHolder(val binding: ItemLoadMoreBinding) :
false -> {
binding.tvLoadMore.visibility = View.VISIBLE
binding.tvLoadMore.isEnabled = true
binding.lottieView.visibility = View.GONE
binding.lottieView.visibility = View.INVISIBLE
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.pixlee.pixleesdk.ui.viewholder

import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.pixlee.pixleesdk.data.PXLPhoto
import com.pixlee.pixleesdk.databinding.ItemPxlphotoBinding
import com.pixlee.pixleesdk.ui.adapter.PXLPhotoAdapter
import com.pixlee.pixleesdk.ui.widgets.PXLPhotoView
import com.pixlee.pixleesdk.util.px
import com.pixlee.pixleesdk.util.setCompatIconWithColor
import kotlinx.android.parcel.Parcelize

/**
Expand All @@ -18,13 +21,40 @@ import kotlinx.android.parcel.Parcelize
class PXLPhotoViewHolder(val binding: ItemPxlphotoBinding) :
RecyclerView.ViewHolder(binding.root) {

fun setData(data: PhotoWithImageScaleType, showingDebugView: Boolean = false) {
binding.pxlPhotoView.layoutParams.height = data.heightInPixel
fun setData(data: PhotoWithImageScaleType, itemType: PXLPhotoAdapter.ItemType, sourceIconColor: Int? = null, showingDebugView: Boolean = false) {
when (itemType) {
is PXLPhotoAdapter.ItemType.Mosaic -> {
binding.root.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
binding.root.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
binding.pxlPhotoView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
binding.pxlPhotoView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
is PXLPhotoAdapter.ItemType.Horizontal -> {
binding.root.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
binding.root.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
binding.pxlPhotoView.layoutParams.width = data.heightInPixel
binding.pxlPhotoView.layoutParams.height = data.heightInPixel
}
else -> {
binding.root.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
binding.root.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
binding.pxlPhotoView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
binding.pxlPhotoView.layoutParams.height = data.heightInPixel
}
}


binding.pxlPhotoView.setConfiguration(configuration = data.configuration)
binding.pxlPhotoView.setContent(data.pxlPhoto, data.configuration.imageScaleType)
binding.pxlPhotoView.setLooping(data.isLoopingVideo)
binding.pxlPhotoView.changeVolume(if(data.soundMuted) 0f else 1f)

val isIconVisible = sourceIconColor!=null && data.pxlPhoto.sourceIconImage() != 0
binding.ivSourceIcon.visibility = if(isIconVisible) View.VISIBLE else View.GONE
binding.ivSourceIcon.setCompatIconWithColor(sourceIconColor, data.pxlPhoto.sourceIconImage())



binding.tv.visibility = if (showingDebugView) View.VISIBLE else View.GONE
binding.tvPercent.visibility = if (showingDebugView) View.VISIBLE else View.GONE
binding.tv.text = "ScaleType: ${data.configuration.imageScaleType.name}\nwidth: ${binding.pxlPhotoView.layoutParams.width}, height: ${binding.pxlPhotoView.layoutParams.height}\nid: ${data.pxlPhoto.id}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import kotlinx.android.parcel.Parcelize
class PXLPhotoView : RelativeLayout {
@Parcelize
data class Configuration(
var pxlPhotoSize: PXLPhotoSize = PXLPhotoSize.ORIGINAL, // PXLPhotoSize [THUMBNAIL, MEDIUM, BIG, ORIGINAL]
var pxlPhotoSize: PXLPhotoSize = PXLPhotoSize.BIG, // PXLPhotoSize [THUMBNAIL, MEDIUM, BIG, ORIGINAL]
var imageScaleType: ImageScaleType = ImageScaleType.FIT_CENTER,
var mainTextViewStyle: TextViewStyle? = null, // if null, the view is gone
var subTextViewStyle: TextViewStyle? = null, // if null, the view is gone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class BaseRecyclerView : RecyclerView {
if (list.isNotEmpty()) {
val position = pxlPhotoAdapter.list.count()
list.forEach {
pxlPhotoAdapter.list.add(PXLPhotoAdapter.Item.Content(it))
pxlPhotoAdapter.list.add(PXLPhotoAdapter.Item.Content(it, PXLPhotoAdapter.ItemType.List))
}
lastItem?.also {
pxlPhotoAdapter.list.add(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ open class ListViewModel(val pxlKtxAlbum: PXLKtxAlbum) {

var loadMoreTextViewStyle: TextViewStyle? = null
var cellHeightInPixel: Int = 200.px.toInt()

val allPXLPhotos = ArrayList<PXLPhoto>()
var canLoadMore = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.pixlee.pixleesdk.ui.adapter.PXLPhotoAdapter
import com.pixlee.pixleesdk.ui.viewholder.PhotoWithImageScaleType
import com.pixlee.pixleesdk.ui.widgets.ImageScaleType
import com.pixlee.pixleesdk.ui.widgets.TextPadding
import com.pixlee.pixleesdk.util.GridSpacingItemDecoration
import com.pixlee.pixleesdk.ui.decoration.GridSpacingItemDecoration
import com.pixlee.pixleesdk.util.px

sealed class ListHeader {
Expand Down Expand Up @@ -57,7 +57,7 @@ open class PXLPhotoRecyclerViewInGrid : BaseRecyclerView {
}
if (list.isNotEmpty()) {
list.forEach {
pxlPhotoAdapter.list.add(PXLPhotoAdapter.Item.Content(it))
pxlPhotoAdapter.list.add(PXLPhotoAdapter.Item.Content(it, PXLPhotoAdapter.ItemType.Grid))
}
pxlPhotoAdapter.notifyDataSetChanged()
}
Expand Down
Loading

0 comments on commit c9efc8e

Please sign in to comment.