Skip to content

Commit bd80afa

Browse files
authored
Merge pull request #208 from Shanghai-Squad/fix--place-holder
Fix place holder
2 parents 79271c7 + 946ac9f commit bd80afa

20 files changed

Lines changed: 138 additions & 45 deletions

ImageHaramBlur/src/main/java/com/ae/imageharamblur/ui/ImageViewFilter.kt

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.util.AttributeSet
1010
import androidx.core.content.withStyledAttributes
1111
import androidx.core.graphics.drawable.toBitmap
1212
import coil.Coil
13+
import coil.request.CachePolicy
1314
import coil.request.ImageRequest
1415
import com.ae.imageharamblur.R
1516
import com.ae.imageharamblur.utils.StackBlur
@@ -35,7 +36,7 @@ class ImageViewFilter @JvmOverloads constructor(
3536
var imageModel: Any? = null
3637
set(value) {
3738
field = value
38-
value?.let { loadImage(it) }
39+
loadImage(value)
3940
}
4041

4142
var onModerationResult: ((Boolean) -> Unit)? = null
@@ -57,27 +58,73 @@ class ImageViewFilter @JvmOverloads constructor(
5758
}
5859
}
5960

60-
private fun loadImage(model: Any) {
61+
private var currentImageModel: Any? = null
62+
63+
private fun loadImage(model: Any?) {
6164
moderationJob?.cancel()
6265

66+
if (model == null) {
67+
currentImageModel = null
68+
// Center the placeholder and make it smaller
69+
scaleType = ScaleType.CENTER
70+
setImageResource(R.drawable.place_holder)
71+
return
72+
}
73+
74+
val key = model.toString()
75+
if (currentImageModel == model) return
76+
currentImageModel = model
77+
78+
// Reset to normal for real images
79+
scaleType = ScaleType.CENTER_CROP
80+
81+
// Check cache first
82+
ModerationCacheManager.get(key)?.let { cachedState ->
83+
cachedState.blurredBitmap?.let { blurred ->
84+
setImageBitmap(blurred)
85+
onModerationResult?.invoke(true)
86+
return
87+
}
88+
cachedState.originalBitmap?.let { original ->
89+
setImageBitmap(original)
90+
onModerationResult?.invoke(false)
91+
return
92+
}
93+
}
94+
95+
setImageResource(R.drawable.loading)
96+
97+
// Coil loading
6398
Coil.imageLoader(context).enqueue(
6499
ImageRequest.Builder(context)
65100
.data(model)
66-
.placeholder(R.drawable.place_holder)
67-
.error(R.drawable.place_holder)
68-
.target { drawable ->
69-
if (config.forceBlur) {
70-
applyBlur(drawable)
71-
onModerationResult?.invoke(true)
72-
} else if (config.enableModeration) {
73-
moderationJob = coroutineScope.launch(Dispatchers.Default) {
74-
processModeration(drawable)
101+
.diskCachePolicy(CachePolicy.ENABLED)
102+
.memoryCachePolicy(CachePolicy.ENABLED)
103+
.crossfade(true)
104+
.target(
105+
onStart = { placeholder ->
106+
scaleType = ScaleType.CENTER // small placeholder
107+
setImageDrawable(placeholder ?: context.getDrawable(R.drawable.loading))
108+
},
109+
onSuccess = { drawable ->
110+
scaleType = ScaleType.CENTER_CROP // real image
111+
if (config.forceBlur) {
112+
applyBlur(drawable)
113+
onModerationResult?.invoke(true)
114+
} else if (config.enableModeration) {
115+
moderationJob = coroutineScope.launch(Dispatchers.Default) {
116+
processModeration(drawable)
117+
}
118+
} else {
119+
setImageDrawable(drawable)
120+
onModerationResult?.invoke(false)
75121
}
76-
} else {
77-
setImageDrawable(drawable)
78-
onModerationResult?.invoke(false)
122+
},
123+
onError = {
124+
scaleType = ScaleType.CENTER
125+
setImageResource(R.drawable.place_holder)
79126
}
80-
}
127+
)
81128
.build()
82129
)
83130
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector android:height="24dp" android:viewportHeight="72"
3+
android:viewportWidth="72" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#cccccf"
5+
android:pathData="M36.06,28.92L36.06,32.18"
6+
android:strokeColor="#e7e7e7" android:strokeLineCap="round" android:strokeWidth="1"/>
7+
<path android:fillColor="#c8c8cc"
8+
android:pathData="M39.45,29.88L37.82,32.71"
9+
android:strokeColor="#cacaca" android:strokeLineCap="round" android:strokeWidth="1"/>
10+
<path android:fillColor="#bbbbbe"
11+
android:pathData="M42.12,32.32L39.3,33.95"
12+
android:strokeColor="#cdcdcd" android:strokeLineCap="round" android:strokeWidth="1"/>
13+
<path android:fillColor="#b2b2b7"
14+
android:pathData="M39.8,35.98L43.06,35.98"
15+
android:strokeColor="#cbcbcb" android:strokeLineCap="round" android:strokeWidth="1"/>
16+
<path android:fillColor="#d0d0d4"
17+
android:pathData="M32.77,29.99L34.4,32.81"
18+
android:strokeColor="#ededed" android:strokeLineCap="round" android:strokeWidth="1"/>
19+
<path android:fillColor="#949497"
20+
android:pathData="M30.1,32.42L32.92,34.05"
21+
android:strokeColor="#525252" android:strokeLineCap="round" android:strokeWidth="1"/>
22+
<path android:fillColor="#97979b"
23+
android:pathData="M32.42,35.98L29.16,35.98"
24+
android:strokeColor="#6e6e6e" android:strokeLineCap="round" android:strokeWidth="1"/>
25+
<path android:fillColor="#a8a8ac"
26+
android:pathData="M36.06,43.08L36.06,39.82"
27+
android:strokeColor="#a0a0a0" android:strokeLineCap="round" android:strokeWidth="1"/>
28+
<path android:fillColor="#cacaca"
29+
android:pathData="M39.7,41.99L38.07,39.16"
30+
android:strokeColor="#cacaca" android:strokeLineCap="round" android:strokeWidth="1"/>
31+
<path android:fillColor="#b6b6ba"
32+
android:pathData="M42.19,39.4L39.37,37.77"
33+
android:strokeColor="#ccc" android:strokeLineCap="round" android:strokeWidth="1"/>
34+
<path android:fillColor="#a1a1a5"
35+
android:pathData="M32.46,41.98L34.09,39.16"
36+
android:strokeColor="#909090" android:strokeLineCap="round" android:strokeWidth="1"/>
37+
<path android:fillColor="#9d9da0"
38+
android:pathData="M29.85,39.4L32.67,37.77"
39+
android:strokeColor="#7a7a7a" android:strokeLineCap="round" android:strokeWidth="1"/>
40+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:drawable="@drawable/ic_loading"
4+
android:pivotX="50%"
5+
android:pivotY="50%" />

app/src/main/java/com/karrar/movieapp/utilities/BindingAdapter.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.databinding.BindingAdapter
1111
import androidx.recyclerview.widget.PagerSnapHelper
1212
import androidx.recyclerview.widget.RecyclerView
1313
import coil.load
14+
import com.ae.imageharamblur.ui.ImageViewFilter
1415
import com.google.android.material.chip.ChipGroup
1516
import com.google.android.material.textview.MaterialTextView
1617
import com.karrar.movieapp.R
@@ -225,6 +226,11 @@ fun bindMovieImage(image: ImageView, imageURL: String?) {
225226
}
226227
}
227228
}
229+
@BindingAdapter("imageUrl")
230+
fun ImageViewFilter.bindImageUrl(url: String?) {
231+
this.imageModel = url
232+
}
233+
228234

229235
@BindingAdapter("posterImage")
230236
fun ImageView.setPosterImage(imageURL: String?) {

app/src/main/res/layout/fragment_actor_details.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
app:isVisible="@{viewModel.actorDetailsUIState.success}"
138138
app:layout_constraintStart_toStartOf="parent"
139139
app:layout_constraintTop_toTopOf="parent"
140-
app:posterImage="@{viewModel.actorDetailsUIState.imageUrl}"
140+
app:imageUrl="@{viewModel.actorDetailsUIState.imageUrl}"
141141
app:shapeAppearanceOverlay="@style/CardCorners.Custom"
142142
tools:src="@drawable/chris_evans" />
143143

app/src/main/res/layout/grid_media_item.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
android:layout_margin="@dimen/spacing_simi_small"
1919
android:onClick="@{() -> listener.onMediaClick(item)}">
2020

21-
<com.google.android.material.imageview.ShapeableImageView
21+
<com.ae.imageharamblur.ui.ImageViewFilter
2222
android:id="@+id/img_poster"
2323
android:layout_width="match_parent"
2424
android:layout_height="232dp"
@@ -27,9 +27,9 @@
2727
app:layout_constraintEnd_toEndOf="parent"
2828
app:layout_constraintStart_toStartOf="parent"
2929
app:layout_constraintTop_toTopOf="parent"
30-
app:mediaPoster="@{item.mediaImage}"
31-
app:shapeAppearanceOverlay="@style/CardCorners.Medium"
32-
android:background="@drawable/image_error_palceholder" />
30+
app:imageUrl="@{item.mediaImage}"
31+
app:shapeAppearanceOverlay="@style/CardCorners.Medium" />
32+
3333

3434
<LinearLayout
3535
android:id="@+id/rating_container"

app/src/main/res/layout/item_actor_home.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
app:layout_constraintEnd_toEndOf="parent"
3434
app:layout_constraintStart_toStartOf="parent"
3535
app:layout_constraintTop_toTopOf="parent"
36-
app:posterImage="@{item.imageUrl}"
36+
app:imageUrl="@{item.imageUrl}"
3737
app:enableModeration="@{true}"
3838
app:detectFemales="@{true}"
3939
app:detectMales="@{false}" />

app/src/main/res/layout/item_airing_today.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
app:layout_constraintEnd_toEndOf="parent"
3434
app:layout_constraintStart_toStartOf="parent"
3535
app:layout_constraintTop_toTopOf="parent"
36-
app:posterImage="@{item.imageUrl}"
36+
app:imageUrl="@{item.imageUrl}"
3737
app:shapeAppearanceOverlay="@style/CardCorners.Medium" />
3838

3939
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/item_cast.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
app:layout_constraintStart_toStartOf="parent"
3434
app:layout_constraintTop_toTopOf="parent"
3535
app:layout_constraintBottom_toBottomOf="parent"
36-
app:posterImage="@{item.imageUrl}"
36+
app:imageUrl="@{item.imageUrl}"
3737
app:shapeAppearanceOverlay="@style/CastImage"
3838
tools:srcCompat="@drawable/image" />
3939

app/src/main/res/layout/item_category.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
android:layout_margin="@dimen/spacing_simi_small"
1919
android:onClick="@{() -> listener.onClickCategory(item.mediaID)}">
2020

21-
<com.google.android.material.imageview.ShapeableImageView
21+
<com.ae.imageharamblur.ui.ImageViewFilter
2222
android:id="@+id/img_poster"
2323
android:layout_width="match_parent"
2424
android:layout_height="232dp"
@@ -27,9 +27,9 @@
2727
app:layout_constraintEnd_toEndOf="parent"
2828
app:layout_constraintStart_toStartOf="parent"
2929
app:layout_constraintTop_toTopOf="parent"
30-
app:mediaPoster="@{item.mediaImage}"
31-
app:shapeAppearanceOverlay="@style/CardCorners.Medium"
32-
tools:background="@drawable/image_error_palceholder" />
30+
app:imageUrl="@{item.mediaImage}"
31+
app:shapeAppearanceOverlay="@style/CardCorners.Medium" />
32+
3333

3434
<LinearLayout
3535
android:id="@+id/rating_container"

0 commit comments

Comments
 (0)