Skip to content

Commit

Permalink
Merge pull request #5 from Yalantis/hotfix/Issue_4
Browse files Browse the repository at this point in the history
Add lost functionality and fix issue #4
  • Loading branch information
severianremi authored Aug 14, 2018
2 parents 2fbd393 + 7714412 commit 0991f01
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 144 deletions.
50 changes: 31 additions & 19 deletions app/src/main/java/com/yalantis/kolodaandroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.yalantis.kolodaandroid

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.yalantis.library.KolodaListener
import kotlinx.android.synthetic.main.activity_main.*
import android.annotation.SuppressLint
import android.view.Menu
import android.view.MenuItem
import android.view.ViewTreeObserver
import com.yalantis.kolodaandroid.R.id.actionReload


class MainActivity : AppCompatActivity() {
Expand All @@ -27,8 +29,6 @@ class MainActivity : AppCompatActivity() {
@SuppressLint("NewApi")
override fun onGlobalLayout() {
//now we can retrieve the width and height
val width = activityMain.width
val height = activityMain.height
//...
//do whatever you want with them
//...
Expand All @@ -55,19 +55,19 @@ class MainActivity : AppCompatActivity() {
internal var cardsSwiped = 0

override fun onNewTopCard(position: Int) {
Toast.makeText(this@MainActivity, "On new top card", Toast.LENGTH_LONG).show()
//todo realize your logic
}

override fun onCardSwipedLeft(position: Int) {
Toast.makeText(this@MainActivity, "On card swiped left", Toast.LENGTH_LONG).show()
//todo realize your logic
}

override fun onCardSwipedRight(position: Int) {
Toast.makeText(this@MainActivity, "On card swiped right", Toast.LENGTH_LONG).show()
//todo realize your logic
}

override fun onEmptyDeck() {
Toast.makeText(this@MainActivity, "On empty deck", Toast.LENGTH_LONG).show()
//todo realize your logic
}
}
}
Expand All @@ -77,18 +77,18 @@ class MainActivity : AppCompatActivity() {
*/
private fun fillData() {
val data = arrayOf(R.drawable.cupcacke,
R.drawable.donut,
R.drawable.eclair,
R.drawable.froyo,
R.drawable.gingerbread,
R.drawable.honeycomb,
R.drawable.ice_cream_sandwich,
R.drawable.jelly_bean,
R.drawable.kitkat,
R.drawable.lollipop,
R.drawable.marshmallow,
R.drawable.nougat,
R.drawable.oreo)
R.drawable.donut,
R.drawable.eclair,
R.drawable.froyo,
R.drawable.gingerbread,
R.drawable.honeycomb,
R.drawable.ice_cream_sandwich,
R.drawable.jelly_bean,
R.drawable.kitkat,
R.drawable.lollipop,
R.drawable.marshmallow,
R.drawable.nougat,
R.drawable.oreo)
adapter = KolodaSampleAdapter(this, data.toList())
koloda.adapter = adapter
koloda.isNeedCircleLoading = true
Expand All @@ -98,4 +98,16 @@ class MainActivity : AppCompatActivity() {
dislike.setOnClickListener { koloda.onClickLeft() }
like.setOnClickListener { koloda.onClickRight() }
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_reload, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
actionReload -> { koloda.reloadPreviousCard() }
}
return super.onOptionsItemSelected(item)
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_reload.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
<path android:fillColor="#FFFFFF" android:pathData="M12,5V1L7,6l5,5V7c3.31,0 6,2.69 6,6s-2.69,6 -6,6 -6,-2.69 -6,-6H4c0,4.42 3.58,8 8,8s8,-3.58 8,-8 -3.58,-8 -8,-8z"/>
</vector>
21 changes: 10 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,43 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activityMain"
android:background="@color/colorPrimary"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:clipToPadding="false">

<com.yalantis.library.Koloda
android:id="@+id/koloda"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:elevation="1dp"
android:layout_height="match_parent"
app:koloda_card_layout="@layout/item_koloda" />

<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:orientation="horizontal">

<ImageButton
android:id="@+id/dislike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dislike"
android:layout_weight="1"
android:background="@android:color/transparent"/>
android:background="@android:color/transparent"
android:src="@drawable/ic_dislike" />


<ImageButton
android:id="@+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_like"
android:layout_weight="0.5"
android:layout_marginLeft="40dp"
android:background="@android:color/transparent"/>
android:layout_weight="0.5"
android:background="@android:color/transparent"
android:src="@drawable/ic_like" />

</LinearLayout>

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/menu/menu_reload.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/actionReload"
android:title=""
android:icon="@drawable/ic_reload"
app:showAsAction="always"/>

</menu>
106 changes: 106 additions & 0 deletions library/src/main/java/com/yalantis/library/CardLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.yalantis.library

import android.content.Context
import android.os.Build
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView

/**
* Created by anna on 1/2/18.
*/
class CardLayout
@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
defStyleAttr: Int = 0, var cardParams: CardParams) : FrameLayout(context, attrs, defStyleAttr) {

private var rightImageView: ImageView? = null
private var leftImageView: ImageView? = null
private var cardView: View? = null
var layoutAlpha = 1f
set(value) {
field = value
cardView?.alpha = value
}


fun addOverlays(cardView: View?, @DrawableRes rightOverlayImage: Int?,
@DrawableRes leftOverlayImage: Int?) {
this.cardView = cardView
addView(cardView)

leftOverlayImage?.let {
leftImageView = ImageView(context)
leftImageView?.setImageDrawable(ContextCompat.getDrawable(context, it))
leftImageView?.alpha = 0f
addView(leftImageView)
}

rightOverlayImage?.let {
rightImageView = ImageView(context)
rightImageView?.setImageDrawable(ContextCompat.getDrawable(context, it))
rightImageView?.alpha = 0f
addView(rightImageView)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rightImageView?.translationZ = SMALL_ELEVATION
leftImageView?.translationZ = SMALL_ELEVATION
cardView?.translationZ = NO_ELEVATION
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val layoutParamsCardView = cardView?.layoutParams as FrameLayout.LayoutParams

cardView?.alpha = layoutAlpha
cardParams.onParamsMeasure(layoutParamsCardView)

rightImageView?.layoutParams?.let {
(rightImageView?.layoutParams as FrameLayout.LayoutParams).apply {
width = layoutParamsCardView.width
height = layoutParamsCardView.height
gravity = layoutParamsCardView.gravity

setMargins(layoutParamsCardView.leftMargin, layoutParamsCardView.topMargin,
layoutParamsCardView.rightMargin, layoutParamsCardView.bottomMargin)
}
rightImageView?.setPadding(cardView?.paddingLeft ?: 0, cardView?.paddingTop ?: 0,
cardView?.paddingRight ?: 0, cardView?.paddingBottom ?: 0)
}

leftImageView?.layoutParams?.let {
(leftImageView?.layoutParams as FrameLayout.LayoutParams).apply {
width = layoutParamsCardView.width
height = layoutParamsCardView.height
gravity = layoutParamsCardView.gravity
setMargins(layoutParamsCardView.leftMargin, layoutParamsCardView.topMargin,
layoutParamsCardView.rightMargin, layoutParamsCardView.bottomMargin)
}

leftImageView?.setPadding(cardView?.paddingLeft ?: 0, cardView?.paddingTop ?: 0,
cardView?.paddingRight ?: 0, cardView?.paddingBottom ?: 0)
}
}

internal fun changeRightOverlayAlpha(progress: Float) {
rightImageView?.alpha = progress
}

internal fun changeLeftOverlayAlpha(progress: Float) {
leftImageView?.alpha = Math.abs(progress)
}

interface CardParams {
fun onParamsMeasure(layoutParamsCardView: LayoutParams)
}

companion object {
private const val NO_ELEVATION = 0F
private const val SMALL_ELEVATION = 10F
}

}
Loading

0 comments on commit 0991f01

Please sign in to comment.