Nicer labels
This commit is contained in:
		@@ -32,9 +32,7 @@ import ch.dissem.apps.abit.repository.AndroidLabelRepository.Companion.LABEL_ARC
 | 
			
		||||
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
 | 
			
		||||
import ch.dissem.apps.abit.util.Preferences
 | 
			
		||||
import ch.dissem.apps.abit.util.*
 | 
			
		||||
import ch.dissem.bitmessage.BitmessageContext
 | 
			
		||||
import ch.dissem.bitmessage.entity.BitmessageAddress
 | 
			
		||||
import ch.dissem.bitmessage.entity.Plaintext
 | 
			
		||||
@@ -404,8 +402,8 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
 | 
			
		||||
            .withIdentifier(label.id as Long)
 | 
			
		||||
            .withName(label.toString())
 | 
			
		||||
            .withTag(label)
 | 
			
		||||
            .withIcon(Labels.getIcon(label))
 | 
			
		||||
            .withIconColor(Labels.getColor(label))
 | 
			
		||||
            .withIcon(label.getIcon())
 | 
			
		||||
            .withIconColor(label.getColor(0xFF000000.toInt()))
 | 
			
		||||
        drawer.addItemAtPosition(item, drawer.drawerItems.size - 3)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,7 @@ import android.text.util.Linkify.WEB_URLS
 | 
			
		||||
import android.view.*
 | 
			
		||||
import android.widget.ImageView
 | 
			
		||||
import android.widget.TextView
 | 
			
		||||
import ch.dissem.apps.abit.adapter.LabelAdapter
 | 
			
		||||
import ch.dissem.apps.abit.service.Singleton
 | 
			
		||||
import ch.dissem.apps.abit.util.Assets
 | 
			
		||||
import ch.dissem.apps.abit.util.Constants.BITMESSAGE_ADDRESS_PATTERN
 | 
			
		||||
@@ -259,40 +260,6 @@ class MessageDetailFragment : Fragment() {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class LabelAdapter internal constructor(private val ctx: Context, labels: Set<Label>) :
 | 
			
		||||
        RecyclerView.Adapter<LabelAdapter.ViewHolder>() {
 | 
			
		||||
 | 
			
		||||
        private val labels = labels.toMutableList()
 | 
			
		||||
 | 
			
		||||
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LabelAdapter.ViewHolder {
 | 
			
		||||
            val context = parent.context
 | 
			
		||||
            val inflater = LayoutInflater.from(context)
 | 
			
		||||
 | 
			
		||||
            // Inflate the custom layout
 | 
			
		||||
            val contactView = inflater.inflate(R.layout.item_label, parent, false)
 | 
			
		||||
 | 
			
		||||
            // Return a new holder instance
 | 
			
		||||
            return ViewHolder(contactView)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Involves populating data into the item through holder
 | 
			
		||||
        override fun onBindViewHolder(viewHolder: LabelAdapter.ViewHolder, position: Int) {
 | 
			
		||||
            // Get the data model based on position
 | 
			
		||||
            val label = labels[position]
 | 
			
		||||
 | 
			
		||||
            viewHolder.icon.icon?.color(Labels.getColor(label))
 | 
			
		||||
            viewHolder.icon.icon?.icon(Labels.getIcon(label))
 | 
			
		||||
            viewHolder.label.text = Labels.getText(label, ctx)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        override fun getItemCount() = labels.size
 | 
			
		||||
 | 
			
		||||
        internal class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
 | 
			
		||||
            var icon = itemView.findViewById<IconicsImageView>(R.id.icon)!!
 | 
			
		||||
            var label = itemView.findViewById<TextView>(R.id.label)!!
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    companion object {
 | 
			
		||||
        /**
 | 
			
		||||
         * The fragment argument representing the item ID that this fragment
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,61 @@
 | 
			
		||||
package ch.dissem.apps.abit.adapter
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.content.res.ColorStateList
 | 
			
		||||
import android.os.Build
 | 
			
		||||
import android.support.annotation.ColorInt
 | 
			
		||||
import android.support.v7.widget.RecyclerView
 | 
			
		||||
import android.view.LayoutInflater
 | 
			
		||||
import android.view.View
 | 
			
		||||
import android.view.ViewGroup
 | 
			
		||||
import android.widget.TextView
 | 
			
		||||
import ch.dissem.apps.abit.R
 | 
			
		||||
import ch.dissem.apps.abit.util.getColor
 | 
			
		||||
import ch.dissem.apps.abit.util.getIcon
 | 
			
		||||
import ch.dissem.apps.abit.util.getText
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.Label
 | 
			
		||||
import com.mikepenz.iconics.view.IconicsImageView
 | 
			
		||||
import org.jetbrains.anko.backgroundColor
 | 
			
		||||
 | 
			
		||||
class LabelAdapter internal constructor(private val ctx: Context, labels: Collection<Label>) :
 | 
			
		||||
    RecyclerView.Adapter<LabelAdapter.ViewHolder>() {
 | 
			
		||||
 | 
			
		||||
    var labels = labels.toList()
 | 
			
		||||
 | 
			
		||||
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LabelAdapter.ViewHolder {
 | 
			
		||||
        val context = parent.context
 | 
			
		||||
        val inflater = LayoutInflater.from(context)
 | 
			
		||||
 | 
			
		||||
        // Inflate the custom layout
 | 
			
		||||
        val contactView = inflater.inflate(R.layout.item_label, parent, false)
 | 
			
		||||
 | 
			
		||||
        // Return a new holder instance
 | 
			
		||||
        return ViewHolder(contactView)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Involves populating data into the item through holder
 | 
			
		||||
    override fun onBindViewHolder(viewHolder: LabelAdapter.ViewHolder, position: Int) {
 | 
			
		||||
        // Get the data model based on position
 | 
			
		||||
        val label = labels[position]
 | 
			
		||||
 | 
			
		||||
        viewHolder.icon.icon?.icon(label.getIcon())
 | 
			
		||||
        viewHolder.label.text = label.getText(ctx)
 | 
			
		||||
        viewHolder.setBackground(label.getColor(0xFF607D8B.toInt()))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun getItemCount() = labels.size
 | 
			
		||||
 | 
			
		||||
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
 | 
			
		||||
        val background = itemView
 | 
			
		||||
        var icon = itemView.findViewById<IconicsImageView>(R.id.icon)!!
 | 
			
		||||
        var label = itemView.findViewById<TextView>(R.id.label)!!
 | 
			
		||||
 | 
			
		||||
        fun setBackground(@ColorInt color: Int) {
 | 
			
		||||
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 | 
			
		||||
                background.backgroundTintList = ColorStateList.valueOf(color)
 | 
			
		||||
            } else {
 | 
			
		||||
                background.backgroundColor = color
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -10,12 +10,13 @@ import com.mikepenz.iconics.typeface.IIcon
 | 
			
		||||
import ch.dissem.apps.abit.R
 | 
			
		||||
import ch.dissem.bitmessage.entity.valueobject.Label
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper class to help with translating the default labels, getting label colors and so on.
 | 
			
		||||
/*
 | 
			
		||||
 * Helper methods to help with translating the default labels, getting label colors and so on.
 | 
			
		||||
 */
 | 
			
		||||
object Labels {
 | 
			
		||||
    fun getText(label: Label, ctx: Context): String = getText(label.type, label.toString(), ctx)!!
 | 
			
		||||
 | 
			
		||||
fun Label.getText(ctx: Context): String = Labels.getText(type, toString(), ctx)!!
 | 
			
		||||
 | 
			
		||||
object Labels {
 | 
			
		||||
    fun getText(type: Label.Type?, alternative: String?, ctx: Context) = when (type) {
 | 
			
		||||
        Label.Type.INBOX -> ctx.getString(R.string.inbox)
 | 
			
		||||
        Label.Type.DRAFT -> ctx.getString(R.string.draft)
 | 
			
		||||
@@ -26,20 +27,20 @@ object Labels {
 | 
			
		||||
        Label.Type.BROADCAST -> ctx.getString(R.string.broadcasts)
 | 
			
		||||
        else -> alternative
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun getIcon(label: Label): IIcon = when (label.type) {
 | 
			
		||||
        Label.Type.INBOX -> GoogleMaterial.Icon.gmd_inbox
 | 
			
		||||
        Label.Type.DRAFT -> CommunityMaterial.Icon.cmd_file
 | 
			
		||||
        Label.Type.OUTBOX -> CommunityMaterial.Icon.cmd_inbox_arrow_up
 | 
			
		||||
        Label.Type.SENT -> CommunityMaterial.Icon.cmd_send
 | 
			
		||||
        Label.Type.BROADCAST -> CommunityMaterial.Icon.cmd_rss
 | 
			
		||||
        Label.Type.UNREAD -> GoogleMaterial.Icon.gmd_markunread_mailbox
 | 
			
		||||
        Label.Type.TRASH -> GoogleMaterial.Icon.gmd_delete
 | 
			
		||||
        else -> CommunityMaterial.Icon.cmd_label
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ColorInt
 | 
			
		||||
    fun getColor(label: Label) = if (label.type == null) {
 | 
			
		||||
        label.color
 | 
			
		||||
    } else 0xFF000000.toInt()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun Label.getIcon(): IIcon = when (type) {
 | 
			
		||||
    Label.Type.INBOX -> GoogleMaterial.Icon.gmd_inbox
 | 
			
		||||
    Label.Type.DRAFT -> CommunityMaterial.Icon.cmd_file
 | 
			
		||||
    Label.Type.OUTBOX -> CommunityMaterial.Icon.cmd_inbox_arrow_up
 | 
			
		||||
    Label.Type.SENT -> CommunityMaterial.Icon.cmd_send
 | 
			
		||||
    Label.Type.BROADCAST -> CommunityMaterial.Icon.cmd_rss
 | 
			
		||||
    Label.Type.UNREAD -> GoogleMaterial.Icon.gmd_markunread_mailbox
 | 
			
		||||
    Label.Type.TRASH -> GoogleMaterial.Icon.gmd_delete
 | 
			
		||||
    else -> CommunityMaterial.Icon.cmd_label
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ColorInt
 | 
			
		||||
fun Label.getColor(@ColorInt default: Int) = if (type == null) {
 | 
			
		||||
    color
 | 
			
		||||
} else default
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								app/src/main/res/drawable/bg_label.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								app/src/main/res/drawable/bg_label.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 | 
			
		||||
    <solid android:color="#000000"/>
 | 
			
		||||
    <corners android:radius="4dp"/>
 | 
			
		||||
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
 | 
			
		||||
</shape>
 | 
			
		||||
@@ -2,27 +2,32 @@
 | 
			
		||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    xmlns:app="http://schemas.android.com/apk/res-auto"
 | 
			
		||||
    xmlns:tools="http://schemas.android.com/tools"
 | 
			
		||||
    android:layout_width="wrap_content"
 | 
			
		||||
    android:layout_width="match_parent"
 | 
			
		||||
    android:layout_height="wrap_content"
 | 
			
		||||
    android:background="@drawable/bg_label"
 | 
			
		||||
    android:gravity="center_vertical"
 | 
			
		||||
    android:orientation="horizontal">
 | 
			
		||||
    android:orientation="horizontal"
 | 
			
		||||
    android:padding="2dp">
 | 
			
		||||
 | 
			
		||||
    <com.mikepenz.iconics.view.IconicsImageView
 | 
			
		||||
        android:id="@+id/icon"
 | 
			
		||||
        android:layout_margin="1dp"
 | 
			
		||||
        android:layout_width="16dp"
 | 
			
		||||
        android:layout_height="16dp"
 | 
			
		||||
        android:layout_alignParentStart="true"
 | 
			
		||||
        android:layout_alignParentTop="true"
 | 
			
		||||
        app:ico_color="@android:color/black"
 | 
			
		||||
        app:ico_icon="cmd-label" />
 | 
			
		||||
        app:iiv_color="@color/colorPrimaryDarkText"
 | 
			
		||||
        app:iiv_icon="cmd-label" />
 | 
			
		||||
 | 
			
		||||
    <TextView
 | 
			
		||||
        android:id="@+id/label"
 | 
			
		||||
        android:layout_width="wrap_content"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:paddingStart="8dp"
 | 
			
		||||
        android:paddingEnd="24dp"
 | 
			
		||||
        tools:text="Label"
 | 
			
		||||
        android:layout_alignParentTop="true"
 | 
			
		||||
        android:layout_toEndOf="@+id/icon" />
 | 
			
		||||
        android:layout_toEndOf="@+id/icon"
 | 
			
		||||
        android:paddingEnd="24dp"
 | 
			
		||||
        android:paddingStart="8dp"
 | 
			
		||||
        android:textColor="@color/colorPrimaryDarkText"
 | 
			
		||||
        tools:text="Label" />
 | 
			
		||||
 | 
			
		||||
</RelativeLayout>
 | 
			
		||||
 
 | 
			
		||||
@@ -70,7 +70,7 @@
 | 
			
		||||
        android:layout_alignParentEnd="true"
 | 
			
		||||
        android:layout_centerVertical="true"
 | 
			
		||||
        android:layout_marginEnd="16dp"
 | 
			
		||||
        app:ico_color="@android:color/black"
 | 
			
		||||
        app:ico_icon="cmd-rss"/>
 | 
			
		||||
        app:iiv_color="@android:color/black"
 | 
			
		||||
        app:iiv_icon="cmd-rss"/>
 | 
			
		||||
 | 
			
		||||
</RelativeLayout>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user