move Utils to kotlin + some cleanup

This commit is contained in:
tibbi 2016-11-17 19:44:23 +01:00
parent ba767b83d4
commit 11d2215e37
20 changed files with 134 additions and 152 deletions

View File

@ -1,85 +0,0 @@
package com.simplemobiletools.calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;
import android.widget.Toast;
import com.simplemobiletools.calendar.helpers.Formatter;
import com.simplemobiletools.calendar.models.Event;
import com.simplemobiletools.calendar.receivers.NotificationReceiver;
import org.joda.time.DateTime;
public class Utils {
public static int adjustAlpha(int color, float factor) {
final int alpha = Math.round(Color.alpha(color) * factor);
final int red = Color.red(color);
final int green = Color.green(color);
final int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
public static void scheduleNextEvent(Context context, Event event) {
int startTS = event.getStartTS() - event.getReminderMinutes() * 60;
int newTS = startTS;
if (event.getRepeatInterval() == Constants.DAY || event.getRepeatInterval() == Constants.WEEK || event.getRepeatInterval() == Constants.BIWEEK) {
while (startTS < System.currentTimeMillis() / 1000 + 5) {
startTS += event.getRepeatInterval();
}
newTS = startTS;
} else if (event.getRepeatInterval() == Constants.MONTH) {
newTS = getNewTS(startTS, true);
} else if (event.getRepeatInterval() == Constants.YEAR) {
newTS = getNewTS(startTS, false);
}
if (newTS != 0)
Utils.scheduleEventIn(context, newTS, event);
}
private static int getNewTS(int ts, boolean isMonthly) {
DateTime dateTime = Formatter.INSTANCE.getDateTimeFromTS(ts);
while (dateTime.isBeforeNow()) {
dateTime = isMonthly ? dateTime.plusMonths(1) : dateTime.plusYears(1);
}
return (int) (dateTime.getMillis() / 1000);
}
public static void scheduleNotification(Context context, Event event) {
if (event.getReminderMinutes() == -1)
return;
scheduleNextEvent(context, event);
}
private static void scheduleEventIn(Context context, int notifTS, Event event) {
final long delayFromNow = (long) notifTS * 1000 - System.currentTimeMillis();
if (delayFromNow < 0)
return;
final long notifInMs = SystemClock.elapsedRealtime() + delayFromNow;
final PendingIntent pendingIntent = getNotificationIntent(context, event.getId());
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notifInMs, pendingIntent);
}
private static PendingIntent getNotificationIntent(Context context, int eventId) {
final Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.Companion.getEVENT_ID(), eventId);
return PendingIntent.getBroadcast(context, eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static int[] getLetterIDs() {
return new int[]{R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter,
R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter};
}
}

View File

@ -98,11 +98,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
}
}
for (day in days) {
if (eventCodes.contains(day.code)) {
day.hasEvent = true
}
}
days.filter { eventCodes.contains(it.code) }.forEach { it.hasEvent = true }
}
private fun isToday(targetDate: DateTime, curDayInMonth: Int): Boolean {

View File

@ -7,10 +7,10 @@ import android.support.design.widget.Snackbar
import android.support.v4.view.ViewPager
import android.view.View
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.helpers.Formatter
import kotlinx.android.synthetic.main.activity_day.*
import org.joda.time.DateTime
import java.util.*
@ -119,8 +119,10 @@ class DayActivity : SimpleActivity(), DayFragment.DeleteListener, ViewPager.OnPa
override fun notifyDeletion(cnt: Int) {
val msg = resources.getQuantityString(R.plurals.events_deleted, cnt, cnt)
mSnackbar = Snackbar.make(day_coordinator, msg, Snackbar.LENGTH_INDEFINITE)
mSnackbar!!.setAction(resources.getString(R.string.undo), undoDeletion)
mSnackbar!!.setActionTextColor(Color.WHITE)
mSnackbar!!.show()
mSnackbar!!.apply {
setAction(resources.getString(R.string.undo), undoDeletion)
setActionTextColor(Color.WHITE)
show()
}
}
}

View File

@ -9,7 +9,8 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.*
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.helpers.DBHelper
@ -217,16 +218,17 @@ class EventActivity : SimpleActivity(), DBHelper.EventsListener {
}
private fun deleteEvent() {
val intent = Intent()
intent.putExtra(DayFragment.DELETED_ID, mEvent.id)
setResult(Activity.RESULT_OK, intent)
Intent().apply {
putExtra(DayFragment.DELETED_ID, mEvent.id)
setResult(Activity.RESULT_OK, this)
}
finish()
}
private fun saveEvent() {
val newTitle = event_title.value
if (newTitle.isEmpty()) {
Utils.showToast(applicationContext, R.string.title_empty)
toast(R.string.title_empty)
event_title.requestFocus()
return
}
@ -235,7 +237,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventsListener {
val newEndTS = (mEventEndDateTime.millis / 1000).toInt()
if (event_end_checkbox.isChecked && newStartTS > newEndTS) {
Utils.showToast(applicationContext, R.string.end_before_start)
toast(R.string.end_before_start)
return
}
@ -360,18 +362,18 @@ class EventActivity : SimpleActivity(), DBHelper.EventsListener {
override fun eventInserted(event: Event) {
if (DateTime.now().isAfter(mEventStartDateTime.millis)) {
Utils.showToast(applicationContext, R.string.past_event_added)
toast(R.string.past_event_added)
} else {
Utils.showToast(applicationContext, R.string.event_added)
toast(R.string.event_added)
}
Utils.scheduleNotification(applicationContext, event)
scheduleNotification(event)
updateWidget()
finish()
}
override fun eventUpdated(event: Event) {
Utils.scheduleNotification(applicationContext, event)
Utils.showToast(applicationContext, R.string.event_updated)
scheduleNotification(event)
toast(R.string.event_updated)
updateWidget()
finish()
}

View File

@ -9,12 +9,12 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter
import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.extensions.updateWidget
import com.simplemobiletools.calendar.fragments.EventListFragment
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.views.dialogs.ChangeViewDialog
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
@ -222,14 +222,16 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener, ChangeV
override fun notifyDeletion(cnt: Int) {
val msg = resources.getQuantityString(R.plurals.events_deleted, cnt, cnt)
mSnackbar = Snackbar.make(calendar_coordinator, msg, Snackbar.LENGTH_LONG)
mSnackbar!!.setCallback(object: Snackbar.Callback() {
override fun onDismissed(snackbar: Snackbar?, event: Int) {
super.onDismissed(snackbar, event)
mEventListFragment?.deleteEvents()
}
})
mSnackbar!!.setAction(resources.getString(R.string.undo), undoDeletion)
mSnackbar!!.setActionTextColor(Color.WHITE)
mSnackbar!!.show()
mSnackbar!!.apply {
setCallback(object : Snackbar.Callback() {
override fun onDismissed(snackbar: Snackbar?, event: Int) {
super.onDismissed(snackbar, event)
mEventListFragment?.deleteEvents()
}
})
setAction(resources.getString(R.string.undo), undoDeletion)
setActionTextColor(Color.WHITE)
show()
}
}
}

View File

@ -3,8 +3,8 @@ package com.simplemobiletools.calendar.activities
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.Config
open class SimpleActivity : AppCompatActivity() {
lateinit var mConfig: Config
@ -16,12 +16,12 @@ open class SimpleActivity : AppCompatActivity() {
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
return when (item.itemId) {
android.R.id.home -> {
finish()
return true
true
}
else -> return super.onOptionsItemSelected(item)
else -> super.onOptionsItemSelected(item)
}
}
}

View File

@ -15,9 +15,9 @@ import android.widget.TextView
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.MonthlyCalendarImpl
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.Utils
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.MyWidgetProvider
import com.simplemobiletools.calendar.helpers.Utils
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.models.Day
import kotlinx.android.synthetic.main.first_row.*

View File

@ -5,8 +5,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.Event
import kotlinx.android.synthetic.main.event_item.view.*
@ -45,17 +45,11 @@ class EventsAdapter(context: Context, private val mEvents: List<Event>) : BaseAd
return view
}
override fun getCount(): Int {
return mEvents.size
}
override fun getCount() = mEvents.size
override fun getItem(position: Int): Any {
return mEvents[position]
}
override fun getItem(position: Int) = mEvents[position]
override fun getItemId(position: Int): Long {
return 0
}
override fun getItemId(position: Int) = 0L
class ViewHolder(view: View) {
val title = view.event_item_title

View File

@ -16,9 +16,7 @@ class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, p
fragments = SparseArray(10)
}
override fun getCount(): Int {
return mCodes.size
}
override fun getCount() = mCodes.size
override fun getItem(position: Int): Fragment {
val bundle = Bundle()

View File

@ -5,14 +5,12 @@ import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.fragments.YearFragment
import com.simplemobiletools.calendar.interfaces.NavigationListener
class MyYearPagerAdapter(fm: FragmentManager, private val mYears: List<Int>, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
override fun getCount(): Int {
return mYears.size
}
override fun getCount() = mYears.size
override fun getItem(position: Int): Fragment {
val bundle = Bundle()

View File

@ -1,16 +1,23 @@
package com.simplemobiletools.calendar.extensions
import android.app.AlarmManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.SystemClock
import android.widget.Toast
import com.simplemobiletools.calendar.helpers.MyWidgetProvider
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.MyWidgetProvider
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.receivers.NotificationReceiver
fun Context.updateWidget() {
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
if (widgetsCnt.size == 0)
if (widgetsCnt.isEmpty())
return
val intent = Intent(this, MyWidgetProvider::class.java)
@ -22,4 +29,53 @@ fun Context.updateWidget() {
fun Context.toast(id: Int) = Toast.makeText(this, resources.getString(id), Toast.LENGTH_SHORT).show()
fun Context.toast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
fun Context.scheduleNextEvent(event: Event) {
var startTS = event.startTS - event.reminderMinutes * 60
var newTS = startTS
if (event.repeatInterval == Constants.DAY || event.repeatInterval == Constants.WEEK || event.repeatInterval == Constants.BIWEEK) {
while (startTS < System.currentTimeMillis() / 1000 + 5) {
startTS += event.repeatInterval
}
newTS = startTS
} else if (event.repeatInterval == Constants.MONTH) {
newTS = getNewTS(startTS, true)
} else if (event.repeatInterval == Constants.YEAR) {
newTS = getNewTS(startTS, false)
}
if (newTS != 0)
scheduleEventIn(newTS, event)
}
private fun getNewTS(ts: Int, isMonthly: Boolean): Int {
var dateTime = Formatter.getDateTimeFromTS(ts)
while (dateTime.isBeforeNow) {
dateTime = if (isMonthly) dateTime.plusMonths(1) else dateTime.plusYears(1)
}
return (dateTime.millis / 1000).toInt()
}
fun Context.scheduleNotification(event: Event) {
if (event.reminderMinutes == -1)
return
scheduleNextEvent(event)
}
fun Context.scheduleEventIn(notifTS: Int, event: Event) {
val delayFromNow = notifTS.toLong() * 1000 - System.currentTimeMillis()
if (delayFromNow < 0)
return
val notifInMs = SystemClock.elapsedRealtime() + delayFromNow
val pendingIntent = getNotificationIntent(this, event.id)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notifInMs, pendingIntent)
}
private fun getNotificationIntent(context: Context, eventId: Int): PendingIntent {
val intent = Intent(context, NotificationReceiver::class.java)
intent.putExtra(NotificationReceiver.EVENT_ID, eventId)
return PendingIntent.getBroadcast(context, eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

View File

@ -2,4 +2,4 @@ package com.simplemobiletools.calendar.extensions
import android.widget.EditText
val EditText.value: String get() = this.text.toString().trim()
val EditText.value: String get() = text.toString().trim()

View File

@ -20,6 +20,7 @@ import com.simplemobiletools.calendar.adapters.EventsAdapter
import com.simplemobiletools.calendar.extensions.updateWidget
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.Utils
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Event
import kotlinx.android.synthetic.main.day_fragment.view.*

View File

@ -16,6 +16,7 @@ import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.extensions.beVisibleIf
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.Utils
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Day
@ -149,7 +150,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
private fun setupLabels() {
val letters = Utils.getLetterIDs()
val letters = Utils.letterIDs
for (i in 0..6) {
val dayTV = mHolder.findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName)) as TextView

View File

@ -10,6 +10,7 @@ import android.view.ViewGroup
import android.widget.TextView
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.helpers.Utils
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.interfaces.YearlyCalendar
import com.simplemobiletools.calendar.views.SmallMonthView

View File

@ -16,7 +16,7 @@ import android.widget.RemoteViews
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.MonthlyCalendarImpl
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.Utils
import com.simplemobiletools.calendar.helpers.Utils
import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
@ -183,7 +183,7 @@ class MyWidgetProvider : AppWidgetProvider(), MonthlyCalendar {
private fun updateLabelColor() {
val mSundayFirst = Config.newInstance(mContext).isSundayFirst
val packageName = mContext.packageName
val letters = Utils.getLetterIDs()
val letters = Utils.letterIDs
for (i in 0..6) {
val id = mRes.getIdentifier("label_" + i, "id", packageName)
mRemoteViews.setInt(id, "setTextColor", mTextColor)

View File

@ -0,0 +1,18 @@
package com.simplemobiletools.calendar.helpers
import android.graphics.Color
import com.simplemobiletools.calendar.R
object Utils {
fun adjustAlpha(color: Int, factor: Float): Int {
val alpha = Math.round(Color.alpha(color) * factor)
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
return Color.argb(alpha, red, green, blue)
}
val letterIDs: IntArray
get() = intArrayOf(R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter,
R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter)
}

View File

@ -3,15 +3,15 @@ package com.simplemobiletools.calendar.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.calendar.extensions.scheduleNextEvent
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.Utils
class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
val events = DBHelper(context).getEventsAtReboot()
for (event in events) {
Utils.scheduleNextEvent(context, event)
context.scheduleNextEvent(event)
}
}
}

View File

@ -9,8 +9,8 @@ import android.content.Intent
import android.media.RingtoneManager
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.Utils
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.extensions.scheduleNextEvent
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.models.Event
@ -38,12 +38,10 @@ class NotificationReceiver : BroadcastReceiver() {
notificationManager.notify(id, notification)
if (event.repeatInterval != 0)
Utils.scheduleNextEvent(context, event)
context.scheduleNextEvent(event)
}
private fun getEventTime(startTime: String, endTime: String): String {
return if (startTime == endTime) startTime else startTime + " - " + endTime
}
private fun getEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else startTime + " - " + endTime
private fun getPendingIntent(context: Context, event: Event): PendingIntent {
val intent = Intent(context, EventActivity::class.java)

View File

@ -9,7 +9,7 @@ import android.view.View
import com.simplemobiletools.calendar.helpers.Config
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.Utils
import com.simplemobiletools.calendar.helpers.Utils
import java.util.*
class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {