Simplify Fragments

This commit is contained in:
Lucas Lima 2020-06-28 01:03:02 -03:00
parent fa9b114599
commit 5c46f73328
No known key found for this signature in database
GPG key ID: C5EEF4C30BFBF8D7
3 changed files with 15 additions and 62 deletions

View file

@ -1,33 +1,17 @@
package dev.lucasnlm.antimine.about.views
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.fragment.app.activityViewModels
import dev.lucasnlm.antimine.BuildConfig
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.models.AboutEvent
import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel
import kotlinx.android.synthetic.main.fragment_about_info.*
class AboutInfoFragment : Fragment() {
private var aboutViewModel: AboutViewModel? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.run {
aboutViewModel = ViewModelProviders.of(this).get(AboutViewModel::class.java)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_about_info, container, false)
class AboutInfoFragment : Fragment(R.layout.fragment_about_info) {
private val aboutViewModel: AboutViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -35,15 +19,15 @@ class AboutInfoFragment : Fragment() {
version.text = getString(R.string.version_s, BuildConfig.VERSION_NAME)
thirdsParties.setOnClickListener {
aboutViewModel?.eventObserver?.postValue(AboutEvent.ThirdPartyLicenses)
aboutViewModel.eventObserver.postValue(AboutEvent.ThirdPartyLicenses)
}
sourceCode.setOnClickListener {
aboutViewModel?.eventObserver?.postValue(AboutEvent.SourceCode)
aboutViewModel.eventObserver.postValue(AboutEvent.SourceCode)
}
translation.setOnClickListener {
aboutViewModel?.eventObserver?.postValue(AboutEvent.Translators)
aboutViewModel.eventObserver.postValue(AboutEvent.Translators)
}
}
}

View file

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
@ -13,28 +14,14 @@ import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel
import kotlinx.android.synthetic.main.fragment_third_party.*
class ThirdPartiesFragment : Fragment() {
private var aboutViewModel: AboutViewModel? = null
class ThirdPartiesFragment : Fragment(R.layout.fragment_third_party) {
private val aboutViewModel: AboutViewModel by activityViewModels()
override fun onResume() {
super.onResume()
activity?.title = getString(R.string.licenses)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.run {
aboutViewModel = ViewModelProviders.of(this).get(AboutViewModel::class.java)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_third_party, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -46,7 +33,7 @@ class ThirdPartiesFragment : Fragment() {
layoutManager = LinearLayoutManager(context)
}
licenses.adapter = aboutViewModel?.getLicenses()
licenses.adapter = aboutViewModel.getLicenses()
}
companion object {

View file

@ -1,43 +1,25 @@
package dev.lucasnlm.antimine.about.views.translators
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel
import kotlinx.android.synthetic.main.fragment_translators.*
class TranslatorsFragment : Fragment() {
private var aboutViewModel: AboutViewModel? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.run {
aboutViewModel = ViewModelProviders.of(this).get(AboutViewModel::class.java)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_translators, container, false)
class TranslatorsFragment : Fragment(R.layout.fragment_translators) {
private val aboutViewModel: AboutViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
translators.apply {
addItemDecoration(
DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)
)
addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL))
layoutManager = LinearLayoutManager(view.context)
adapter = aboutViewModel?.getTranslators()
adapter = aboutViewModel.getTranslators()
}
}