Итак, кто-то сказал мне ранее использовать модель общего представления в моем другом вопросе ( Как мне сохранить данные из фрагмента, чтобы их можно было повторно использовать в архитектуре MVVM? )
В приведенном ниже коде не используется модель общего представления, и он работает быстрее
package io.github.andraantariksa.cratesio.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import io.github.andraantariksa.cratesio.R
import io.github.andraantariksa.cratesio.data.api.model.CrateSummary.CrateSummary
import io.github.andraantariksa.cratesio.utils.InjectorUtils
import kotlinx.android.synthetic.main.fragment_summary.*
import kotlinx.coroutines.launch
class SummaryFragment : ScopedFragment() {
private lateinit var viewModelFactory: CratesViewModelFactory
private lateinit var viewModel: CratesViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_summary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
viewModel = ViewModelProvider(this, viewModelFactory)
.get(CratesViewModel::class.java)
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
private fun fetchRecyclerViewData() {
launch {
val crateSummaryLiveData = viewModel.crateSummary.await()
crateSummaryLiveData.observe(viewLifecycleOwner, Observer {
setupSummaryRecyclerView(it)
})
}
}
private fun setupSummaryRecyclerView(crateSummary: CrateSummary) {
viewModel.cratesSummaryLast = crateSummary
textViewCratesTotalNum.text = crateSummary.numCrates.toString()
textViewDownloadsTotalNum.text = crateSummary.numDownloads.toString()
val newCratesAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.newCrates)
recyclerViewNewCrates.layoutManager = LinearLayoutManager(context)
recyclerViewNewCrates.adapter = newCratesAdapter
val mostDownloadedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.mostDownloaded)
recyclerViewMostDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostDownloaded.adapter = mostDownloadedAdapter
val justUpdatedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.justUpdated)
recyclerViewJustUpdated.layoutManager = LinearLayoutManager(context)
recyclerViewJustUpdated.adapter = justUpdatedAdapter
val mostRecentlyDownloaded =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewMostRecentlyDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostRecentlyDownloaded.adapter = mostRecentlyDownloaded
val mostPopularKeywords =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewPopularKeywords.layoutManager = LinearLayoutManager(context)
recyclerViewPopularKeywords.adapter = mostPopularKeywords
val popularCategories =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerPopularCategories.layoutManager = LinearLayoutManager(context)
recyclerPopularCategories.adapter = popularCategories
}
}
Но при использовании модели общего представления доступ к фрагменту / открытие фрагмента стал медленнее
package io.github.andraantariksa.cratesio.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import io.github.andraantariksa.cratesio.R
import io.github.andraantariksa.cratesio.data.api.model.CrateSummary.CrateSummary
import io.github.andraantariksa.cratesio.utils.InjectorUtils
import kotlinx.android.synthetic.main.fragment_summary.*
import kotlinx.coroutines.launch
class SummaryFragment : ScopedFragment() {
// private lateinit var viewModelFactory: CratesViewModelFactory
// private lateinit var viewModel: CratesViewModel
private val viewModel: CratesViewModel by activityViewModels {
InjectorUtils.provideCratesViewModelFactory(requireContext())
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_summary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
// viewModel = ViewModelProvider(this, viewModelFactory)
// .get(CratesViewModel::class.java)
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
private fun fetchRecyclerViewData() {
launch {
val crateSummaryLiveData = viewModel.crateSummary.await()
crateSummaryLiveData.observe(viewLifecycleOwner, Observer {
setupSummaryRecyclerView(it)
})
}
}
private fun setupSummaryRecyclerView(crateSummary: CrateSummary) {
viewModel.cratesSummaryLast = crateSummary
textViewCratesTotalNum.text = crateSummary.numCrates.toString()
textViewDownloadsTotalNum.text = crateSummary.numDownloads.toString()
val newCratesAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.newCrates)
recyclerViewNewCrates.layoutManager = LinearLayoutManager(context)
recyclerViewNewCrates.adapter = newCratesAdapter
val mostDownloadedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.mostDownloaded)
recyclerViewMostDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostDownloaded.adapter = mostDownloadedAdapter
val justUpdatedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.justUpdated)
recyclerViewJustUpdated.layoutManager = LinearLayoutManager(context)
recyclerViewJustUpdated.adapter = justUpdatedAdapter
val mostRecentlyDownloaded =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewMostRecentlyDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostRecentlyDownloaded.adapter = mostRecentlyDownloaded
val mostPopularKeywords =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewPopularKeywords.layoutManager = LinearLayoutManager(context)
recyclerViewPopularKeywords.adapter = mostPopularKeywords
val popularCategories =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerPopularCategories.layoutManager = LinearLayoutManager(context)
recyclerPopularCategories.adapter = popularCategories
}
}
Итак, почему фрагмент медленно открывается? И как мне это исправить? Неправильно ли я использую модель общего представления?
Edit
Поэтому мне нужно запустить setupSummaryRecyclerView
асинхронно. Но все равно медленнее
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
viewModel = ViewModelProvider(this, viewModelFactory)
.get(CratesViewModel::class.java)
launch {
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
}