Skip to content

Commit

Permalink
finalize loading items for carousel
Browse files Browse the repository at this point in the history
add toolbar navigation and better FAB UI
add "isPrepared" to attrs
  • Loading branch information
radiopatrick committed Jan 25, 2024
1 parent 499ab68 commit 11dfa18
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 86 deletions.
44 changes: 38 additions & 6 deletions androidveil/src/main/java/com/skydoves/androidveil/VeilLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,21 @@ public class VeilLayout : FrameLayout {
set(value) {
field = value
shimmerContainer.setShimmer(value)
getPreparedView()?.setShimmer(value)
}
public var shimmerEnable: Boolean = true
set(value) {
field = value
when (value) {
true -> shimmerContainer.setShimmer(shimmer)
false -> shimmerContainer.setShimmer(nonShimmer)
true -> {
shimmerContainer.setShimmer(shimmer)
getPreparedView()?.setShimmer(shimmer)
}

false -> {
shimmerContainer.setShimmer(nonShimmer)
getPreparedView()?.setShimmer(nonShimmer)
}
}
}
public var defaultChildVisible: Boolean = false
Expand Down Expand Up @@ -179,6 +187,10 @@ public class VeilLayout : FrameLayout {
defaultChildVisible =
a.getBoolean(R.styleable.VeilLayout_veilLayout_defaultChildVisible, defaultChildVisible)
}
if (a.hasValue(R.styleable.VeilLayout_veilLayout_isPrepared)) {
isPrepared =
a.getBoolean(R.styleable.VeilLayout_veilLayout_isPrepared, isPrepared)
}
} finally {
a.recycle()
}
Expand All @@ -201,6 +213,9 @@ public class VeilLayout : FrameLayout {

/** Remove previous views and inflate a new layout using an inflated view. */
public fun setLayout(layout: View) {
require(!isPrepared || layout is ShimmerFrameLayout) {
"If you place a 'prepared' Layout, then it must be a ShimmerFrameLayout"
}
removeAllViews()
addView(layout)
shimmerContainer.removeAllViews()
Expand All @@ -210,8 +225,9 @@ public class VeilLayout : FrameLayout {
/** Invokes addMaskElements method after inflating. */
override fun onFinishInflate() {
super.onFinishInflate()
removeView(shimmerContainer)
if (!isPrepared) {
removeView(shimmerContainer)
// The layout is not pre-shimmering, this VeilLayout will try to make a close representation
addView(shimmerContainer)
addMaskElements(this)
}
Expand Down Expand Up @@ -304,9 +320,17 @@ public class VeilLayout : FrameLayout {

/** Starts the shimmer animation. */
public fun startShimmer() {
this.shimmerContainer.visible()
if (this.shimmerEnable) {
this.shimmerContainer.startShimmer()
if (shimmerEnable) {
if (isPrepared) {
getPreparedView()?.apply {
setShimmer(shimmer)
visible()
startShimmer()
}
} else {
shimmerContainer.visible()
shimmerContainer.startShimmer()
}
}
if (!this.defaultChildVisible) {
setChildVisibility(false)
Expand Down Expand Up @@ -335,4 +359,12 @@ public class VeilLayout : FrameLayout {
super.invalidate()
this.shimmerContainer.invalidate()
}

private fun getPreparedView(): ShimmerFrameLayout? {
if (childCount > 0) {
val view = getChildAt(0)
if (view is ShimmerFrameLayout) return view
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class VeilRecyclerFrameView : RelativeLayout {
public var shimmer: Shimmer? = null
public var shimmerEnable: Boolean = true
public var defaultChildVisible: Boolean = false
public var isPrepared: Boolean = false
private var isItemWrapContentWidth = false
private var isItemWrapContentHeight = true
private val threshold = 10
Expand Down Expand Up @@ -146,6 +147,10 @@ public class VeilRecyclerFrameView : RelativeLayout {
defaultChildVisible
)
}
if (a.hasValue(R.styleable.VeilRecyclerFrameView_veilFrame_isPrepared)) {
isPrepared =
a.getBoolean(R.styleable.VeilRecyclerFrameView_veilFrame_isPrepared, isPrepared)
}
if (a.hasValue(R.styleable.VeilRecyclerFrameView_veilFrame_isItemWrapContentWidth)) {
isItemWrapContentWidth = a.getBoolean(
R.styleable.VeilRecyclerFrameView_veilFrame_isItemWrapContentWidth,
Expand Down Expand Up @@ -174,8 +179,7 @@ public class VeilRecyclerFrameView : RelativeLayout {
}

if (this.layout != -1) {
// TODO get isPrepared from attrs as well
setVeilLayout(layout = this.layout, isPrepared = false)
setVeilLayout(layout = this.layout, isPrepared = this.isPrepared)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ internal class VeiledAdapter(
radius = veilParams.radius
drawable = veilParams.drawable
shimmerEnable = veilParams.shimmerEnable
// Make sure prepared layout (which is the first child view) is always visible
defaultChildVisible = veilParams.defaultChildVisible || isPrepared
} else {
startShimmer()
Expand Down
3 changes: 3 additions & 0 deletions androidveil/src/main/res/values/attrs_veil.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Designed and developed by 2018 skydoves (Jaewoong Eum)
Expand Down Expand Up @@ -27,6 +28,7 @@
<attr name="veilLayout_highlightAlpha" format="float" />
<attr name="veilLayout_dropOff" format="float" />
<attr name="veilLayout_defaultChildVisible" format="boolean" />
<attr name="veilLayout_isPrepared" format="boolean" />
</declare-styleable>
<declare-styleable name="VeilRecyclerFrameView">
<attr name="veilFrame_veiled" format="boolean" />
Expand All @@ -42,5 +44,6 @@
<attr name="veilFrame_isItemWrapContentWidth" format="boolean" />
<attr name="veilFrame_isItemWrapContentHeight" format="boolean" />
<attr name="veilFrame_defaultChildVisible" format="boolean" />
<attr name="veilFrame_isPrepared" format="boolean" />
</declare-styleable>
</resources>
31 changes: 27 additions & 4 deletions app/src/main/java/com/skydoves/androidveildemo/CarouselActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,49 @@ import com.skydoves.androidveil.VeiledItemOnClickListener
import com.skydoves.androidveildemo.databinding.ActivityCarouselBinding
import com.skydoves.androidveildemo.profile.ListItemUtils
import com.skydoves.androidveildemo.profile.Profile
import com.skydoves.androidveildemo.profile.ProfileAdapter
import com.skydoves.androidveildemo.profile.ProfileCarouselAdapter

class CarouselActivity :
AppCompatActivity(),
VeiledItemOnClickListener,
ProfileCarouselAdapter.ProfileViewHolder.Delegate {

private val adapter = ProfileCarouselAdapter(this)
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val binding = ActivityCarouselBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true);
supportActionBar?.setDisplayShowHomeEnabled(true);

val adapter = ProfileCarouselAdapter(this)
// sets VeilRecyclerView's properties
binding.veilRecyclerView.run {
setVeilLayout(
layout = R.layout.item_preview_carousel,
layout = R.layout.item_prepared_shimmer_carousel,
isPrepared = true,
onItemClickListener = this@CarouselActivity
)
setAdapter(adapter)
setLayoutManager(LinearLayoutManager(this@CarouselActivity, RecyclerView.HORIZONTAL, false))
addVeiledItems(6)
addVeiledItems(10)
}

binding.veilRecyclerView2.run {
setVeilLayout(
layout = R.layout.item_prepared_shimmer_carousel,
isPrepared = true,
onItemClickListener = this@CarouselActivity
)
setAdapter(adapter)
setLayoutManager(LinearLayoutManager(this@CarouselActivity, RecyclerView.HORIZONTAL, false))
addVeiledItems(10)
}

// add profile times to adapter
Expand All @@ -64,6 +81,12 @@ class CarouselActivity :
{
binding.veilRecyclerView.unVeil()
},
3000
)
Handler(Looper.getMainLooper()).postDelayed(
{
binding.veilRecyclerView2.unVeil()
},
5000
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ class GridActivity :

private val adapter = ProfileAdapter(this)

override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val binding = ActivityGridBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true);
supportActionBar?.setDisplayShowHomeEnabled(true);

// sets VeilRecyclerView's properties
binding.veilFrameView.run {
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/com/skydoves/androidveildemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -79,13 +80,15 @@ class MainActivity :
} else {
closeFloatingMenu()
}
}
}
binding.fabGrid.setOnClickListener {
startActivity(Intent(this, GridActivity::class.java))
}
closeFloatingMenu()
}
binding.fabCarousel.setOnClickListener {
startActivity(Intent(this, CarouselActivity::class.java))
}
closeFloatingMenu()
}
}

override fun onBackPressed() {
Expand All @@ -98,14 +101,20 @@ class MainActivity :

private fun showFloatingMenu() {
isFloatingMenuOpen = true
binding.fabCarousel.animate().translationY(-resources.getDimension(R.dimen.distance_fab_first))
binding.fabGrid.animate().translationY(-resources.getDimension(R.dimen.distance_fab_second))
binding.containerFabCarousel.animate()
.translationY(-resources.getDimension(R.dimen.distance_fab_first))
binding.containerFabGrid.animate()
.translationY(-resources.getDimension(R.dimen.distance_fab_second))
binding.fabGridText.visibility = View.VISIBLE
binding.fabCarouselText.visibility = View.VISIBLE
}

private fun closeFloatingMenu() {
isFloatingMenuOpen = false;
binding.fabCarousel.animate().translationY(0f);
binding.fabGrid.animate().translationY(0f);
binding.containerFabCarousel.animate().translationY(0f);
binding.containerFabGrid.animate().translationY(0f);
binding.fabGridText.visibility = View.GONE
binding.fabCarouselText.visibility = View.GONE
}

/** OnItemClickListener by Veiled Item */
Expand Down
35 changes: 31 additions & 4 deletions app/src/main/res/layout/activity_carousel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,45 @@
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/detail_toolbar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:title="@string/app_name" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_default"
android:text="Carousel 1"
android:textColor="@android:color/white"
android:textStyle="bold" />

<com.skydoves.androidveil.VeilRecyclerFrameView
android:id="@+id/veilRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail_toolbar"
android:layout_height="150dp"
app:veilFrame_baseAlpha="0.6"
app:veilFrame_baseColor="@android:color/holo_green_dark"
app:veilFrame_highlightAlpha="1.0"
app:veilFrame_highlightColor="@android:color/holo_green_light"
app:veilFrame_isItemWrapContentWidth="true"
app:veilFrame_radius="4dp"
app:veilFrame_shimmerEnable="true"
app:veilFrame_veiled="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_default"
android:text="Carousel 2"
android:textColor="@android:color/white"
android:textStyle="bold" />

<com.skydoves.androidveil.VeilRecyclerFrameView
android:id="@+id/veilRecyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:veilFrame_baseAlpha="0.6"
app:veilFrame_baseColor="@android:color/holo_green_dark"
app:veilFrame_highlightAlpha="1.0"
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_grid.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
Designed and developed by 2018 skydoves (Jaewoong Eum)
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -23,7 +22,7 @@
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/detail_toolbar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
Expand Down
Loading

0 comments on commit 11dfa18

Please sign in to comment.