package com.alexstyl.specialdates.upcoming.widget.list

import android.content.res.Resources
import android.support.annotation.Px
import android.widget.RemoteViews
import com.alexstyl.specialdates.R
import com.alexstyl.specialdates.upcoming.UpcomingContactEventViewModel


internal class ContactEventBinder(private val remoteViews: RemoteViews,
                                  private val resources: Resources,
                                  private val avatarFactory: CircularAvatarFactory)
    : UpcomingEventViewBinder<UpcomingContactEventViewModel> {

    override fun bind(viewModel: UpcomingContactEventViewModel) {
        remoteViews.setTextViewText(R.id.row_upcoming_event_contact_name, viewModel.contactName)
        remoteViews.setTextViewText(R.id.row_upcoming_event_contact_event, viewModel.eventLabel)
        @Px val targetSize = resources.getDimensionPixelSize(R.dimen.widget_upcoming_avatar_size)
        val avatar = avatarFactory.circularAvatarFor(viewModel.contact, targetSize)
        if (avatar.isPresent) {
            remoteViews.setImageViewBitmap(R.id.row_upcoming_event_contact_image, avatar.get())
        }
    }

    override fun getViews(): RemoteViews = remoteViews
}

package com.alexstyl.specialdates.upcoming.widget.list

import android.content.res.Resources
import android.graphics.Bitmap
import android.support.annotation.Px
import android.widget.RemoteViews
import com.alexstyl.specialdates.R
import com.alexstyl.specialdates.contact.Contact
import com.alexstyl.specialdates.upcoming.UpcomingContactEventViewModel


internal class ContactEventBinder(private val remoteViews: RemoteViews,
                                  private val resources: Resources,
                                  private val avatarFactory: CircularAvatarFactory)
    : UpcomingEventViewBinder<UpcomingContactEventViewModel> {

    override fun bind(viewModel: UpcomingContactEventViewModel) {
        remoteViews.setTextViewText(R.id.row_upcoming_event_contact_name, viewModel.contactName)
        remoteViews.setTextViewText(R.id.row_upcoming_event_contact_event, viewModel.eventLabel)
        remoteViews.setTextColor(R.id.row_upcoming_event_contact_event, viewModel.eventColor)

        val avatar = createAvatarFor(viewModel.contact)
        remoteViews.setImageViewBitmap(R.id.row_upcoming_event_contact_image, avatar)
    }

    private fun createAvatarFor(contact: Contact): Bitmap {
        @Px val targetSize = resources.getDimensionPixelSize(R.dimen.widget_upcoming_avatar_size)
        val avatar = avatarFactory.circularAvatarFor(contact, targetSize)
        return if (avatar.isPresent) {
            avatar.get()
        } else {
            val textSize = resources.getDimensionPixelSize(R.dimen.widget_upcoming_avatar_text_size)
            avatarFactory.createLetterAvatarFor(contact.displayName, targetSize, textSize)
        }
    }

    override fun getViews(): RemoteViews = remoteViews
}

Create a circle avatar if a contact has none
Create a circle avatar if a contact has none

pchrysa/Memento-Calendar,pchrysa/Memento-Calendar,pchrysa/Memento-Calendar