Skip to content

Instantly share code, notes, and snippets.

View PiotrPrus's full-sized avatar
🏠
Working from home

Piotr Prus PiotrPrus

🏠
Working from home
View GitHub Profile
@PiotrPrus
PiotrPrus / GridItemRVAdapter.kt
Created January 7, 2020 23:02
RV Adapter that inflate motionLayout and binds item with animationListener
class MySceneGridItemRecyclerViewAdapter(
private val mValues: MutableList<DummyItem>,
private val animationListener: (state: AnimationState, tag: String) -> Unit
) : RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_scene_13_grid, parent, false)
return ViewHolder(view)
}
@PiotrPrus
PiotrPrus / GridListFragment.kt
Last active January 8, 2020 05:45
Fragment that observe RV items with given tag and update its elevation
class GridListFragment {
...
adapter = MySceneGridItemRecyclerViewAdapter( itemList) { state, tag ->
Log.d("Scene13Fragment", "Animation state: $state")
animationStateLiveData.value = state to tag
}
private fun initObservers() {
animationStateLiveData.observe(viewLifecycleOwner, Observer { pair ->
@PiotrPrus
PiotrPrus / CustomMotionLayout.kt
Created January 8, 2020 07:40
Custom ML class that overrides VelocityTracker and provide null checks before each function
class CustomMotionLayout : MotionLayout {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(
context,
attrs
)
constructor(
context: Context?,
attrs: AttributeSet?,
@PiotrPrus
PiotrPrus / InterceptScrollingRecyclerView.kt
Created January 8, 2020 09:58
Custom recyclerView class that intercept touch
class InterceptScrollingRecyclerView: RecyclerView {
constructor(context: Context): super(context)
constructor(context: Context, attrs: AttributeSet): super(context, attrs)
var swipeFrozen = false
override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
if (swipeFrozen) {
@PiotrPrus
PiotrPrus / AnimationStateObserver
Created January 8, 2020 11:01
AnimationState observer with swipeFrozen option
private fun initObservers() {
animationStateLiveData.observe(viewLifecycleOwner, Observer { pair ->
when (pair.first) {
AnimationState.STARTED, AnimationState.IN_PROGRESS -> {
recyclerView.swipeFrozen = true
}
else -> {
recyclerView.swipeFrozen = false
}
}
@PiotrPrus
PiotrPrus / goneRight.xml
Created January 8, 2020 13:06
constraintSet that describe position of view at end of like animation in recyclerView
<ConstraintSet
android:id="@+id/goneRight"
app:deriveConstraintsFrom="@id/like">
<Constraint
android:id="@+id/cardViewScene"
android:layout_marginStart="600dp"
android:rotation="70"
app:layout_constraintHeight_percent="0.9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
@PiotrPrus
PiotrPrus / onTransitionCompleted
Created January 8, 2020 14:46
expanded onTransitionCompleted that trigger item removal
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
animationListener(AnimationState.COMPLETED)
if (p1 == R.id.goneRight || p1 == R.id.goneLeft) {
removeItemListener(position)
}
}
@PiotrPrus
PiotrPrus / GoneRightTransition.xml
Created January 8, 2020 14:57
auto transition from like to goneRight
<Transition
app:autoTransition="animateToEnd"
app:constraintSetEnd="@id/goneRight"
app:constraintSetStart="@id/like"
app:duration="50" />
<Transition
app:constraintSetEnd="@id/like"
app:constraintSetStart="@id/rest"
app:duration="100">
<OnSwipe
app:dragDirection="dragRight"
app:onTouchUp="autoCompleteToStart"
app:touchAnchorId="@id/cardViewScene"
app:touchAnchorSide="right" />
@PiotrPrus
PiotrPrus / awaitListChanged
Created January 9, 2020 10:45
suspend function that waits for item to be removed from recyclerView
private val awaitListChanged: suspend () -> Unit = {
suspendCancellableCoroutine<Int> { cont ->
val observer = object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
unregisterAdapterDataObserver(this)
cont.resume(positionStart)
}
}
cont.invokeOnCancellation { unregisterAdapterDataObserver(observer) }
registerAdapterDataObserver(observer)