Skip to content

Commit

Permalink
Implement ViewPowerMenuLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jan 6, 2021
1 parent 49010c4 commit b8d21b2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import kotlin.reflect.KClass
*
* @param context A context for creating resources of the [PowerMenu] lazily.
* @param lifecycleOwner A [LifecycleOwner] for dismissing automatically when the [LifecycleOwner] is being destroyed.
* This will prevents memory leak: [Avoid Memory Leak](https://github.com/skydoves/balloon#avoid-memory-leak).
* @param clazz A [PowerMenu.Factory] kotlin class for creating a new instance of the Balloon.
* This will prevents memory leak: [Avoid Memory Leak](https://github.com/skydoves/powermenu#avoid-memory-leak).
* @param clazz A [PowerMenu.Factory] kotlin class for creating a new instance of the PowerMenu.
*/
@PublishedApi
internal class ActivityPowerMenuLazy<out T : PowerMenu.Factory>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import kotlin.reflect.KClass
* Tied to the given fragment's lifecycle and, [clazz].
*
* @param fragment An instance of the [PowerMenu] will be created in this Fragment lazily.
* This will prevents memory leak: [Avoid Memory Leak](https://github.com/skydoves/balloon#avoid-memory-leak).
* @param clazz A [PowerMenu.Factory] kotlin class for creating a new instance of the Balloon.
* This will prevents memory leak: [Avoid Memory Leak](https://github.com/skydoves/powermenu#avoid-memory-leak).
* @param clazz A [PowerMenu.Factory] kotlin class for creating a new instance of the PowerMenu.
*/
@PublishedApi
internal class FragmentPowerMenuLazy<out T : PowerMenu.Factory>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2017 skydoves
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.skydoves.powermenu.kotlin

import android.content.Context
import androidx.activity.ComponentActivity
import com.skydoves.powermenu.PowerMenu
import java.io.Serializable
import kotlin.reflect.KClass

/**
* An implementation of [Lazy] for creating an instance of the [PowerMenu] lazily in Activities.
* Tied to the given Activity's lifecycleOwner, [factory].
*
* @param context A context for creating resources of the [PowerMenu] lazily.
* This will prevents memory leak: [Avoid Memory Leak](https://github.com/skydoves/powermenu#avoid-memory-leak).
* @param factory A [PowerMenu.Factory] kotlin class for creating a new instance of the PowerMenu.
*/
@PublishedApi
internal class ViewPowerMenuLazy<out T : PowerMenu.Factory>(
private val context: Context,
private val factory: KClass<T>
) : Lazy<PowerMenu>, Serializable {

private var cached: PowerMenu? = null

override val value: PowerMenu
get() {
var instance = cached
if (instance === null) {
if (context is ComponentActivity) {
val factory = factory::java.get().newInstance()
instance = factory.create(context, context)
cached = instance
} else {
throw IllegalArgumentException(
"PowerMenu can not be initialized. The passed context is not an instance of the ComponentActivity."
)
}
}

return instance
}

override fun isInitialized(): Boolean = cached !== null

override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
}

0 comments on commit b8d21b2

Please sign in to comment.