From df581f4c51a164639b146166139d713a8c0f71d8 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 13 Jan 2018 21:59:20 +0100 Subject: [PATCH] Use observer pattern for label change --- app/build.gradle | 2 +- .../apps/abit/AbstractItemListFragment.kt | 2 - .../java/ch/dissem/apps/abit/ListHolder.kt | 2 - .../java/ch/dissem/apps/abit/MainActivity.kt | 63 +++++---------- .../dissem/apps/abit/MessageDetailActivity.kt | 8 +- .../dissem/apps/abit/MessageListFragment.kt | 78 +++++-------------- .../ch/dissem/apps/abit/service/Singleton.kt | 51 +++++++++++- .../ch/dissem/apps/abit/util/Observable.kt | 37 +++++++++ 8 files changed, 130 insertions(+), 113 deletions(-) create mode 100644 app/src/main/java/ch/dissem/apps/abit/util/Observable.kt diff --git a/app/build.gradle b/app/build.gradle index 4b08511..ed4fd3f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,7 @@ android { buildToolsVersion "26.0.2" defaultConfig { - applicationId "ch.dissem.apps." + appName.toLowerCase() + applicationId "ch.dissem.apps.${appName.toLowerCase()}" minSdkVersion 19 targetSdkVersion 27 versionCode 17 diff --git a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt index a9cd265..2e919d2 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.kt @@ -127,8 +127,6 @@ abstract class AbstractItemListFragment : ListFragment(), ListHolder { activatedPosition = position } - override var currentLabel: L? = null - override fun showPreviousList() = false /** diff --git a/app/src/main/java/ch/dissem/apps/abit/ListHolder.kt b/app/src/main/java/ch/dissem/apps/abit/ListHolder.kt index 6e57319..a72ad87 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ListHolder.kt +++ b/app/src/main/java/ch/dissem/apps/abit/ListHolder.kt @@ -24,7 +24,5 @@ interface ListHolder { fun setActivateOnItemClick(activateOnItemClick: Boolean) - var currentLabel: L? - fun showPreviousList(): Boolean } diff --git a/app/src/main/java/ch/dissem/apps/abit/MainActivity.kt b/app/src/main/java/ch/dissem/apps/abit/MainActivity.kt index 9296101..2e288dd 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MainActivity.kt +++ b/app/src/main/java/ch/dissem/apps/abit/MainActivity.kt @@ -30,6 +30,7 @@ import ch.dissem.apps.abit.drawer.ProfileSelectionListener import ch.dissem.apps.abit.listener.ListSelectionListener import ch.dissem.apps.abit.repository.AndroidLabelRepository.Companion.LABEL_ARCHIVE import ch.dissem.apps.abit.service.Singleton +import ch.dissem.apps.abit.service.Singleton.currentLabel import ch.dissem.apps.abit.synchronization.SyncAdapter import ch.dissem.apps.abit.util.Labels import ch.dissem.apps.abit.util.NetworkUtils @@ -89,9 +90,6 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { var hasDetailPane: Boolean = false private set - var selectedLabel: Label? = null - private set - private lateinit var bmc: BitmessageContext private lateinit var accountHeader: AccountHeader @@ -280,14 +278,16 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { uiThread { if (intent.hasExtra(EXTRA_SHOW_LABEL)) { - selectedLabel = intent.getSerializableExtra(EXTRA_SHOW_LABEL) as Label - } else if (selectedLabel == null) { - selectedLabel = labels[0] + currentLabel.value = intent.getSerializableExtra(EXTRA_SHOW_LABEL) as Label + } else if (currentLabel.value == null) { + currentLabel.value = labels[0] } for (label in labels) { addLabelEntry(label) } - drawer.setSelection(selectedLabel?.id as Long) + currentLabel.value?.let { + drawer.setSelection(it.id as Long) + } updateUnread() } } @@ -295,16 +295,9 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { override fun onBackPressed() { val listFragment = supportFragmentManager.findFragmentById(R.id.item_list) - if (listFragment is ListHolder<*>) { - val listHolder = listFragment as ListHolder<*> - if (listHolder.showPreviousList()) { - drawer.getDrawerItem(listHolder.currentLabel)?.let { - drawer.setSelection(it) - } - return - } + if (listFragment !is ListHolder<*> || !listFragment.showPreviousList()) { + super.onBackPressed() } - super.onBackPressed() } private inner class DrawerItemClickListener : Drawer.OnDrawerItemClickListener { @@ -312,13 +305,9 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { val itemList = supportFragmentManager.findFragmentById(R.id.item_list) val tag = item.tag if (tag is Label) { - selectedLabel = tag - if (itemList is MessageListFragment) { - itemList.updateList(tag) - } else { - val listFragment = MessageListFragment() - changeList(listFragment) - listFragment.updateList(tag) + currentLabel.value = tag + if (itemList !is MessageListFragment) { + changeList(MessageListFragment()) } return false } else if (item is Nameable<*>) { @@ -347,33 +336,23 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { } } - override fun onSaveInstanceState(savedInstanceState: Bundle) { - super.onSaveInstanceState(savedInstanceState) - savedInstanceState.putSerializable("selectedLabel", selectedLabel) - } - - override fun onRestoreInstanceState(savedInstanceState: Bundle) { - selectedLabel = savedInstanceState.getSerializable("selectedLabel") as? Label - - selectedLabel?.let { selectedLabel -> - drawer.getDrawerItem(selectedLabel)?.let { selectedItem -> - drawer.setSelection(selectedItem) - } - } - super.onRestoreInstanceState(savedInstanceState) - } - override fun onResume() { updateUnread() if (Preferences.isFullNodeActive(this) && Preferences.isConnectionAllowed(this@MainActivity)) { NetworkUtils.enableNode(this, false) } Singleton.getMessageListener(this).resetNotification() + currentLabel.addObserver(this) { label -> + if (label != null) { + drawer.setSelection(label.id as Long) + } + } active = true super.onResume() } override fun onPause() { + currentLabel.removeObserver(this) super.onPause() active = false } @@ -471,9 +450,7 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { // for the selected item ID. val detailIntent = when (item) { is Plaintext -> { - Intent(this, MessageDetailActivity::class.java).apply { - putExtra(EXTRA_SHOW_LABEL, selectedLabel) - } + Intent(this, MessageDetailActivity::class.java) } is BitmessageAddress -> Intent(this, AddressDetailActivity::class.java) else -> throw IllegalArgumentException("Plaintext or BitmessageAddress expected, but was ${item::class.simpleName}") @@ -521,7 +498,7 @@ class MainActivity : AppCompatActivity(), ListSelectionListener { * Runs the given code in the main activity context, if it currently exists. Otherwise, * it's ignored. */ - fun apply(run: MainActivity.() -> Unit){ + fun apply(run: MainActivity.() -> Unit) { instance?.get()?.let { run.invoke(it) } } } diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageDetailActivity.kt b/app/src/main/java/ch/dissem/apps/abit/MessageDetailActivity.kt index f8586cb..62d2cec 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageDetailActivity.kt +++ b/app/src/main/java/ch/dissem/apps/abit/MessageDetailActivity.kt @@ -5,8 +5,6 @@ import android.os.Bundle import android.support.v4.app.NavUtils import android.view.MenuItem -import ch.dissem.bitmessage.entity.valueobject.Label - /** * An activity representing a single Message detail screen. This @@ -18,7 +16,6 @@ import ch.dissem.bitmessage.entity.valueobject.Label * more than a [MessageDetailFragment]. */ class MessageDetailActivity : DetailActivity() { - private var label: Label? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -33,7 +30,6 @@ class MessageDetailActivity : DetailActivity() { // http://developer.android.com/guide/components/fragments.html // if (savedInstanceState == null) { - label = intent.getSerializableExtra(MainActivity.EXTRA_SHOW_LABEL) as Label? // Create the detail fragment and add it to the activity // using a fragment transaction. val arguments = Bundle() @@ -49,9 +45,7 @@ class MessageDetailActivity : DetailActivity() { override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { android.R.id.home -> { - val parentIntent = Intent(this, MainActivity::class.java) - parentIntent.putExtra(MainActivity.EXTRA_SHOW_LABEL, label) - NavUtils.navigateUpTo(this, parentIntent) + NavUtils.navigateUpTo(this, Intent(this, MainActivity::class.java)) true } else -> super.onOptionsItemSelected(item) diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.kt b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.kt index a579cd3..b63e991 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.kt @@ -32,6 +32,7 @@ import ch.dissem.apps.abit.adapter.SwipeableMessageAdapter import ch.dissem.apps.abit.listener.ListSelectionListener import ch.dissem.apps.abit.repository.AndroidMessageRepository import ch.dissem.apps.abit.service.Singleton +import ch.dissem.apps.abit.service.Singleton.currentLabel import ch.dissem.apps.abit.util.FabUtils import ch.dissem.bitmessage.entity.Plaintext import ch.dissem.bitmessage.entity.valueobject.Label @@ -87,8 +88,6 @@ class MessageListFragment : Fragment(), ListHolder