Skip to content

Commit 81db642

Browse files
authored
New presenter APIs, release v0.5.0 (#12)
* Add presenter.onUnbind, onAttach, onDetach * Add docs, release v0.5.0
1 parent a08fce0 commit 81db642

File tree

9 files changed

+70
-12
lines changed

9 files changed

+70
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A collection of modular elements for `RecyclerView` lists, alternative to
1616
[Google's Paging library](https://developer.android.com/topic/libraries/architecture/paging/), designed in Kotlin with these goals in mind:
1717

1818
```kotlin
19-
implementation("com.otaliastudios:elements:0.4.0")
19+
implementation("com.otaliastudios:elements:0.5.0")
2020
```
2121

2222
Design features:

build.gradle.kts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
buildscript {
22

33
extra["minSdkVersion"] = 14
4-
extra["compileSdkVersion"] = 29
5-
extra["targetSdkVersion"] = 29
4+
extra["compileSdkVersion"] = 30
5+
extra["targetSdkVersion"] = 30
66

77
repositories {
88
mavenCentral()
@@ -11,8 +11,8 @@ buildscript {
1111
}
1212

1313
dependencies {
14-
classpath("com.android.tools.build:gradle:4.0.1")
15-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")
14+
classpath("com.android.tools.build:gradle:4.1.1")
15+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
1616
classpath("com.otaliastudios.tools:publisher:0.3.3")
1717
}
1818
}

docs/_about/changelog.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ New versions are released through GitHub, so the reference page is the [GitHub R
99
> You can [support development](https://github.com/natario1/Elements/issues/4) through the GitHub Sponsors program.
1010
Companies can share a tiny part of their revenue and get private support hours in return. Thanks!
1111

12+
### v0.5.0
13+
14+
- New: Presenter.onUnbind, to release resources acquired during onBind [#12][12]
15+
- New: Presenter.onAttach and Presenter.onDetach to understand when the view is attached to the hierarchy [#12][12]
16+
17+
<https://github.com/natario1/Elements/compare/v0.4.0...v0.5.0>
18+
1219
### v0.4.0
1320

14-
- Refreshed dependencies, Kotlin 1.4.0 [#10][10]
21+
- Refreshed dependencies, Kotlin 1.4.0 [#10][10]
1522

1623
<https://github.com/natario1/Elements/compare/v0.3.7...v0.4.0>
1724

@@ -22,3 +29,4 @@ First versioned release.
2229
[natario1]: https://github.com/natario1
2330

2431
[10]: https://github.com/natario1/Elements/pull/10
32+
[12]: https://github.com/natario1/Elements/pull/12

docs/_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ google_analytics_id: 'UA-155077779-6'
1212
google_site_verification: '4x49i17ABIrSvUl52SeL0-t0341aTnWWaC62-FYCRT4'
1313
github: [metadata] # TODO What's this?
1414
github_repo: Elements
15-
github_version: 0.4.0
15+
github_version: 0.5.0
1616
github_branch: master
1717
baseurl: '/Elements' # Keep as an empty string if served up at the root
1818
collections:

docs/_docs/presenters.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ These are the main tasks:
1616
|----|--------|-----------|
1717
|Holder creation|`onCreate(ViewGroup, Int)`|Here you must provide a `Holder` instance, typically inflating a layout resource.|
1818
|Holder initialization|`onInitialize(Holder, Int)`|The holder was created. You can perform here initialization task that do not depend on data (like color filters to icon), or add Views and object to the Holder cache using `Holder.set(key, data)` and `Holder.get(key)`.|
19-
|Binding|`onBind(Page, Holder, Element<T>)`|Bind data, contained in the given `Element`, to the view held by `Holder`.|
19+
|Binding holder to data|`onBind(Page, Holder, Element<T>)`|Bind data, contained in the given `Element`, to the view held by `Holder`.|
20+
|Attaching holder to hierarchy|`onAttach(Holder)`|Called when holder is attached to the view hierarchy and is about to be visible.|
21+
|Detaching holder from hierarchy|`onDetach(Holder)`|Called when holder is detached from the view hierarchy and is invisible.|
22+
|Unbinding holder from data|`onUnbind(Holder)`|Called when the holder is unbound from the `Element` data. Can be used to release resources acquired during `onBind`.|
2023

2124
Presenters also **accept a click listener** that will be automatically added to each view.
2225
The click listener will be added to the root view of the Holder, or, if found, to a child view that
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

library/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ android {
1414
defaultConfig {
1515
setMinSdkVersion(property("minSdkVersion") as Int)
1616
setTargetSdkVersion(property("targetSdkVersion") as Int)
17-
versionName = "0.4.0"
17+
versionName = "0.5.0"
1818
}
1919

2020
sourceSets {

library/src/main/kotlin/com/otaliastudios/elements/Adapter.kt

+26-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public final class Adapter private constructor(
338338
@Suppress("UNCHECKED_CAST")
339339
val cast = data as LiveData<List<Element<*>>>
340340
var firstTime = true
341-
cast.observe(this, Observer {
341+
cast.observe(this, {
342342
if (it != null) {
343343
val sync = fromSavedState && firstTime
344344
onSourceResults(page, source, it, sync)
@@ -409,7 +409,31 @@ public final class Adapter private constructor(
409409
}
410410

411411
// We use the payloads version.
412-
override fun onBindViewHolder(holder: Presenter.Holder, position: Int) {}
412+
override fun onBindViewHolder(holder: Presenter.Holder, position: Int): Unit = Unit
413+
414+
override fun onFailedToRecycleView(holder: Presenter.Holder): Boolean {
415+
onUnbindViewHolder(holder)
416+
return false
417+
}
418+
419+
override fun onViewRecycled(holder: Presenter.Holder) {
420+
onUnbindViewHolder(holder)
421+
}
422+
423+
private fun onUnbindViewHolder(holder: Presenter.Holder) {
424+
val presenter = typeMap.get(holder.itemViewType)
425+
presenter.onUnbind(holder)
426+
}
427+
428+
override fun onViewAttachedToWindow(holder: Presenter.Holder) {
429+
val presenter = typeMap.get(holder.itemViewType)
430+
presenter.onAttach(holder)
431+
}
432+
433+
override fun onViewDetachedFromWindow(holder: Presenter.Holder) {
434+
val presenter = typeMap.get(holder.itemViewType)
435+
presenter.onDetach(holder)
436+
}
413437

414438
/**
415439
* Request a new page to be opened.

library/src/main/kotlin/com/otaliastudios/elements/Presenter.kt

+23
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ public abstract class Presenter<T: Any>(
113113
}
114114
}
115115

116+
/**
117+
* Called when the [Holder] is unbound from the element that was applied during [onBind].
118+
* Every [onBind] call is followed by an [onUnbind] call later on. This can be used to release
119+
* resources.
120+
*/
121+
@UiThread
122+
public open fun onUnbind(holder: Holder): Unit = Unit
123+
124+
/**
125+
* Called after [onBind], when the [Holder] is attached to the view hierarchy and is about to
126+
* be visible to the user.
127+
*/
128+
@UiThread
129+
public open fun onAttach(holder: Holder): Unit = Unit
130+
131+
/**
132+
* Called when the [Holder] is detached from the view hierarchy that was attached during [onAttach].
133+
* Every [onAttach] call is followed by an [onDetach] call later on. At this point the holder
134+
* is not visible anymore.
135+
*/
136+
@UiThread
137+
public open fun onDetach(holder: Holder): Unit = Unit
138+
116139
/**
117140
* Called to understand whether we should perform animations for the given animation type
118141
* and for the given holder. Presenters have fine grained control over what is animated and

0 commit comments

Comments
 (0)