From fb72356467a2a9daced445cbf687df90b6e8318f Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 24 Feb 2018 07:39:06 +0100 Subject: [PATCH] Retain content of compose view on orientation change --- .../apps/abit/ComposeMessageActivity.kt | 38 ++++++++++++------- .../apps/abit/ComposeMessageFragment.kt | 9 +++++ .../apps/abit/adapter/ContactAdapter.kt | 9 +++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt index 618504a..ea9828c 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt +++ b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageActivity.kt @@ -43,13 +43,15 @@ class ComposeMessageActivity : AppCompatActivity() { setHomeButtonEnabled(false) } - // Display the fragment as the main content. - val fragment = ComposeMessageFragment() - fragment.arguments = intent.extras - supportFragmentManager - .beginTransaction() - .replace(R.id.content, fragment) - .commit() + if (supportFragmentManager.findFragmentById(R.id.content) == null) { + // Display the fragment as the main content. + val fragment = ComposeMessageFragment() + fragment.arguments = intent.extras + supportFragmentManager + .beginTransaction() + .replace(R.id.content, fragment) + .commit() + } } companion object { @@ -63,10 +65,13 @@ class ComposeMessageActivity : AppCompatActivity() { const val EXTRA_PARENT = "ch.dissem.abit.Message.PARENT" fun launchReplyTo(fragment: Fragment, item: Plaintext) = - fragment.startActivity(getReplyIntent( - ctx = fragment.activity ?: throw IllegalStateException("Fragment not attached to an activity"), - item = item - )) + fragment.startActivity( + getReplyIntent( + ctx = fragment.activity + ?: throw IllegalStateException("Fragment not attached to an activity"), + item = item + ) + ) fun launchReplyTo(activity: Activity, item: Plaintext) = activity.startActivity(getReplyIntent(activity, item)) @@ -90,15 +95,20 @@ class ComposeMessageActivity : AppCompatActivity() { } replyIntent.putExtra(EXTRA_PARENT, item) item.subject?.let { subject -> - val prefix: String = if (subject.length >= 3 && subject.substring(0, 3).equals("RE:", ignoreCase = true)) { + val prefix: String = if (subject.length >= 3 && subject.substring(0, 3).equals( + "RE:", + ignoreCase = true + )) { "" } else { "RE: " } replyIntent.putExtra(EXTRA_SUBJECT, prefix + subject) } - replyIntent.putExtra(EXTRA_CONTENT, - "\n\n------------------------------------------------------\n" + item.text!!) + replyIntent.putExtra( + EXTRA_CONTENT, + "\n\n------------------------------------------------------\n" + item.text!! + ) return replyIntent } } diff --git a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt index c248aef..dce64fa 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt +++ b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.kt @@ -63,6 +63,7 @@ class ComposeMessageFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + retainInstance = true arguments?.apply { val draft = getSerializable(EXTRA_DRAFT) as Plaintext? if (draft != null) { @@ -266,6 +267,14 @@ class ComposeMessageFragment : Fragment() { super.onPause() } + override fun onDestroyView() { + identity = sender_input.selectedItem as BitmessageAddress + // recipient is set when one is selected + subject = subject_input.text?.toString() ?: "" + content = body_input.text?.toString() ?: "" + super.onDestroyView() + } + private fun send() { val ctx = activity ?: throw IllegalStateException("Fragment is not attached to an activity") if (recipient == null) { diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/ContactAdapter.kt b/app/src/main/java/ch/dissem/apps/abit/adapter/ContactAdapter.kt index c32e44d..8aa73f4 100644 --- a/app/src/main/java/ch/dissem/apps/abit/adapter/ContactAdapter.kt +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/ContactAdapter.kt @@ -114,6 +114,13 @@ class ContactAdapter( }.invoke() } + if (newValues.isEmpty()) { + try { + newValues.add(BitmessageAddress(prefix.toString())) + } catch (_: Exception) { + } + } + results.values = newValues results.count = newValues.size } else { @@ -134,4 +141,6 @@ class ContactAdapter( } } } + + fun indexOf(element: BitmessageAddress) = originalData.indexOf(element) }