share some functions between monthly fragment and monthly widget

This commit is contained in:
tibbi 2017-10-08 11:00:22 +02:00
parent 3111713de8
commit 407af664a1
4 changed files with 110 additions and 151 deletions

View File

@ -4,22 +4,18 @@ import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Intent
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.TextView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.addDayEvents
import com.simplemobiletools.calendar.extensions.addDayNumber
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.helpers.MEDIUM_ALPHA
import com.simplemobiletools.calendar.helpers.MonthlyCalendarImpl
import com.simplemobiletools.calendar.helpers.MyWidgetMonthlyProvider
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
@ -27,8 +23,6 @@ import com.simplemobiletools.calendar.models.DayMonthly
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.beVisible
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.onGlobalLayout
import kotlinx.android.synthetic.main.first_row.*
import kotlinx.android.synthetic.main.top_navigation.*
import kotlinx.android.synthetic.main.widget_config_monthly.*
@ -38,7 +32,6 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
lateinit var mRes: Resources
private var mDays: List<DayMonthly>? = null
private var mPackageName = ""
private var dividerMargin = 0
private var dayLabelHeight = 0
private var mBgAlpha = 0f
@ -48,6 +41,7 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
private var mTextColorWithoutTransparency = 0
private var mTextColor = 0
private var mWeakTextColor = 0
private var mPrimaryColor = 0
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -70,7 +64,6 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
private fun initVariables() {
mPackageName = packageName
mRes = resources
dividerMargin = mRes.displayMetrics.density.toInt()
mTextColorWithoutTransparency = config.widgetTextColor
updateTextColors()
@ -134,6 +127,7 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
private fun updateTextColors() {
mTextColor = mTextColorWithoutTransparency
mWeakTextColor = mTextColorWithoutTransparency.adjustAlpha(LOW_ALPHA)
mPrimaryColor = config.primaryColor
top_left_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
top_right_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
@ -166,15 +160,16 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
}
}
val todayCircle = resources.getDrawable(R.drawable.circle_empty)
todayCircle.setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN)
val dividerMargin = mRes.displayMetrics.density.toInt()
for (i in 0 until len) {
(findViewById(mRes.getIdentifier("day_$i", "id", mPackageName)) as LinearLayout).apply {
val day = mDays!![i]
removeAllViews()
addDayNumber(day, this)
addDayEvents(day, this)
context.addDayNumber(mTextColor, day, this, dayLabelHeight) {
dayLabelHeight = it
}
context.addDayEvents(day, this, mRes, dividerMargin)
}
}
}
@ -197,15 +192,11 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
override fun updateMonthlyCalendar(month: String, days: List<DayMonthly>) {
runOnUiThread {
mDays = days
updateMonth(month)
top_value.text = month
updateDays()
}
}
private fun updateMonth(month: String) {
top_value.text = month
}
private fun updateLabels() {
for (i in 0..6) {
(findViewById(mRes.getIdentifier("label_$i", "id", mPackageName)) as TextView).apply {
@ -213,62 +204,4 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
}
}
}
private fun addDayNumber(day: DayMonthly, linearLayout: LinearLayout) {
(View.inflate(this, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(if (day.isThisMonth) mTextColor else mWeakTextColor)
text = day.value.toString()
gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearLayout.addView(this)
if (day.isToday) {
val primaryColor = context.config.primaryColor
setTextColor(primaryColor.getContrastColor().adjustAlpha(MEDIUM_ALPHA))
if (dayLabelHeight == 0) {
onGlobalLayout {
if (this@apply.height > 0) {
dayLabelHeight = this@apply.height
updateDayLabelHeight(this, primaryColor)
}
}
} else {
updateDayLabelHeight(this, primaryColor)
}
}
}
}
private fun updateDayLabelHeight(textView: TextView, primaryColor: Int) {
val baseDrawable = mRes.getDrawable(R.drawable.monthly_today_circle)
val bitmap = (baseDrawable as BitmapDrawable).bitmap
val scaledDrawable = BitmapDrawable(mRes, Bitmap.createScaledBitmap(bitmap, dayLabelHeight, dayLabelHeight, true))
scaledDrawable.mutate().setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN)
textView.background = scaledDrawable
}
private fun addDayEvents(day: DayMonthly, linearLayout: LinearLayout) {
day.dayEvents.forEach {
val backgroundDrawable = mRes.getDrawable(R.drawable.day_monthly_event_background)
backgroundDrawable.mutate().setColorFilter(it.color, PorterDuff.Mode.SRC_IN)
val eventLayoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
eventLayoutParams.setMargins(dividerMargin, dividerMargin, dividerMargin, dividerMargin)
var textColor = it.color.getContrastColor().adjustAlpha(MEDIUM_ALPHA)
if (!day.isThisMonth) {
backgroundDrawable.alpha = 64
textColor = textColor.adjustAlpha(0.25f)
}
(View.inflate(this, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(textColor)
text = it.title.replace(" ", "\u00A0") // allow word break by char
gravity = Gravity.START
background = backgroundDrawable
layoutParams = eventLayoutParams
linearLayout.addView(this)
}
}
}
}

View File

@ -11,22 +11,28 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.Color
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Build
import android.support.v4.content.ContextCompat
import android.support.v7.app.NotificationCompat
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.DayMonthly
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.receivers.CalDAVSyncReceiver
import com.simplemobiletools.calendar.receivers.NotificationReceiver
import com.simplemobiletools.calendar.services.SnoozeService
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.isKitkatPlus
import com.simplemobiletools.commons.extensions.isLollipopPlus
import com.simplemobiletools.commons.extensions.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.text.SimpleDateFormat
@ -261,3 +267,61 @@ fun Context.scheduleCalDAVSync(activate: Boolean) {
val Context.config: Config get() = Config.newInstance(this)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(this)
fun Context.addDayNumber(rawTextColor: Int, day: DayMonthly, linearLayout: LinearLayout, dayLabelHeight: Int, callback: (Int) -> Unit) {
var textColor = rawTextColor
if (!day.isThisMonth)
textColor = textColor.adjustAlpha(LOW_ALPHA)
(View.inflate(this, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(textColor)
text = day.value.toString()
gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearLayout.addView(this)
if (day.isToday) {
val primaryColor = config.primaryColor
setTextColor(config.primaryColor.getContrastColor().adjustAlpha(MEDIUM_ALPHA))
if (dayLabelHeight == 0) {
onGlobalLayout {
val height = this@apply.height
if (height > 0) {
callback(height)
addTodaysBackground(this, resources, height, primaryColor)
}
}
} else {
addTodaysBackground(this, resources, dayLabelHeight, primaryColor)
}
}
}
}
private fun addTodaysBackground(textView: TextView, res: Resources, dayLabelHeight: Int, mPrimaryColor: Int) =
textView.addResizedBackgroundDrawable(res, dayLabelHeight, mPrimaryColor)
fun Context.addDayEvents(day: DayMonthly, linearLayout: LinearLayout, res: Resources, dividerMargin: Int) {
day.dayEvents.forEach {
val backgroundDrawable = res.getDrawable(R.drawable.day_monthly_event_background)
backgroundDrawable.mutate().setColorFilter(it.color, PorterDuff.Mode.SRC_IN)
val eventLayoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
eventLayoutParams.setMargins(dividerMargin, dividerMargin, dividerMargin, dividerMargin)
var textColor = it.color.getContrastColor().adjustAlpha(MEDIUM_ALPHA)
if (!day.isThisMonth) {
backgroundDrawable.alpha = 64
textColor = textColor.adjustAlpha(0.25f)
}
(View.inflate(this, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(textColor)
text = it.title.replace(" ", "\u00A0") // allow word break by char
gravity = Gravity.START
background = backgroundDrawable
layoutParams = eventLayoutParams
linearLayout.addView(this)
}
}
}

View File

@ -0,0 +1,16 @@
package com.simplemobiletools.calendar.extensions
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.PorterDuff
import android.graphics.drawable.BitmapDrawable
import android.widget.TextView
import com.simplemobiletools.calendar.R
fun TextView.addResizedBackgroundDrawable(res: Resources, drawableHeight: Int, primaryColor: Int) {
val baseDrawable = res.getDrawable(R.drawable.monthly_today_circle)
val bitmap = (baseDrawable as BitmapDrawable).bitmap
val scaledDrawable = BitmapDrawable(res, Bitmap.createScaledBitmap(bitmap, drawableHeight, drawableHeight, true))
scaledDrawable.mutate().setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN)
background = scaledDrawable
}

View File

@ -2,13 +2,10 @@ package com.simplemobiletools.calendar.fragments
import android.content.Intent
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.PorterDuff
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -18,13 +15,17 @@ import android.widget.RelativeLayout
import android.widget.TextView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.extensions.addDayEvents
import com.simplemobiletools.calendar.extensions.addDayNumber
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getAppropriateTheme
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.DayMonthly
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.first_row.*
import kotlinx.android.synthetic.main.fragment_month.view.*
import kotlinx.android.synthetic.main.top_navigation.view.*
@ -32,11 +33,10 @@ import org.joda.time.DateTime
class MonthFragment : Fragment(), MonthlyCalendar {
private var mTextColor = 0
private var mWeakTextColor = 0
private var mPrimaryColor = 0
private var mSundayFirst = false
private var mDayCode = ""
private var mPackageName = ""
private var dividerMargin = 0
private var dayLabelHeight = 0
var listener: NavigationListener? = null
@ -49,7 +49,6 @@ class MonthFragment : Fragment(), MonthlyCalendar {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_month, container, false)
mRes = resources
dividerMargin = mRes.displayMetrics.density.toInt()
mPackageName = activity.packageName
mHolder = view.calendar_holder
@ -88,8 +87,10 @@ class MonthFragment : Fragment(), MonthlyCalendar {
override fun updateMonthlyCalendar(month: String, days: List<DayMonthly>) {
activity?.runOnUiThread {
mHolder.top_value.text = month
mHolder.top_value.setTextColor(mConfig.textColor)
mHolder.top_value.apply {
text = month
setTextColor(mConfig.textColor)
}
updateDays(days)
}
}
@ -97,7 +98,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
private fun setupButtons() {
val baseColor = mConfig.textColor
mTextColor = baseColor
mWeakTextColor = baseColor.adjustAlpha(LOW_ALPHA)
mPrimaryColor = mConfig.primaryColor
mHolder.apply {
top_left_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
@ -174,14 +175,17 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
}
val dividerMargin = mRes.displayMetrics.density.toInt()
for (i in 0 until len) {
(mHolder.findViewById(mRes.getIdentifier("day_$i", "id", mPackageName)) as LinearLayout).apply {
val day = days[i]
setOnClickListener { openDay(day.code) }
removeAllViews()
addDayNumber(day, this)
addDayEvents(day, this)
context.addDayNumber(mTextColor, day, this, dayLabelHeight) {
dayLabelHeight = it
}
context.addDayEvents(day, this, mRes, dividerMargin)
}
}
}
@ -194,62 +198,4 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
}
}
private fun addDayNumber(day: DayMonthly, linearLayout: LinearLayout) {
(View.inflate(context, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(if (day.isThisMonth) mTextColor else mWeakTextColor)
text = day.value.toString()
gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearLayout.addView(this)
if (day.isToday) {
val primaryColor = context.config.primaryColor
setTextColor(primaryColor.getContrastColor().adjustAlpha(MEDIUM_ALPHA))
if (dayLabelHeight == 0) {
onGlobalLayout {
if (this@apply.height > 0) {
dayLabelHeight = this@apply.height
updateDayLabelHeight(this, primaryColor)
}
}
} else {
updateDayLabelHeight(this, primaryColor)
}
}
}
}
private fun updateDayLabelHeight(textView: TextView, primaryColor: Int) {
val baseDrawable = mRes.getDrawable(R.drawable.monthly_today_circle)
val bitmap = (baseDrawable as BitmapDrawable).bitmap
val scaledDrawable = BitmapDrawable(mRes, Bitmap.createScaledBitmap(bitmap, dayLabelHeight, dayLabelHeight, true))
scaledDrawable.mutate().setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN)
textView.background = scaledDrawable
}
private fun addDayEvents(day: DayMonthly, linearLayout: LinearLayout) {
day.dayEvents.forEach {
val backgroundDrawable = mRes.getDrawable(R.drawable.day_monthly_event_background)
backgroundDrawable.mutate().setColorFilter(it.color, PorterDuff.Mode.SRC_IN)
val eventLayoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
eventLayoutParams.setMargins(dividerMargin, dividerMargin, dividerMargin, dividerMargin)
var textColor = it.color.getContrastColor().adjustAlpha(MEDIUM_ALPHA)
if (!day.isThisMonth) {
backgroundDrawable.alpha = 64
textColor = textColor.adjustAlpha(0.25f)
}
(View.inflate(context, R.layout.day_monthly_item_view, null) as TextView).apply {
setTextColor(textColor)
text = it.title.replace(" ", "\u00A0") // allow word break by char
gravity = Gravity.START
background = backgroundDrawable
layoutParams = eventLayoutParams
linearLayout.addView(this)
}
}
}
}