Skip to content

Commit

Permalink
Feature/internal api marker (#12)
Browse files Browse the repository at this point in the history
[feature] internal api marker
- `BinderInternalApi` created to hide internal library content
- typo fixed
- version bumped to 1.0.7
  • Loading branch information
GrzegorzBobryk authored Oct 10, 2023
1 parent 022372d commit 318dae3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ If applicable, add screenshots to help explain your problem.
**Smartphone (please complete the following information):**
- Device: [e.g. Pixel 3a]
- OS: [e.g. api 33]
- Version [e.g. 1.0.5]
- Version [e.g. 1.0.7]

**Additional context**
Add any other context about the problem here.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and the dependency itself

```gradle
dependencies {
implementation 'com.github.GrzegorzBobryk:ViewBindingDelegate:1.0.6'
implementation 'com.github.GrzegorzBobryk:ViewBindingDelegate:1.0.7'
}
```

Expand Down
2 changes: 1 addition & 1 deletion view-binder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ afterEvaluate {

groupId = 'com.github.grzegorzbobryk'
artifactId = 'view-binder'
version = '1.0.6'
version = '1.0.7'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pl.beavercoding.viewBinder

@RequiresOptIn(message = "Do not use it outside of internal binder api")
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
internal annotation class BinderInternalApi
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@ import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

/**
* Lazy create new [ViewBinding] associated with the [Fragment][this] via delegate without any reflection.
*/
@OptIn(BinderInternalApi::class)
@Suppress("UnusedReceiverParameter")
inline fun <reified T : ViewBinding> Fragment.viewBinding(vb: ViewBinder<T>): ReadOnlyProperty<Fragment, T> =
FragmentViewBindingProperty(vb)

fun interface ViewBinder<T : ViewBinding> {
fun bind(view: View): T
}

/**
* Property that create, hold and destroy binder object. It must be used from ui thread.
* Remember to <strong>not</strong> call binder object from [onDestroyView()] event
* because it will cause recreation and memory leak.
*/
@BinderInternalApi
class FragmentViewBindingProperty<T : ViewBinding>(
private val viewBinder: ViewBinder<T>
) : ReadOnlyProperty<Fragment, T> {
Expand All @@ -33,7 +46,7 @@ class FragmentViewBindingProperty<T : ViewBinding>(
}

private fun assertMainThread() {
check(isMainThread()) { "View binder called outside of mine thread!" }
check(isMainThread()) { "View binder called outside of main thread!" }
}

private fun isMainThread() = Looper.getMainLooper().thread === Thread.currentThread()
Expand All @@ -46,14 +59,3 @@ class FragmentViewBindingProperty<T : ViewBinding>(
}
}
}

fun interface ViewBinder<T : ViewBinding> {
fun bind(view: View): T
}

/**
* Lazy create new [ViewBinding] associated with the [Fragment][this] via delegate without any reflection.
*/
@Suppress("UnusedReceiverParameter")
inline fun <reified T : ViewBinding> Fragment.viewBinding(vb: ViewBinder<T>): ReadOnlyProperty<Fragment, T> =
FragmentViewBindingProperty(vb)

0 comments on commit 318dae3

Please sign in to comment.