Minor code improvements and bug fixes

This commit is contained in:
Christian Basler 2017-08-29 21:14:50 +02:00
parent cc18f34161
commit 415107a6c8
6 changed files with 86 additions and 96 deletions

View File

@ -58,12 +58,11 @@ class AddressDetailFragment : Fragment() {
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.address, menu) inflater.inflate(R.menu.address, menu)
val activity = activity val ctx = activity
Drawables.addIcon(activity, menu, R.id.write_message, GoogleMaterial.Icon.gmd_mail) Drawables.addIcon(ctx, menu, R.id.write_message, GoogleMaterial.Icon.gmd_mail)
Drawables.addIcon(activity, menu, R.id.share, GoogleMaterial.Icon.gmd_share) Drawables.addIcon(ctx, menu, R.id.share, GoogleMaterial.Icon.gmd_share)
Drawables.addIcon(activity, menu, R.id.delete, GoogleMaterial.Icon.gmd_delete) Drawables.addIcon(ctx, menu, R.id.delete, GoogleMaterial.Icon.gmd_delete)
Drawables.addIcon(activity, menu, R.id.export, Drawables.addIcon(ctx, menu, R.id.export, CommunityMaterial.Icon.cmd_export).isVisible = item?.privateKey != null
CommunityMaterial.Icon.cmd_export).isVisible = item != null && item!!.privateKey != null
super.onCreateOptionsMenu(menu, inflater) super.onCreateOptionsMenu(menu, inflater)
} }
@ -187,9 +186,8 @@ class AddressDetailFragment : Fragment() {
override fun onPause() { override fun onPause() {
item?.let { item -> item?.let { item ->
Singleton.getAddressRepository(context).save(item) Singleton.getAddressRepository(context).save(item)
val mainActivity = MainActivity.getInstance() if (item.privateKey != null) {
if (mainActivity != null && item.privateKey != null) { MainActivity.getInstance()?.updateIdentityEntry(item)
mainActivity.updateIdentityEntry(item)
} }
} }
super.onPause() super.onPause()

View File

@ -56,7 +56,7 @@ class ComposeMessageFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (arguments != null) { arguments?.let { arguments ->
var id = arguments.getSerializable(EXTRA_IDENTITY) as? BitmessageAddress var id = arguments.getSerializable(EXTRA_IDENTITY) as? BitmessageAddress
if (context != null && (id == null || id.privateKey == null)) { if (context != null && (id == null || id.privateKey == null)) {
id = Singleton.getIdentity(context) id = Singleton.getIdentity(context)
@ -81,9 +81,9 @@ class ComposeMessageFragment : Fragment() {
if (arguments.containsKey(EXTRA_PARENT)) { if (arguments.containsKey(EXTRA_PARENT)) {
parent = arguments.getSerializable(EXTRA_PARENT) as Plaintext parent = arguments.getSerializable(EXTRA_PARENT) as Plaintext
} }
} else { } ?: {
throw IllegalStateException("No identity set for ComposeMessageFragment") throw IllegalStateException("No identity set for ComposeMessageFragment")
} }.invoke()
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
@ -109,22 +109,20 @@ class ComposeMessageFragment : Fragment() {
// leave current selection // leave current selection
} }
} }
if (recipient != null) { recipient?.let { recipient_input.setText(it.toString()) }
recipient_input.setText(recipient.toString())
}
} }
subject_input.setText(subject) subject_input.setText(subject)
body_input.setText(content) body_input.setText(content)
if (recipient == null) { when {
recipient_input.requestFocus() recipient == null -> recipient_input.requestFocus()
} else if (subject.isEmpty()) { subject.isEmpty() -> subject_input.requestFocus()
subject_input.requestFocus() else -> {
} else {
body_input.requestFocus() body_input.requestFocus()
body_input.setSelection(0) body_input.setSelection(0)
} }
} }
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater) {
inflater.inflate(R.menu.compose, menu) inflater.inflate(R.menu.compose, menu)

View File

@ -90,7 +90,7 @@ class CreateAddressActivity : AppCompatActivity() {
if (subscribe.isChecked) { if (subscribe.isChecked) {
bmc.addSubscribtion(bmAddress) bmc.addSubscribtion(bmAddress)
} }
if (pubkeyBytes != null) { pubkeyBytes?.let { pubkeyBytes ->
try { try {
val pubkeyStream = ByteArrayInputStream(pubkeyBytes) val pubkeyStream = ByteArrayInputStream(pubkeyBytes)
val stream = bmAddress.stream val stream = bmAddress.stream

View File

@ -33,7 +33,7 @@ class MessageDetailActivity : DetailActivity() {
// http://developer.android.com/guide/components/fragments.html // http://developer.android.com/guide/components/fragments.html
// //
if (savedInstanceState == null) { if (savedInstanceState == null) {
label = intent.getSerializableExtra(MainActivity.EXTRA_SHOW_LABEL) as Label label = intent.getSerializableExtra(MainActivity.EXTRA_SHOW_LABEL) as Label?
// Create the detail fragment and add it to the activity // Create the detail fragment and add it to the activity
// using a fragment transaction. // using a fragment transaction.
val arguments = Bundle() val arguments = Bundle()

View File

@ -90,7 +90,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
} }
override fun updateList(label: Label) { override fun updateList(label: Label) {
if (currentLabel != null && currentLabel != label) { if (currentLabel != null && currentLabel != label && currentLabel != backStack.peek()) {
backStack.push(currentLabel) backStack.push(currentLabel)
} }
if (!isResumed) { if (!isResumed) {
@ -123,9 +123,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
messageRepo.findMessageIds(label) messageRepo.findMessageIds(label)
.map { messageRepo.getMessage(it) } .map { messageRepo.getMessage(it) }
.forEach { message -> .forEach { message ->
uiThread { uiThread { swipeableMessageAdapter?.add(message) }
swipeableMessageAdapter?.add(message)
}
} }
} }
} }
@ -274,9 +272,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
messageRepo.remove(message) messageRepo.remove(message)
} }
uiThread { uiThread { updateList(currentLabel) }
updateList(currentLabel)
}
} }
return true return true
} }

View File

@ -26,32 +26,27 @@ import android.view.ViewGroup
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import ch.dissem.apps.abit.Identicon
import ch.dissem.apps.abit.R
import ch.dissem.apps.abit.repository.AndroidMessageRepository.Companion.LABEL_ARCHIVE
import ch.dissem.apps.abit.util.Assets
import ch.dissem.apps.abit.util.Strings.prepareMessageExtract
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.valueobject.Label
import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemAdapter import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemAdapter
import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemConstants import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemConstants
import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultAction import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemConstants.*
import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionMoveToSwipedDirection import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionMoveToSwipedDirection
import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionRemoveItem import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionRemoveItem
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractSwipeableItemViewHolder import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractSwipeableItemViewHolder
import com.h6ah4i.android.widget.advrecyclerview.utils.RecyclerViewAdapterUtils import com.h6ah4i.android.widget.advrecyclerview.utils.RecyclerViewAdapterUtils
import java.util.*
import java.util.LinkedList
import ch.dissem.apps.abit.Identicon
import ch.dissem.apps.abit.R
import ch.dissem.apps.abit.util.Assets
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.valueobject.Label
import ch.dissem.apps.abit.repository.AndroidMessageRepository.Companion.LABEL_ARCHIVE
import ch.dissem.apps.abit.util.Strings.prepareMessageExtract
/** /**
* Adapted from the basic swipeable example by Haruki Hasegawa. See * Adapted from the basic swipeable example by Haruki Hasegawa. See
* *
* @author Christian Basler * @author Christian Basler
* @see [ * @see [https://github.com/h6ah4i/android-advancedrecyclerview](https://github.com/h6ah4i/android-advancedrecyclerview)
* https://github.com/h6ah4i/android-advancedrecyclerview](https://github.com/h6ah4i/android-advancedrecyclerview)
*/ */
class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.ViewHolder>(), SwipeableItemAdapter<SwipeableMessageAdapter.ViewHolder>, SwipeableItemConstants { class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.ViewHolder>(), SwipeableItemAdapter<SwipeableMessageAdapter.ViewHolder>, SwipeableItemConstants {
@ -108,17 +103,13 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
} }
private fun onItemViewClick(v: View) { private fun onItemViewClick(v: View) {
if (eventListener != null) { eventListener?.onItemViewClicked(v)
eventListener!!.onItemViewClicked(v)
}
} }
private fun onSwipeableViewContainerClick(v: View) { private fun onSwipeableViewContainerClick(v: View) {
if (eventListener != null) { eventListener?.onItemViewClicked(
eventListener!!.onItemViewClicked(
RecyclerViewAdapterUtils.getParentViewHolderItemView(v)) RecyclerViewAdapterUtils.getParentViewHolderItemView(v))
} }
}
fun getItem(position: Int) = data[position] fun getItem(position: Int) = data[position]
@ -133,8 +124,9 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position] val item = data[position]
holder.apply {
if (activateOnItemClick) { if (activateOnItemClick) {
holder.container.setBackgroundResource( container.setBackgroundResource(
if (position == selectedPosition) if (position == selectedPosition)
R.drawable.bg_item_selected_state R.drawable.bg_item_selected_state
else else
@ -144,41 +136,44 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
// set listeners // set listeners
// (if the item is *pinned*, click event comes to the itemView) // (if the item is *pinned*, click event comes to the itemView)
holder.itemView.setOnClickListener(itemViewOnClickListener) itemView.setOnClickListener(itemViewOnClickListener)
// (if the item is *not pinned*, click event comes to the container) // (if the item is *not pinned*, click event comes to the container)
holder.container.setOnClickListener(swipeableViewContainerOnClickListener) container.setOnClickListener(swipeableViewContainerOnClickListener)
// set data // set data
holder.avatar.setImageDrawable(Identicon(item.from)) avatar.setImageDrawable(Identicon(item.from))
holder.status.setImageResource(Assets.getStatusDrawable(item.status)) status.setImageResource(Assets.getStatusDrawable(item.status))
holder.status.contentDescription = holder.status.context.getString(Assets.getStatusString(item.status)) status.contentDescription = holder.status.context.getString(Assets.getStatusString(item.status))
holder.sender.text = item.from.toString() sender.text = item.from.toString()
holder.subject.text = prepareMessageExtract(item.subject) subject.text = prepareMessageExtract(item.subject)
holder.extract.text = prepareMessageExtract(item.text) extract.text = prepareMessageExtract(item.text)
if (item.isUnread()) { if (item.isUnread()) {
holder.sender.typeface = Typeface.DEFAULT_BOLD sender.typeface = Typeface.DEFAULT_BOLD
holder.subject.typeface = Typeface.DEFAULT_BOLD subject.typeface = Typeface.DEFAULT_BOLD
} else { } else {
holder.sender.typeface = Typeface.DEFAULT sender.typeface = Typeface.DEFAULT
holder.subject.typeface = Typeface.DEFAULT subject.typeface = Typeface.DEFAULT
}
} }
} }
override fun getItemCount() = data.size override fun getItemCount() = data.size
override fun onGetSwipeReactionType(holder: ViewHolder, position: Int, x: Int, y: Int): Int { override fun onGetSwipeReactionType(holder: ViewHolder, position: Int, x: Int, y: Int): Int {
return if (label === LABEL_ARCHIVE || label!!.type == Label.Type.TRASH) { return if (label === LABEL_ARCHIVE || label?.type == Label.Type.TRASH) {
SwipeableItemConstants.REACTION_CAN_SWIPE_LEFT or SwipeableItemConstants.REACTION_CAN_NOT_SWIPE_RIGHT_WITH_RUBBER_BAND_EFFECT REACTION_CAN_SWIPE_LEFT or REACTION_CAN_NOT_SWIPE_RIGHT_WITH_RUBBER_BAND_EFFECT
} else SwipeableItemConstants.REACTION_CAN_SWIPE_BOTH_H } else {
REACTION_CAN_SWIPE_BOTH_H
}
} }
@SuppressLint("SwitchIntDef") @SuppressLint("SwitchIntDef")
override fun onSetSwipeBackground(holder: ViewHolder, position: Int, type: Int) { override fun onSetSwipeBackground(holder: ViewHolder, position: Int, type: Int) {
var bgRes = 0 var bgRes = 0
when (type) { when (type) {
SwipeableItemConstants.DRAWABLE_SWIPE_NEUTRAL_BACKGROUND -> bgRes = R.drawable.bg_swipe_item_neutral DRAWABLE_SWIPE_NEUTRAL_BACKGROUND -> bgRes = R.drawable.bg_swipe_item_neutral
SwipeableItemConstants.DRAWABLE_SWIPE_LEFT_BACKGROUND -> bgRes = R.drawable.bg_swipe_item_left DRAWABLE_SWIPE_LEFT_BACKGROUND -> bgRes = R.drawable.bg_swipe_item_left
SwipeableItemConstants.DRAWABLE_SWIPE_RIGHT_BACKGROUND -> if (label === LABEL_ARCHIVE || label!!.type == Label.Type.TRASH) { DRAWABLE_SWIPE_RIGHT_BACKGROUND -> if (label === LABEL_ARCHIVE || label?.type == Label.Type.TRASH) {
bgRes = R.drawable.bg_swipe_item_neutral bgRes = R.drawable.bg_swipe_item_neutral
} else { } else {
bgRes = R.drawable.bg_swipe_item_right bgRes = R.drawable.bg_swipe_item_right
@ -188,12 +183,11 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
} }
@SuppressLint("SwitchIntDef") @SuppressLint("SwitchIntDef")
override fun onSwipeItem(holder: ViewHolder, position: Int, result: Int): SwipeResultAction? { override fun onSwipeItem(holder: ViewHolder, position: Int, result: Int) =
when (result) { when (result) {
SwipeableItemConstants.RESULT_SWIPED_RIGHT -> return SwipeRightResultAction(this, position) RESULT_SWIPED_RIGHT -> SwipeRightResultAction(this, position)
SwipeableItemConstants.RESULT_SWIPED_LEFT -> return SwipeLeftResultAction(this, position) RESULT_SWIPED_LEFT -> SwipeLeftResultAction(this, position)
else -> return null else -> null
}
} }
fun setSelectedPosition(selectedPosition: Int) { fun setSelectedPosition(selectedPosition: Int) {
@ -210,8 +204,10 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
override fun onPerformAction() { override fun onPerformAction() {
super.onPerformAction() super.onPerformAction()
adapter?.data?.removeAt(position) adapter?.apply {
adapter?.notifyItemRemoved(position) data.removeAt(position)
notifyItemRemoved(position)
}
} }
override fun onSlideAnimationEnd() { override fun onSlideAnimationEnd() {
@ -233,8 +229,10 @@ class SwipeableMessageAdapter : RecyclerView.Adapter<SwipeableMessageAdapter.Vie
override fun onPerformAction() { override fun onPerformAction() {
super.onPerformAction() super.onPerformAction()
adapter?.data?.removeAt(position) adapter?.apply {
adapter?.notifyItemRemoved(position) data.removeAt(position)
notifyItemRemoved(position)
}
} }
override fun onSlideAnimationEnd() { override fun onSlideAnimationEnd() {