-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd6f24d
commit c481ce2
Showing
6 changed files
with
102 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
viewbinding-base/src/main/java/com/dylanc/viewbinding/base/ActivityBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2020. Dylan Cai | ||
* | ||
* 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.dylanc.viewbinding.base | ||
|
||
import android.app.Activity | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.viewbinding.ViewBinding | ||
|
||
interface ActivityBinding<VB : ViewBinding> { | ||
val binding: VB | ||
fun Activity.setContentViewWithBinding() | ||
} | ||
|
||
class ActivityBindingDelegate<VB : ViewBinding> : ActivityBinding<VB> { | ||
private lateinit var _binding: VB | ||
|
||
override val binding: VB get() = _binding | ||
|
||
override fun Activity.setContentViewWithBinding() { | ||
_binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater) | ||
setContentView(binding.root) | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
viewbinding-base/src/main/java/com/dylanc/viewbinding/base/BindingOwner.kt
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
viewbinding-base/src/main/java/com/dylanc/viewbinding/base/FragmentBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2020. Dylan Cai | ||
* | ||
* 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.dylanc.viewbinding.base | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.viewbinding.ViewBinding | ||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
interface FragmentBinding<VB : ViewBinding> { | ||
val binding: VB | ||
fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View | ||
} | ||
|
||
class FragmentBindingDelegate<VB : ViewBinding> : FragmentBinding<VB> { | ||
private var _binding: VB? = null | ||
private val handler by lazy { Handler(Looper.getMainLooper()) } | ||
|
||
override val binding: VB | ||
get() = requireNotNull(_binding) { "The property of binding has been destroyed." } | ||
|
||
override fun Fragment.createViewWithBinding(inflater: LayoutInflater, container: ViewGroup?): View { | ||
if (_binding == null) { | ||
_binding = ViewBindingUtil.inflateWithGeneric(this, inflater, container, false) | ||
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver { | ||
override fun onDestroy(owner: LifecycleOwner) { | ||
handler.post { _binding = null } | ||
} | ||
}) | ||
} | ||
return _binding!!.root | ||
} | ||
} |