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

View File

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

View File

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

View File

@ -33,7 +33,7 @@ class MessageDetailActivity : DetailActivity() {
// http://developer.android.com/guide/components/fragments.html
//
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
// using a fragment transaction.
val arguments = Bundle()

View File

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

View File

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