Skip to content

Commit

Permalink
Optimized interface names
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCaiCoding committed Jun 21, 2022
1 parent fd6f24d commit c481ce2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ package com.dylanc.viewbinding.sample.base.reflection.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.dylanc.viewbinding.base.BindingOwner
import com.dylanc.viewbinding.base.BindingOwnerDelegate
import com.dylanc.viewbinding.base.ActivityBinding
import com.dylanc.viewbinding.base.ActivityBindingDelegate

/**
* @author Dylan Cai
*/
abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity(),
BindingOwner<VB> by BindingOwnerDelegate() {
ActivityBinding<VB> by ActivityBindingDelegate() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.dylanc.viewbinding.base.BindingOwner
import com.dylanc.viewbinding.base.BindingOwnerDelegate
import com.dylanc.viewbinding.base.FragmentBinding
import com.dylanc.viewbinding.base.FragmentBindingDelegate

/**
* @author Dylan Cai
*/
abstract class BaseBindingFragment<VB : ViewBinding> : Fragment(),
BindingOwner<VB> by BindingOwnerDelegate() {
FragmentBinding<VB> by FragmentBindingDelegate() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
createViewWithBinding(inflater, container)
Expand Down
1 change: 1 addition & 0 deletions viewbinding-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ android {

dependencies {
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
}
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)
}
}

This file was deleted.

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
}
}

0 comments on commit c481ce2

Please sign in to comment.