From 8b9ddca7bdf200e6d77bf1769bc92494c31207a2 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 27 Dec 2020 21:25:35 +0100 Subject: [PATCH] cleanup code in ConversationsFragment, SearchFragment and Report*Fragments (#2027) --- .../conversation/ConversationsFragment.kt | 3 --- .../report/fragments/ReportDoneFragment.kt | 16 +++------------- .../report/fragments/ReportNoteFragment.kt | 14 +++----------- .../report/fragments/ReportStatusesFragment.kt | 18 ++++-------------- .../search/fragments/SearchFragment.kt | 13 +++---------- 5 files changed, 13 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt index 2add9c449..d50f90438 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt @@ -28,7 +28,6 @@ import androidx.recyclerview.widget.SimpleItemAnimator import com.keylesspalace.tusky.AccountActivity import com.keylesspalace.tusky.R import com.keylesspalace.tusky.ViewTagActivity -import com.keylesspalace.tusky.db.AppDatabase import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.di.ViewModelFactory import com.keylesspalace.tusky.fragment.SFragment @@ -46,8 +45,6 @@ class ConversationsFragment : SFragment(), StatusActionListener, Injectable, Res @Inject lateinit var viewModelFactory: ViewModelFactory - @Inject - lateinit var db: AppDatabase private val viewModel: ConversationsViewModel by viewModels { viewModelFactory } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportDoneFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportDoneFragment.kt index 6151903fd..03bd8ef91 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportDoneFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportDoneFragment.kt @@ -15,13 +15,10 @@ package com.keylesspalace.tusky.components.report.fragments - import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels +import androidx.fragment.app.activityViewModels import com.keylesspalace.tusky.R import com.keylesspalace.tusky.components.report.ReportViewModel import com.keylesspalace.tusky.components.report.Screen @@ -33,19 +30,12 @@ import com.keylesspalace.tusky.util.show import kotlinx.android.synthetic.main.fragment_report_done.* import javax.inject.Inject - -class ReportDoneFragment : Fragment(), Injectable { +class ReportDoneFragment : Fragment(R.layout.fragment_report_done), Injectable { @Inject lateinit var viewModelFactory: ViewModelFactory - private val viewModel: ReportViewModel by viewModels({ requireActivity() }) { viewModelFactory } - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_report_done, container, false) - } + private val viewModel: ReportViewModel by activityViewModels { viewModelFactory } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { textReported.text = getString(R.string.report_sent_success, viewModel.accountUserName) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt index 4e7b00abf..b933b2fa7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt @@ -16,12 +16,10 @@ package com.keylesspalace.tusky.components.report.fragments import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels +import androidx.fragment.app.activityViewModels import com.google.android.material.snackbar.Snackbar import com.keylesspalace.tusky.R import com.keylesspalace.tusky.components.report.ReportViewModel @@ -33,18 +31,12 @@ import kotlinx.android.synthetic.main.fragment_report_note.* import java.io.IOException import javax.inject.Inject -class ReportNoteFragment : Fragment(), Injectable { +class ReportNoteFragment : Fragment(R.layout.fragment_report_note), Injectable { @Inject lateinit var viewModelFactory: ViewModelFactory - private val viewModel: ReportViewModel by viewModels({ requireActivity() }) { viewModelFactory } - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_report_note, container, false) - } + private val viewModel: ReportViewModel by activityViewModels { viewModelFactory } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { fillViews() diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportStatusesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportStatusesFragment.kt index 116a0c0fe..70bc694dc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportStatusesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportStatusesFragment.kt @@ -16,13 +16,11 @@ package com.keylesspalace.tusky.components.report.fragments import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import androidx.core.app.ActivityOptionsCompat import androidx.core.view.ViewCompat import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels +import androidx.fragment.app.activityViewModels import androidx.preference.PreferenceManager import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager @@ -50,7 +48,7 @@ import com.keylesspalace.tusky.viewdata.AttachmentViewData import kotlinx.android.synthetic.main.fragment_report_statuses.* import javax.inject.Inject -class ReportStatusesFragment : Fragment(), Injectable, AdapterHandler { +class ReportStatusesFragment : Fragment(R.layout.fragment_report_statuses), Injectable, AdapterHandler { @Inject lateinit var viewModelFactory: ViewModelFactory @@ -58,10 +56,9 @@ class ReportStatusesFragment : Fragment(), Injectable, AdapterHandler { @Inject lateinit var accountManager: AccountManager - private val viewModel: ReportViewModel by viewModels({ requireActivity() }) { viewModelFactory } + private val viewModel: ReportViewModel by activityViewModels { viewModelFactory } private lateinit var adapter: StatusesAdapter - private lateinit var layoutManager: LinearLayoutManager private var snackbarErrorRetry: Snackbar? = null @@ -89,12 +86,6 @@ class ReportStatusesFragment : Fragment(), Injectable, AdapterHandler { } } - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_report_statuses, container, false) - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { handleClicks() initStatusesView() @@ -127,8 +118,7 @@ class ReportStatusesFragment : Fragment(), Injectable, AdapterHandler { viewModel.statusViewState, this) recyclerView.addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)) - layoutManager = LinearLayoutManager(requireContext()) - recyclerView.layoutManager = layoutManager + recyclerView.layoutManager = LinearLayoutManager(requireContext()) recyclerView.adapter = adapter (recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchFragment.kt index c5929c43a..83eb30910 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchFragment.kt @@ -1,11 +1,9 @@ package com.keylesspalace.tusky.components.search.fragments import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels +import androidx.fragment.app.activityViewModels import androidx.lifecycle.LiveData import androidx.paging.PagedList import androidx.paging.PagedListAdapter @@ -26,13 +24,13 @@ import com.keylesspalace.tusky.util.* import kotlinx.android.synthetic.main.fragment_search.* import javax.inject.Inject -abstract class SearchFragment : Fragment(), +abstract class SearchFragment : Fragment(R.layout.fragment_search), LinkListener, Injectable, SwipeRefreshLayout.OnRefreshListener { @Inject lateinit var viewModelFactory: ViewModelFactory - protected val viewModel: SearchViewModel by viewModels({ requireActivity() }) { viewModelFactory } + protected val viewModel: SearchViewModel by activityViewModels { viewModelFactory } private var snackbarErrorRetry: Snackbar? = null @@ -43,12 +41,7 @@ abstract class SearchFragment : Fragment(), abstract val data: LiveData> protected lateinit var adapter: PagedListAdapter - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_search, container, false) - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) initAdapter() setupSwipeRefreshLayout() subscribeObservables()