Merge pull request #250 from HaliksaR/improvements/viewbinding

improvements view binding
This commit is contained in:
Tornike Khintibidze 2020-11-15 01:26:57 +04:00 committed by GitHub
commit 50a59a4eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 130 deletions

View File

@ -53,6 +53,7 @@ android {
buildFeatures { buildFeatures {
dataBinding true dataBinding true
viewBinding true
} }
// To inline the bytecode built with JVM target 1.8 into // To inline the bytecode built with JVM target 1.8 into

View File

@ -0,0 +1,37 @@
package com.vanced.manager.ui.core
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
abstract class BindingFragment<VB : ViewBinding> : Fragment() {
private var _binding: VB? = null
protected val binding: VB get() = requireNotNull(_binding)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = binding(inflater, container, savedInstanceState)
otherSetups()
return binding.root
}
protected abstract fun binding(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): VB
protected open fun otherSetups() = Unit
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

View File

@ -9,37 +9,33 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.core.content.edit import androidx.core.content.edit
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.databinding.FragmentAboutBinding import com.vanced.manager.databinding.FragmentAboutBinding
import com.vanced.manager.ui.core.BindingFragment
import com.vanced.manager.ui.viewmodels.AboutViewModel import com.vanced.manager.ui.viewmodels.AboutViewModel
class AboutFragment : Fragment() { class AboutFragment : BindingFragment<FragmentAboutBinding>() {
private lateinit var binding: FragmentAboutBinding
private val viewModel: AboutViewModel by viewModels() private val viewModel: AboutViewModel by viewModels()
private var count = 0 private var count = 0
private var startMillSec: Long = 0 private var startMillSec: Long = 0
override fun onCreateView( override fun binding(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ) = FragmentAboutBinding.inflate(inflater, container, false)
requireActivity().title = getString(R.string.title_about)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_about, container, false) override fun otherSetups() {
binding.viewModel = viewModel dataBind()
return binding.root
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { private fun dataBind() {
super.onViewCreated(view, savedInstanceState) requireActivity().title = getString(R.string.title_about)
binding.root.setOnTouchListener { _, event: MotionEvent ->
view.setOnTouchListener { _, event: MotionEvent ->
val eventAction = event.action val eventAction = event.action
if (eventAction == MotionEvent.ACTION_UP) { if (eventAction == MotionEvent.ACTION_UP) {
val time = System.currentTimeMillis() val time = System.currentTimeMillis()
@ -64,5 +60,7 @@ class AboutFragment : Fragment() {
} }
false false
} }
binding.aboutSources.aboutGithubButton.setOnClickListener { viewModel.openUrl("https://github.com/YTVanced/VancedInstaller") }
binding.aboutSources.aboutLicenseButton.setOnClickListener { viewModel.openUrl("https://raw.githubusercontent.com/YTVanced/VancedInstaller/dev/LICENSE") }
} }
} }

View File

@ -1,16 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.core.widget.NestedScrollView
xmlns:bind="http://schemas.android.com/tools"> xmlns:android="http://schemas.android.com/apk/res/android"
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.AboutViewModel" />
</data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fillViewport="true" android:fillViewport="true"
@ -19,40 +9,34 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="@dimen/twelvedp" android:layout_marginTop="@dimen/twelvedp"
android:layout_marginEnd="16dp"
android:clipToPadding="false" android:clipToPadding="false"
android:orientation="vertical"> android:orientation="vertical">
<include <include
layout="@layout/include_about_header" layout="@layout/include_about_header"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"/>
<include <include
layout="@layout/include_about_vanced_devs" layout="@layout/include_about_vanced_devs"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/stdp" /> android:layout_marginTop="@dimen/stdp"/>
<include <include
layout="@layout/include_about_app_devs" layout="@layout/include_about_app_devs"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/stdp" /> android:layout_marginTop="@dimen/stdp"/>
<include <include
android:id="@+id/about_sources"
layout="@layout/include_about_sources" layout="@layout/include_about_sources"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/stdp" android:layout_marginTop="@dimen/stdp"/>
bind:viewModel="@{viewModel}" />
</LinearLayout> </LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.core.widget.NestedScrollView>
</layout>

View File

@ -1,15 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <com.google.android.material.card.MaterialCardView
<layout> xmlns:android="http://schemas.android.com/apk/res/android"
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.AboutViewModel" />
</data>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -43,7 +33,6 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/about_github_button" android:id="@+id/about_github_button"
style="@style/SocialButtonStyle" style="@style/SocialButtonStyle"
android:onClick='@{()-> viewModel.openUrl("https://github.com/YTVanced/VancedInstaller")}'
app:icon="@drawable/ic_github" app:icon="@drawable/ic_github"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/about_license_button" app:layout_constraintEnd_toStartOf="@id/about_license_button"
@ -54,7 +43,6 @@
android:id="@+id/about_license_button" android:id="@+id/about_license_button"
style="@style/SocialButtonStyle" style="@style/SocialButtonStyle"
android:fontFamily="@font/exo_semibold" android:fontFamily="@font/exo_semibold"
android:onClick='@{()-> viewModel.openUrl("https://raw.githubusercontent.com/YTVanced/VancedInstaller/dev/LICENSE")}'
android:text="GPL" android:text="GPL"
android:textSize="21sp" android:textSize="21sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@ -62,12 +50,6 @@
app:layout_constraintStart_toEndOf="@id/about_github_button" app:layout_constraintStart_toEndOf="@id/about_github_button"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
</com.google.android.flexbox.FlexboxLayout> </com.google.android.flexbox.FlexboxLayout>
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.card.MaterialCardView>
</layout>