|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 David Allison <davidallisongithub@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + * This file incorporates code under the following license: |
| 17 | + * |
| 18 | + * Copyright 2020-2025 Kirill Rozov |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | + |
| 33 | +@file:JvmName("ActivityViewBindings") |
| 34 | + |
| 35 | +package dev.androidbroadcast.vbpd |
| 36 | + |
| 37 | +import android.app.Activity |
| 38 | +import android.app.Application.ActivityLifecycleCallbacks |
| 39 | +import android.os.Bundle |
| 40 | +import android.view.View |
| 41 | +import androidx.annotation.IdRes |
| 42 | +import androidx.annotation.RestrictTo |
| 43 | +import androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP |
| 44 | +import androidx.viewbinding.ViewBinding |
| 45 | +import dev.androidbroadcast.vbpd.internal.findRootView |
| 46 | +import dev.androidbroadcast.vbpd.internal.requireViewByIdCompat |
| 47 | +import dev.androidbroadcast.vbpd.internal.weakReference |
| 48 | +import kotlin.reflect.KProperty |
| 49 | + |
| 50 | +@RestrictTo(LIBRARY_GROUP) |
| 51 | +public class ActivityViewBindingProperty<in A : Activity, T : ViewBinding>( |
| 52 | + viewBinder: (A) -> T, |
| 53 | +) : LazyViewBindingProperty<A, T>(viewBinder) { |
| 54 | + private var lifecycleCallbacks: ActivityLifecycleCallbacks? = null |
| 55 | + private var activity: Activity? by weakReference(null) |
| 56 | + |
| 57 | + override fun getValue( |
| 58 | + thisRef: A, |
| 59 | + property: KProperty<*>, |
| 60 | + ): T = |
| 61 | + super |
| 62 | + .getValue(thisRef, property) |
| 63 | + .also { registerLifecycleCallbacksIfNeeded(thisRef) } |
| 64 | + |
| 65 | + private fun registerLifecycleCallbacksIfNeeded(activity: Activity) { |
| 66 | + if (lifecycleCallbacks != null) return |
| 67 | + this.activity = activity |
| 68 | + VBActivityLifecycleCallbacks() |
| 69 | + .also { callbacks -> this.lifecycleCallbacks = callbacks } |
| 70 | + .let(activity.application::registerActivityLifecycleCallbacks) |
| 71 | + } |
| 72 | + |
| 73 | + override fun clear() { |
| 74 | + super.clear() |
| 75 | + val lifecycleCallbacks = lifecycleCallbacks |
| 76 | + if (lifecycleCallbacks != null) { |
| 77 | + activity?.application?.unregisterActivityLifecycleCallbacks(lifecycleCallbacks) |
| 78 | + } |
| 79 | + |
| 80 | + this.activity = null |
| 81 | + this.lifecycleCallbacks = null |
| 82 | + } |
| 83 | + |
| 84 | + private inner class VBActivityLifecycleCallbacks : ActivityLifecycleCallbacks { |
| 85 | + override fun onActivityCreated( |
| 86 | + activity: Activity, |
| 87 | + savedInstanceState: Bundle?, |
| 88 | + ) { |
| 89 | + } |
| 90 | + |
| 91 | + override fun onActivityStarted(activity: Activity) { |
| 92 | + } |
| 93 | + |
| 94 | + override fun onActivityResumed(activity: Activity) { |
| 95 | + } |
| 96 | + |
| 97 | + override fun onActivityPaused(activity: Activity) { |
| 98 | + } |
| 99 | + |
| 100 | + override fun onActivityStopped(activity: Activity) { |
| 101 | + } |
| 102 | + |
| 103 | + override fun onActivitySaveInstanceState( |
| 104 | + activity: Activity, |
| 105 | + outState: Bundle, |
| 106 | + ) { |
| 107 | + } |
| 108 | + |
| 109 | + override fun onActivityDestroyed(activity: Activity) { |
| 110 | + if (activity === this@ActivityViewBindingProperty.activity) clear() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Create new [ViewBinding] associated with the [Activity]. |
| 117 | + * Cached [ViewBinding] will be cleaned after [Activity.onDestroy] |
| 118 | + * |
| 119 | + * @param viewBinder Function that creates a new instance of [ViewBinding]. Use `MyViewBinding::bind` as default |
| 120 | + * |
| 121 | + * @return [ViewBindingProperty] associated with the [Activity]'s view |
| 122 | + */ |
| 123 | +@JvmName("viewBindingActivityWithCallbacks") |
| 124 | +@Suppress("UnusedReceiverParameter") |
| 125 | +public fun <A : Activity, T : ViewBinding> Activity.viewBinding(viewBinder: (A) -> T): ViewBindingProperty<A, T> = |
| 126 | + ActivityViewBindingProperty(viewBinder = viewBinder) |
| 127 | + |
| 128 | +/** |
| 129 | + * Create new [ViewBinding] associated with the [Activity]. |
| 130 | + * Cached [ViewBinding] will be cleaned after [Activity.onDestroy] |
| 131 | + * |
| 132 | + * @param vbFactory Function that creates a new instance of [ViewBinding]. Use `MyViewBinding::bind` as default |
| 133 | + * @param viewProvider Function that provides a root view for the view binding |
| 134 | + * |
| 135 | + * @return [ViewBindingProperty] associated with the [Activity]'s view |
| 136 | + */ |
| 137 | +@JvmName("viewBindingActivityWithCallbacks") |
| 138 | +public inline fun <A : Activity, T : ViewBinding> Activity.viewBinding( |
| 139 | + crossinline vbFactory: (View) -> T, |
| 140 | + crossinline viewProvider: (A) -> View = ::findRootView, |
| 141 | +): ViewBindingProperty<A, T> = viewBinding { activity -> vbFactory(viewProvider(activity)) } |
| 142 | + |
| 143 | +/** |
| 144 | + * Create new [ViewBinding] associated with the [Activity][this] and allow customization of how |
| 145 | + * a [View] will be bound to the view binding. |
| 146 | + * |
| 147 | + * @param vbFactory Function that creates a new instance of [ViewBinding]. `MyViewBinding::bind` can be used |
| 148 | + * @param viewBindingRootId Root view's id that will be used as a root for the view binding |
| 149 | + * |
| 150 | + * @return [ViewBindingProperty] associated with the [Activity]'s view |
| 151 | + */ |
| 152 | +@JvmName("viewBindingActivity") |
| 153 | +public inline fun <A : Activity, T : ViewBinding> Activity.viewBinding( |
| 154 | + crossinline vbFactory: (View) -> T, |
| 155 | + @IdRes viewBindingRootId: Int, |
| 156 | +): ViewBindingProperty<A, T> = |
| 157 | + viewBinding { activity -> |
| 158 | + vbFactory(activity.requireViewByIdCompat(viewBindingRootId)) |
| 159 | + } |
0 commit comments