Skip to content

Commit

Permalink
Follow-up to #1232: Fix crash on device rotation
Browse files Browse the repository at this point in the history
Migrated from ViewPager to ViewPager2
  • Loading branch information
mtotschnig committed Oct 26, 2023
1 parent 78622c5 commit 943c92c
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.Menu
import android.widget.AdapterView
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import com.evernote.android.state.State
import org.apache.commons.csv.CSVRecord
import org.totschnig.myexpenses.R
Expand All @@ -27,8 +26,6 @@ import javax.inject.Inject


class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
@State
var dataReady = false

@State
var mUsageRecorded = false
Expand All @@ -39,10 +36,6 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
@Inject
lateinit var repository: Repository

private fun setDataReady() {
dataReady = true
mSectionsPagerAdapter.notifyDataSetChanged()
}

private val csvImportViewModel: CsvImportViewModel by viewModels()

Expand All @@ -56,7 +49,7 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
}

override fun onPrepareOptionsMenu(menu: Menu): Boolean {
val allowed = parseFragment.isReady && idle
val allowed = parseFragment?.isReady == true && idle
menu.findItem(R.id.PARSE_COMMAND)?.isEnabled = allowed
menu.findItem(R.id.IMPORT_COMMAND)?.isEnabled = allowed
super.onPrepareOptionsMenu(menu)
Expand Down Expand Up @@ -86,33 +79,25 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
if (shouldGoBack()) super.onBackPressed()
}

override fun setupTabs() {
//we only add the first tab, the second one once data has been parsed
addTab(0)
if (dataReady) {
addTab(1)
}
override fun createFragment(position: Int) = when(position) {
0 -> CsvImportParseFragment.newInstance()
1 -> CsvImportDataFragment.newInstance()
else -> throw IllegalArgumentException()
}

private fun addTab(index: Int) {
when (index) {
0 -> mSectionsPagerAdapter.addFragment(
CsvImportParseFragment.newInstance(),
getString(R.string.menu_parse)
)
1 -> mSectionsPagerAdapter.addFragment(
CsvImportDataFragment.newInstance(),
getString(R.string.csv_import_preview)
)
}
override fun getItemCount() = 2

override fun getTitle(position: Int) = when(position) {
0 -> getString(R.string.menu_parse)
1 -> getString(R.string.csv_import_preview)
else -> throw IllegalArgumentException()
}


override fun onPositive(args: Bundle, checked: Boolean) {
super.onPositive(args, checked)
if (args.getInt(ConfirmationDialogFragment.KEY_COMMAND_POSITIVE) == R.id.SET_HEADER_COMMAND) {
(supportFragmentManager.findFragmentByTag(
mSectionsPagerAdapter.getFragmentName(1)
) as? CsvImportDataFragment)?.setHeader(args.getInt(CsvImportDataFragment.KEY_HEADER_LINE_POSITION))
dataFragment!!.setHeader(args.getInt(CsvImportDataFragment.KEY_HEADER_LINE_POSITION))
}
}

Expand All @@ -132,21 +117,8 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
showProgress()
csvImportViewModel.parseFile(uri, delimiter, encoding).observe(this) { result ->
hideProgress()
result.onSuccess { data ->
if (data.isNotEmpty()) {
if (!dataReady) {
addTab(1)
setDataReady()
}
(supportFragmentManager.findFragmentByTag(
mSectionsPagerAdapter.getFragmentName(1)
) as? CsvImportDataFragment)?.let {
it.setData(data)
binding.viewPager.currentItem = 1
}
} else {
showSnackBar(R.string.parse_error_no_data_found)
}
result.onSuccess {
binding.viewPager.currentItem = 1
}.onFailure {
showSnackBar(
when (it) {
Expand All @@ -168,9 +140,9 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
dataSet,
columnToFieldMap,
dateFormat,
parseFragment.autoFillCategories,
parseFragment!!.autoFillCategories,
AccountConfiguration(accountId, currency, accountType),
parseFragment.uri!!
parseFragment!!.uri!!
).observe(this) { result ->
hideProgress()
result.onSuccess { resultList ->
Expand Down Expand Up @@ -206,26 +178,31 @@ class CsvImportActivity : TabbedActivity(), ConfirmationDialogListener {
}
}

private val parseFragment: CsvImportParseFragment
private val dataFragment: CsvImportDataFragment?
get() = supportFragmentManager.findFragmentByTag(
mSectionsPagerAdapter.getFragmentName(1)
) as CsvImportDataFragment?

private val parseFragment: CsvImportParseFragment?
get() = supportFragmentManager.findFragmentByTag(
mSectionsPagerAdapter.getFragmentName(0)
) as CsvImportParseFragment
) as? CsvImportParseFragment

val accountId: Long
get() {
return parseFragment.getSelectedAccountId()
return parseFragment!!.getSelectedAccountId()
}

val currency: String
get() = parseFragment.getSelectedCurrency()
get() = parseFragment!!.getSelectedCurrency()

val dateFormat: QifDateFormat
get() {
return parseFragment.dateFormat
return parseFragment!!.dateFormat
}

val accountType: AccountType
get() {
return parseFragment.accountType
return parseFragment!!.accountType
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.totschnig.myexpenses.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import org.totschnig.myexpenses.databinding.ActivityWithTabsBinding
import org.totschnig.myexpenses.ui.FragmentPagerAdapter

abstract class TabbedActivity : ProtectedFragmentActivity() {
lateinit var binding: ActivityWithTabsBinding

lateinit var mSectionsPagerAdapter: SectionsPagerAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityWithTabsBinding.inflate(
layoutInflater
)
setContentView(binding.root)
setupToolbar()
mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
binding.viewPager.adapter = mSectionsPagerAdapter
TabLayoutMediator(binding.tabs, binding.viewPager) { tab, position ->
tab.text = getTitle(position)
}.attach()
}

abstract fun getTitle(position: Int): CharSequence

abstract fun createFragment(position: Int): Fragment

abstract fun getItemCount(): Int

/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentStateAdapter(fm, lifecycle) {

fun getFragmentName(currentPosition: Int): String {
//https://stackoverflow.com/a/61178226/1199911
return "f" + getItemId(currentPosition)
}

override fun getItemCount() = this@TabbedActivity.getItemCount()

override fun createFragment(position: Int) = this@TabbedActivity.createFragment(position)
}

override val snackBarContainerId: Int
get() = binding.viewPager.id
}
Loading

0 comments on commit 943c92c

Please sign in to comment.