diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt index ccb24473f..72d7472d7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.activities import android.app.Activity import android.appwidget.AppWidgetManager +import android.content.Context import android.content.Intent import android.content.res.Resources import android.graphics.Color @@ -187,7 +188,7 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar { } } - override fun updateMonthlyCalendar(month: String, days: List) { + override fun updateMonthlyCalendar(context: Context, month: String, days: List) { runOnUiThread { mDays = days top_value.text = month diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt index eebcda135..757060973 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.calendar.fragments +import android.content.Context import android.content.Intent import android.content.res.Resources import android.graphics.PorterDuff @@ -61,9 +62,6 @@ class MonthFragment : Fragment(), MonthlyCalendar { setupLabels() mCalendar = MonthlyCalendarImpl(this, context) - val padding = mRes.getDimension(R.dimen.activity_margin).toInt() - view.calendar_holder.setPadding(padding, padding, padding, padding) - return view } @@ -85,7 +83,7 @@ class MonthFragment : Fragment(), MonthlyCalendar { mCalendar.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode)) } - override fun updateMonthlyCalendar(month: String, days: List) { + override fun updateMonthlyCalendar(context: Context, month: String, days: List) { activity?.runOnUiThread { mHolder.top_value.apply { text = month diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt index 0a084c0d6..660643f1c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt @@ -79,7 +79,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) if (markDaysWithEvents) { markDaysWithEvents(days) } else { - mCallback.updateMonthlyCalendar(monthName, days) + mCallback.updateMonthlyCalendar(mContext, monthName, days) } } @@ -108,7 +108,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) days.filter { dayEvents.keys.contains(it.code) }.forEach { it.dayEvents = dayEvents[it.code]!! } - mCallback.updateMonthlyCalendar(monthName, days) + mCallback.updateMonthlyCalendar(mContext, monthName, days) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt index d8c29d83b..f728e4227 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt @@ -6,7 +6,6 @@ import android.appwidget.AppWidgetProvider import android.content.ComponentName import android.content.Context import android.content.Intent -import android.content.res.Resources import android.graphics.* import android.view.View import android.widget.RemoteViews @@ -22,151 +21,129 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.commons.extensions.setTextSize import org.joda.time.DateTime -class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar { +class MyWidgetMonthlyProvider : AppWidgetProvider() { private val PREV = "prev" private val NEXT = "next" private val NEW_EVENT = "new_event" - private var mTextColor = 0 - private var mCalendar: MonthlyCalendarImpl? = null - - private var mSmallerFontSize = 0f - private var mMediumFontSize = 0f - private var mLargerFontSize = 0f - - lateinit var mRes: Resources - lateinit var mContext: Context - lateinit var mWidgetManager: AppWidgetManager - lateinit var mIntent: Intent - companion object { - private var mTargetDate = DateTime() - private var mRemoteViews: RemoteViews? = null + var targetDate = DateTime.now() } override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { - initVariables(context) - updateWidget() - super.onUpdate(context, appWidgetManager, appWidgetIds) + performUpdate(context) } - private fun initVariables(context: Context) { - mContext = context - mRes = mContext.resources - mCalendar = MonthlyCalendarImpl(this, mContext) - mWidgetManager = AppWidgetManager.getInstance(mContext) + private fun performUpdate(context: Context) { + val largerFontSize = context.config.getFontSize() + 3f + val textColor = context.config.widgetTextColor - context.config.apply { - mTextColor = widgetTextColor - mMediumFontSize = getFontSize() - mSmallerFontSize = getFontSize() - 3f - mLargerFontSize = getFontSize() + 3f + val appWidgetManager = AppWidgetManager.getInstance(context) + appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { + val views = RemoteViews(context.packageName, R.layout.fragment_month_widget) + views.setBackgroundColor(R.id.calendar_holder, context.config.widgetBgColor) - mRemoteViews = RemoteViews(mContext.packageName, R.layout.fragment_month_widget) - mRemoteViews?.setBackgroundColor(R.id.calendar_holder, widgetBgColor) - } + views.setTextColor(R.id.top_value, textColor) + views.setTextSize(R.id.top_value, largerFontSize) - mIntent = Intent(mContext, MyWidgetMonthlyProvider::class.java) - setupButtons() - updateLabelColor() - updateTopViews() - mCalendar?.updateMonthlyCalendar(mTargetDate, false) - } + var bmp = getColoredIcon(context, textColor, R.drawable.ic_pointer_left) + views.setImageViewBitmap(R.id.top_left_arrow, bmp) - private fun updateWidget() { - val thisWidget = ComponentName(mContext, MyWidgetMonthlyProvider::class.java) - try { - AppWidgetManager.getInstance(mContext).updateAppWidget(thisWidget, mRemoteViews) - } catch (ignored: Exception) { + bmp = getColoredIcon(context, textColor, R.drawable.ic_pointer_right) + views.setImageViewBitmap(R.id.top_right_arrow, bmp) + + bmp = getColoredIcon(context, textColor, R.drawable.ic_plus) + views.setImageViewBitmap(R.id.top_new_event, bmp) + + setupIntent(context, views, PREV, R.id.top_left_arrow) + setupIntent(context, views, NEXT, R.id.top_right_arrow) + setupIntent(context, views, NEW_EVENT, R.id.top_new_event) + setupAppOpenIntent(context, views, R.id.top_value) + updateDayLabels(context, views, textColor) + + appWidgetManager.updateAppWidget(it, views) + MonthlyCalendarImpl(monthlyCalendar, context).getMonth(targetDate) } } - private fun setupIntent(action: String, id: Int) { - mIntent.action = action - val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0) - mRemoteViews?.setOnClickPendingIntent(id, pendingIntent) - } - - private fun setupAppOpenIntent(id: Int) { - Intent(mContext, SplashActivity::class.java).apply { - val pendingIntent = PendingIntent.getActivity(mContext, 0, this, 0) - mRemoteViews?.setOnClickPendingIntent(id, pendingIntent) + private fun setupIntent(context: Context, views: RemoteViews, action: String, id: Int) { + Intent(context, MyWidgetMonthlyProvider::class.java).apply { + this.action = action + val pendingIntent = PendingIntent.getBroadcast(context, 0, this, 0) + views.setOnClickPendingIntent(id, pendingIntent) } } - private fun setupDayOpenIntent(id: Int, dayCode: String) { - Intent(mContext, SplashActivity::class.java).apply { + private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) { + val intent = Intent(context, SplashActivity::class.java) + val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) + views.setOnClickPendingIntent(id, pendingIntent) + } + + private fun setupDayOpenIntent(context: Context, views: RemoteViews, id: Int, dayCode: String) { + Intent(context, SplashActivity::class.java).apply { putExtra(DAY_CODE, dayCode) - val pendingIntent = PendingIntent.getActivity(mContext, Integer.parseInt(dayCode), this, 0) - mRemoteViews?.setOnClickPendingIntent(id, pendingIntent) + val pendingIntent = PendingIntent.getActivity(context, Integer.parseInt(dayCode), this, 0) + views.setOnClickPendingIntent(id, pendingIntent) } } - private fun setupButtons() { - setupIntent(PREV, R.id.top_left_arrow) - setupIntent(NEXT, R.id.top_right_arrow) - setupIntent(NEW_EVENT, R.id.top_new_event) - setupAppOpenIntent(R.id.top_value) - } + private fun getComponentName(context: Context) = ComponentName(context, MyWidgetMonthlyProvider::class.java) override fun onReceive(context: Context, intent: Intent) { - if (mCalendar == null || mRemoteViews == null) { - initVariables(context) - } - - val action = intent.action - when (action) { - PREV -> getPrevMonth() - NEXT -> getNextMonth() - NEW_EVENT -> mContext.launchNewEventIntent() + when (intent.action) { + PREV -> getPrevMonth(context) + NEXT -> getNextMonth(context) + NEW_EVENT -> context.launchNewEventIntent() else -> super.onReceive(context, intent) } } - private fun getPrevMonth() { - mTargetDate = mTargetDate.minusMonths(1) - mCalendar?.getMonth(mTargetDate) + private fun getPrevMonth(context: Context) { + targetDate = targetDate!!.minusMonths(1) + MonthlyCalendarImpl(monthlyCalendar, context).getMonth(targetDate!!) } - private fun getNextMonth() { - mTargetDate = mTargetDate.plusMonths(1) - mCalendar?.getMonth(mTargetDate) + private fun getNextMonth(context: Context) { + targetDate = targetDate!!.plusMonths(1) + MonthlyCalendarImpl(monthlyCalendar, context).getMonth(targetDate!!) } - private fun updateDays(days: List) { - if (mRemoteViews == null) - return - - val displayWeekNumbers = mContext.config.displayWeekNumbers + private fun updateDays(context: Context, views: RemoteViews, days: List) { + val displayWeekNumbers = context.config.displayWeekNumbers + val textColor = context.config.widgetTextColor + val smallerFontSize = context.config.getFontSize() - 3f + val res = context.resources val len = days.size - val packageName = mContext.packageName - mRemoteViews!!.apply { - setTextColor(R.id.week_num, mTextColor) - setTextSize(R.id.week_num, mSmallerFontSize) + val packageName = context.packageName + views.apply { + setTextColor(R.id.week_num, textColor) + setTextSize(R.id.week_num, smallerFontSize) setViewVisibility(R.id.week_num, if (displayWeekNumbers) View.VISIBLE else View.GONE) } for (i in 0..5) { - val id = mRes.getIdentifier("week_num_$i", "id", packageName) - mRemoteViews!!.apply { - setTextViewText(id, "${days[i * 7 + 3].weekOfYear}:") // fourth day of the week matters - setTextColor(id, mTextColor) - setTextSize(id, mSmallerFontSize) + val id = res.getIdentifier("week_num_$i", "id", packageName) + views.apply { + setTextViewText(id, "${days[i * 7 + 3].weekOfYear}:") // fourth day of the week matters at determining week of the year + setTextColor(id, textColor) + setTextSize(id, smallerFontSize) setViewVisibility(id, if (displayWeekNumbers) View.VISIBLE else View.GONE) } } - val weakTextColor = mTextColor.adjustAlpha(LOW_ALPHA) + val weakTextColor = textColor.adjustAlpha(LOW_ALPHA) for (i in 0 until len) { val day = days[i] - var textColor = if (day.isThisMonth) mTextColor else weakTextColor - val primaryColor = mContext.config.primaryColor + var currTextColor = if (day.isThisMonth) textColor else weakTextColor + val primaryColor = context.config.primaryColor if (day.isToday) - textColor = primaryColor.getContrastColor() + currTextColor = primaryColor.getContrastColor() - val id = mRes.getIdentifier("day_$i", "id", packageName) - mRemoteViews!!.removeAllViews(id) - addDayNumber(day, packageName, textColor, id, primaryColor) + val id = res.getIdentifier("day_$i", "id", packageName) + views.removeAllViews(id) + addDayNumber(context, views, day, currTextColor, id, primaryColor) + setupDayOpenIntent(context, views, id, day.code) day.dayEvents.forEach { var backgroundColor = it.color @@ -180,68 +157,56 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar { val newRemoteView = RemoteViews(packageName, R.layout.day_monthly_event_view).apply { setTextViewText(R.id.day_monthly_event_id, it.title.replace(" ", "\u00A0")) setTextColor(R.id.day_monthly_event_id, eventTextColor) + setTextSize(R.id.day_monthly_event_id, smallerFontSize - 3f) setBackgroundColor(R.id.day_monthly_event_id, backgroundColor) } - mRemoteViews!!.addView(id, newRemoteView) + views.addView(id, newRemoteView) } } } - private fun addDayNumber(day: DayMonthly, packageName: String, textColor: Int, id: Int, primaryColor: Int) { - val newRemoteView = RemoteViews(packageName, R.layout.day_monthly_number_view).apply { + private fun addDayNumber(context: Context, views: RemoteViews, day: DayMonthly, textColor: Int, id: Int, primaryColor: Int) { + val newRemoteView = RemoteViews(context.packageName, R.layout.day_monthly_number_view).apply { setTextViewText(R.id.day_monthly_number_id, day.value.toString()) setTextColor(R.id.day_monthly_number_id, textColor) + setTextSize(R.id.day_monthly_number_id, context.config.getFontSize() - 3f) if (day.isToday) { setViewPadding(R.id.day_monthly_number_id, 10, 0, 10, 0) setBackgroundColor(R.id.day_monthly_number_id, primaryColor) } } - mRemoteViews!!.addView(id, newRemoteView) - setupDayOpenIntent(id, day.code) + views.addView(id, newRemoteView) } - private fun updateTopViews() { - mRemoteViews?.setTextColor(R.id.top_value, mTextColor) - mRemoteViews?.setTextSize(R.id.top_value, mLargerFontSize) - - var bmp = getColoredIcon(mContext, mTextColor, R.drawable.ic_pointer_left) - mRemoteViews?.setImageViewBitmap(R.id.top_left_arrow, bmp) - - bmp = getColoredIcon(mContext, mTextColor, R.drawable.ic_pointer_right) - mRemoteViews?.setImageViewBitmap(R.id.top_right_arrow, bmp) - - bmp = getColoredIcon(mContext, mTextColor, R.drawable.ic_plus) - mRemoteViews?.setImageViewBitmap(R.id.top_new_event, bmp) - } - - private fun updateMonth(month: String) { - mRemoteViews?.setTextViewText(R.id.top_value, month) - } - - override fun updateMonthlyCalendar(month: String, days: List) { - try { - updateMonth(month) - updateDays(days) - updateWidget() - } catch (ignored: ArrayIndexOutOfBoundsException) { + private val monthlyCalendar = object : MonthlyCalendar { + override fun updateMonthlyCalendar(context: Context, month: String, days: List) { + val appWidgetManager = AppWidgetManager.getInstance(context) + appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { + val views = RemoteViews(context.packageName, R.layout.fragment_month_widget) + views.setTextViewText(R.id.top_value, month) + updateDays(context, views, days) + appWidgetManager.updateAppWidget(it, views) + } } } - private fun updateLabelColor() { - val mSundayFirst = mContext.config.isSundayFirst - val packageName = mContext.packageName + private fun updateDayLabels(context: Context, views: RemoteViews, textColor: Int) { + val sundayFirst = context.config.isSundayFirst + val smallerFontSize = context.config.getFontSize() - 3f + val res = context.resources + val packageName = context.packageName val letters = letterIDs for (i in 0..6) { - val id = mRes.getIdentifier("label_$i", "id", packageName) - mRemoteViews?.setTextColor(id, mTextColor) - mRemoteViews?.setTextSize(id, mSmallerFontSize) + val id = res.getIdentifier("label_$i", "id", packageName) + views.setTextColor(id, textColor) + views.setTextSize(id, smallerFontSize) var index = i - if (!mSundayFirst) + if (!sundayFirst) index = (index + 1) % letters.size - mRemoteViews?.setTextViewText(id, mContext.resources.getString(letters[index])) + views.setTextViewText(id, res.getString(letters[index])) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt index b6ae53991..c6ac7cb5a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt @@ -1,7 +1,8 @@ package com.simplemobiletools.calendar.interfaces +import android.content.Context import com.simplemobiletools.calendar.models.DayMonthly interface MonthlyCalendar { - fun updateMonthlyCalendar(month: String, days: List) + fun updateMonthlyCalendar(context: Context, month: String, days: List) } diff --git a/app/src/main/res/layout/day_monthly_event_view.xml b/app/src/main/res/layout/day_monthly_event_view.xml index 9a85d597e..2c3127b9c 100644 --- a/app/src/main/res/layout/day_monthly_event_view.xml +++ b/app/src/main/res/layout/day_monthly_event_view.xml @@ -5,8 +5,7 @@ android:id="@+id/day_monthly_event_id" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginLeft="@dimen/tiny_margin" - android:layout_marginRight="@dimen/tiny_margin" + android:layout_margin="1dp" android:ellipsize="none" android:gravity="start" android:maxLines="1" diff --git a/app/src/main/res/layout/first_row.xml b/app/src/main/res/layout/first_row.xml index c21a525b4..569481f1b 100644 --- a/app/src/main/res/layout/first_row.xml +++ b/app/src/main/res/layout/first_row.xml @@ -1,7 +1,6 @@ + android:text="#" + android:visibility="gone"/> + android:orientation="horizontal" + android:paddingBottom="@dimen/small_margin" + android:paddingTop="@dimen/small_margin"> + android:text="#"/> + android:text="@string/monday_letter"/> + android:text="@string/tuesday_letter"/> + android:text="@string/wednesday_letter"/> + android:text="@string/thursday_letter"/> + android:text="@string/friday_letter"/> + android:text="@string/saturday_letter"/> + android:text="@string/sunday_letter"/> diff --git a/app/src/main/res/layout/fragment_month.xml b/app/src/main/res/layout/fragment_month.xml index f6bd01566..00aeee173 100644 --- a/app/src/main/res/layout/fragment_month.xml +++ b/app/src/main/res/layout/fragment_month.xml @@ -4,7 +4,8 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/calendar_holder" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:padding="@dimen/medium_margin"> diff --git a/app/src/main/res/layout/fragment_month_widget.xml b/app/src/main/res/layout/fragment_month_widget.xml index 7a24b10c6..6a1677961 100644 --- a/app/src/main/res/layout/fragment_month_widget.xml +++ b/app/src/main/res/layout/fragment_month_widget.xml @@ -1,26 +1,30 @@ + android:layout_height="match_parent" + android:padding="@dimen/medium_margin"> + + - - + android:visibility="gone"/> + android:visibility="gone"/>