Skip to content

Commit

Permalink
Bug fixes, Refactoring, Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Prem Suman committed Dec 27, 2018
1 parent 4b61649 commit 14bbad6
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SelectionActivity : BaseCardActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_selection)

cardWithListBtn.onReducingClick { pushFragment(CardWithListFragment()) }
cardWithListBtn.onReducingClick { pushFragment(CardWithListFragment.newInstance(true)) }

listOfCardsBtn.onReducingClick { pushFragment(ListOfCardsContainerFragment()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.prembros.facilis.cardwithlist

import android.view.*
import androidx.recyclerview.widget.RecyclerView
import com.prembros.facilis.activity.BaseCardActivity
import com.prembros.facilis.sample.R
import com.prembros.facilis.util.*
import kotlinx.android.synthetic.main.item_card_list.view.*
import org.jetbrains.anko.toast
import kotlin.random.Random

class CardWithListAdapter : RecyclerView.Adapter<CardWithListAdapter.CardWithListViewHolder>() {
class CardWithListAdapter(private val activity: BaseCardActivity) : RecyclerView.Adapter<CardWithListAdapter.CardWithListViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardWithListViewHolder =
CardWithListViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_card_list, parent, false))
Expand All @@ -16,10 +18,15 @@ class CardWithListAdapter : RecyclerView.Adapter<CardWithListAdapter.CardWithLis
holder.itemView.run {
cardListItem.text = cardListItem.context.getString(R.string.card_item_, (position + 1))
rootCard.setCardBackgroundColor(getRandomMaterialColor())
rootCard.onReducingClick { rootCard.context.toast("This is reducing click") }
rootCard.onReducingClick {
rootCard.context.toast("This is reducing click")
activity.pushFragment(CardWithListFragment.newInstance(false))
}
}
}

private fun getRandomBoolean(): Boolean = arrayListOf(true, false)[Random.nextInt(1)]

override fun getItemCount(): Int = 10

class CardWithListViewHolder(view: View) : RecyclerView.ViewHolder(view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import android.os.Bundle
import android.view.*
import com.prembros.facilis.fragment.BaseCardFragment
import com.prembros.facilis.sample.R
import com.prembros.facilis.util.getRandomMaterialColor
import kotlinx.android.synthetic.main.fragment_card_with_list.*
import org.jetbrains.anko.backgroundColor

class CardWithListFragment : BaseCardFragment() {

companion object {
private const val IS_BLUR_ENABLED = "isBlurEnabled"
fun newInstance(isBlurEnabled: Boolean) = CardWithListFragment().apply { arguments = Bundle().apply { putBoolean(IS_BLUR_ENABLED, isBlurEnabled) } }
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.run { isBlurEnabled = getBoolean(IS_BLUR_ENABLED, true) }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_card_with_list, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView.adapter = CardWithListAdapter()
header.text = getString(R.string.this_is_a_card_fragment_with_a_recyclerview, parentActivity().index)
header.backgroundColor = getRandomMaterialColor()
recyclerView.adapter = CardWithListAdapter(parentActivity())
}

override fun getBackgroundBlurLayout(): ViewGroup? = blurLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import android.os.Bundle
import android.view.*
import com.prembros.facilis.fragment.BaseCardFragment
import com.prembros.facilis.sample.R
import com.prembros.facilis.util.getRandomMaterialColor
import kotlinx.android.synthetic.main.fragment_plain_card.*

class PlainCardFragment : BaseCardFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_plain_card, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rootCard.setCardBackgroundColor(getRandomMaterialColor())
}

override fun getBackgroundBlurLayout(): ViewGroup? = blurLayout

override fun getDragView(): View? = dragArea
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ScrollView
style="@style/MatchParentMatchParent"
android:clipToPadding="false"
android:paddingTop="160dp"
android:paddingTop="20dp"
android:paddingBottom="20dp">

<LinearLayout
Expand Down Expand Up @@ -131,7 +131,6 @@
<androidx.cardview.widget.CardView
android:id="@+id/longPressBlurPopupBtn"
style="@style/MatchParentWrapContent"
android:foreground="@drawable/ripple_dark_rect_curved_corners"
app:cardBackgroundColor="@color/husk"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/fragment_card_with_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@

<LinearLayout
style="@style/MatchParentMatchParent"
android:layout_marginTop="15dp"
android:orientation="vertical">

<TextView
android:id="@+id/header"
style="@style/MatchParentWrapContent"
android:layout_gravity="center_horizontal"
android:paddingTop="15dp"
android:gravity="center"
android:paddingBottom="15dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/this_is_a_card_fragment_with_a_recyclerview"
android:textColor="@color/grey"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

Expand All @@ -50,7 +51,7 @@

<ImageView
android:id="@+id/drag_handle_image"
style="@style/DragHandleDark"
style="@style/DragHandleLight"
android:contentDescription="@string/drag_handle" />

<View
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/fragment_plain_card.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_layout"
style="@style/MatchParentMatchParent"
android:animateLayoutChanges="true">
Expand All @@ -11,8 +10,7 @@
<androidx.cardview.widget.CardView
android:id="@+id/rootCard"
style="@style/CardView.WrapWidthHeight"
android:layout_centerInParent="true"
app:cardBackgroundColor="@color/cornflower_blue">
android:layout_centerInParent="true">

<TextView
android:layout_width="wrap_content"
Expand Down
48 changes: 36 additions & 12 deletions app/src/main/res/layout/item_card_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@
style="@style/CardView.WrapHeight"
android:layout_gravity="center">

<TextView
android:id="@+id/cardListItem"
style="@style/WrapContentWrapContent"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="@drawable/bg_translucent"
<LinearLayout
style="@style/MatchParentWrapContent"
android:gravity="center"
android:paddingStart="20dp"
android:paddingTop="5dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:text="@string/card_item_"
android:textColor="@android:color/white" />
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingBottom="20dp">

<TextView
android:id="@+id/cardListItem"
style="@style/WrapContentWrapContent"
android:layout_gravity="center"
android:background="@drawable/bg_translucent"
android:gravity="center"
android:paddingStart="20dp"
android:paddingTop="5dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:textColor="@android:color/white" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="8dp"
android:background="@color/off_white" />

<TextView
style="@style/MatchParentWrapContent"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="@string/click_to_open_another_card_with_list"
android:textColor="@android:color/white"
android:textSize="12sp" />

</LinearLayout>

</androidx.cardview.widget.CardView>
5 changes: 0 additions & 5 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorDisabled">#19555555</color>
<color name="white">#FFFFFF</color>
<color name="off_white">#b3ffffff</color>
<color name="black">#000000</color>
<color name="red">#E10C1B</color>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
<string name="this_is_a_sample_plain_card_fragment">This is a sample plain card fragment</string>
<string name="you_can_swipe_down_from_anywhere_inside_to_dismiss_this_card">You can swipe down from anywhere inside to dismiss this card</string>
<string name="card_item_">Card Item %1$d</string>
<string name="this_is_a_card_fragment_with_a_recyclerview">This is a card fragment with a recyclerView</string>
<string name="this_is_a_card_fragment_with_a_recyclerview">This is a card fragment with a recyclerView, index %1$d</string>
<string name="card_with_list_implementation">It contains a basic implementation of recyclerView, you can swipe down form the top to dismiss this fragment.</string>
<string name="list_of_cards_implementation">It contains an implementation of horizontal ViewPager, which has cards as children.\n\nYou can swipe down from the top of any card to dismiss all.</string>
<string name="this_is_a_sample_blur_popup">This is a sample blur popup</string>
<string name="blur_popup_implementation">You can swipe down from the top of any card to dismiss all.\nOr you can click outside, on the blurred area to dismiss this popup.</string>
<string name="click_to_open_another_card_with_list">Click to open another Card with list</string>
</resources>
7 changes: 4 additions & 3 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorPrimary">@color/shuttle_grey</item>
<item name="colorPrimaryDark">@color/shuttle_grey</item>
<item name="colorAccent">@color/crusoe</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>

<style name="CardView.Full" parent="CardView.Light">
Expand Down
18 changes: 11 additions & 7 deletions facilis/src/main/java/com/prembros/facilis/util/ViewUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,18 @@ fun View.onReducingClick(launchDelay: Long = 100, action: () -> Unit) {
}
ACTION_CANCEL, ACTION_UP -> animatorOf(R.animator.original_size).start()
ACTION_MOVE -> {
if (isNullOrEmpty(originPoint)) originPoint = arrayListOf(event.rawX, event.rawY)

if (!isNullOrEmpty(originPoint)) {
val deltaX = Math.abs(originPoint[0] - event.rawX)
val deltaY = Math.abs(originPoint[1] - event.rawY)
if (deltaX > 1 && deltaY > 1) {
animatorOf(R.animator.original_size).start()
try {
if (isNullOrEmpty(originPoint)) originPoint = arrayListOf(event.rawX, event.rawY)

if (!isNullOrEmpty(originPoint)) {
val deltaX = Math.abs(originPoint[0] - event.rawX)
val deltaY = Math.abs(originPoint[1] - event.rawY)
if (deltaX > 1 && deltaY > 1) {
animatorOf(R.animator.original_size).start()
}
}
} catch (e: Exception) {
animatorOf(R.animator.original_size).start()
}
}
}
Expand Down

0 comments on commit 14bbad6

Please sign in to comment.