create a DateTime extension for getting the DateTime timestamp in seconds

This commit is contained in:
tibbi 2017-01-15 22:26:52 +01:00
parent 2c74330a3c
commit ab4964db5f
15 changed files with 42 additions and 38 deletions

View File

@ -9,10 +9,10 @@ import android.view.View
import android.view.WindowManager
import android.widget.AdapterView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.beVisibleIf
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getAppropriateTheme
import com.simplemobiletools.calendar.extensions.scheduleNotification
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -230,8 +230,8 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
return
}
val newStartTS = (mEventStartDateTime.millis / 1000).toInt()
val newEndTS = (mEventEndDateTime.millis / 1000).toInt()
val newStartTS = mEventStartDateTime.seconds()
val newEndTS = mEventEndDateTime.seconds()
if (event_end_checkbox.isChecked && newStartTS > newEndTS) {
toast(R.string.end_before_start)

View File

@ -9,7 +9,6 @@ import android.os.Parcelable
import android.view.View
import android.widget.AdapterView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.beVisibleIf
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.commons.extensions.*

View File

@ -11,6 +11,7 @@ import android.widget.SeekBar
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventListWidgetAdapterOld
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.MyWidgetListProvider
import com.simplemobiletools.calendar.models.ListEvent
@ -134,26 +135,26 @@ class WidgetListConfigureActivity : AppCompatActivity() {
private fun getListItems(): ArrayList<ListItem> {
val listItems = ArrayList<ListItem>(10)
var dateTime = DateTime.now().withTime(0, 0, 0, 0).plusDays(1)
var code = Formatter.getDayCodeFromTS((dateTime.millis / 1000).toInt())
var code = Formatter.getDayCodeFromTS(dateTime.seconds())
var day = Formatter.getDayTitle(this, code)
listItems.add(ListSection(day))
var time = dateTime.withHourOfDay(7)
listItems.add(ListEvent(1, (time.millis / 1000).toInt(), (time.plusMinutes(30).millis / 1000).toInt(), getString(R.string.sample_title_1), getString(R.string.sample_description_1)))
listItems.add(ListEvent(1, time.seconds(), time.plusMinutes(30).seconds(), getString(R.string.sample_title_1), getString(R.string.sample_description_1)))
time = dateTime.withHourOfDay(8)
listItems.add(ListEvent(2, (time.millis / 1000).toInt(), (time.plusHours(1).millis / 1000).toInt(), getString(R.string.sample_title_2), getString(R.string.sample_description_2)))
listItems.add(ListEvent(2, time.seconds(), time.plusHours(1).seconds(), getString(R.string.sample_title_2), getString(R.string.sample_description_2)))
dateTime = dateTime.plusDays(1)
code = Formatter.getDayCodeFromTS((dateTime.millis / 1000).toInt())
code = Formatter.getDayCodeFromTS(dateTime.seconds())
day = Formatter.getDayTitle(this, code)
listItems.add(ListSection(day))
time = dateTime.withHourOfDay(8)
listItems.add(ListEvent(3, (time.millis / 1000).toInt(), (time.plusHours(1).millis / 1000).toInt(), getString(R.string.sample_title_3), ""))
listItems.add(ListEvent(3, time.seconds(), time.plusHours(1).seconds(), getString(R.string.sample_title_3), ""))
time = dateTime.withHourOfDay(13)
listItems.add(ListEvent(4, (time.millis / 1000).toInt(), (time.plusHours(1).millis / 1000).toInt(), getString(R.string.sample_title_4), getString(R.string.sample_description_4)))
listItems.add(ListEvent(4, time.seconds(), time.plusHours(1).seconds(), getString(R.string.sample_title_4), getString(R.string.sample_description_4)))
time = dateTime.withHourOfDay(18)
listItems.add(ListEvent(5, (time.millis / 1000).toInt(), (time.plusMinutes(10).millis / 1000).toInt(), getString(R.string.sample_title_5), ""))
listItems.add(ListEvent(5, time.seconds(), time.plusMinutes(10).seconds(), getString(R.string.sample_title_5), ""))
return listItems
}

View File

@ -10,6 +10,7 @@ import android.widget.RemoteViewsService
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.R.id.event_item_holder
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID
import com.simplemobiletools.calendar.helpers.Formatter
@ -96,8 +97,8 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
override fun getItemId(position: Int) = position.toLong()
override fun onDataSetChanged() {
val fromTS = (DateTime().millis / 1000).toInt()
val toTS = (DateTime().plusMonths(6).millis / 1000).toInt()
val fromTS = DateTime().seconds()
val toTS = DateTime().plusMonths(6).seconds()
DBHelper(context).getEventsInBackground(fromTS, toTS, object : DBHelper.GetEventsListener {
override fun gotEvents(events: MutableList<Event>) {
val listItems = ArrayList<ListItem>(events.size)

View File

@ -64,7 +64,7 @@ private fun getNewTS(ts: Int, isMonthly: Boolean): Int {
while (dateTime.isBeforeNow) {
dateTime = if (isMonthly) dateTime.plusMonths(1) else dateTime.plusYears(1)
}
return (dateTime.millis / 1000).toInt()
return dateTime.seconds()
}
fun Context.scheduleNotification(event: Event) {

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.calendar.extensions
import org.joda.time.DateTime
fun DateTime.seconds() = (millis / 1000).toInt()

View File

@ -1,7 +0,0 @@
package com.simplemobiletools.calendar.extensions
import android.view.View
fun View.beVisibleIf(beVisible: Boolean) = if (beVisible) visibility = View.VISIBLE else visibility = View.GONE
fun View.beGoneIf(beGone: Boolean) = if (beGone) visibility = View.GONE else visibility = View.VISIBLE

View File

@ -11,8 +11,7 @@ import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.activities.SimpleActivity
import com.simplemobiletools.calendar.adapters.EventListAdapter
import com.simplemobiletools.calendar.extensions.beGoneIf
import com.simplemobiletools.calendar.extensions.beVisibleIf
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID
import com.simplemobiletools.calendar.helpers.Formatter
@ -21,6 +20,8 @@ import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.models.ListItem
import com.simplemobiletools.calendar.models.ListSection
import com.simplemobiletools.commons.extensions.beGoneIf
import com.simplemobiletools.commons.extensions.beVisibleIf
import kotlinx.android.synthetic.main.fragment_event_list.view.*
import org.joda.time.DateTime
import java.util.*
@ -49,8 +50,8 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
}
private fun checkEvents() {
val fromTS = (DateTime().millis / 1000).toInt()
val toTS = (DateTime().plusMonths(6).millis / 1000).toInt()
val fromTS = DateTime().seconds()
val toTS = DateTime().plusMonths(6).seconds()
DBHelper(context).getEvents(fromTS, toTS, this)
}

View File

@ -15,7 +15,6 @@ import android.widget.RelativeLayout
import android.widget.TextView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.extensions.beVisibleIf
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getAppropriateTheme
import com.simplemobiletools.calendar.extensions.removeUnderlineFlag
@ -24,6 +23,7 @@ import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Day
import com.simplemobiletools.commons.extensions.adjustAlpha
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.*

View File

@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.database.sqlite.SQLiteQueryBuilder
import android.text.TextUtils
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.extensions.updateWidgets
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.commons.extensions.getIntValue
@ -208,7 +209,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
fun getEventsAtReboot(): List<Event> {
val selection = "$COL_REMINDER_MINUTES != -1 AND ($COL_START_TS > ? OR $COL_REPEAT_INTERVAL != 0)"
val selectionArgs = arrayOf((DateTime.now().millis / 1000).toString())
val selectionArgs = arrayOf(DateTime.now().seconds().toString())
val cursor = getEventsCursor(selection, selectionArgs)
return fillEvents(cursor)
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import android.text.format.DateFormat
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.seconds
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat
@ -44,9 +45,9 @@ object Formatter {
fun getTimeFromTS(context: Context, ts: Int) = getTime(context, getDateTimeFromTS(ts))
fun getDayStartTS(dayCode: String) = (getLocalDateTimeFromCode(dayCode).millis / 1000).toInt()
fun getDayStartTS(dayCode: String) = getLocalDateTimeFromCode(dayCode).seconds()
fun getDayEndTS(dayCode: String) = (getLocalDateTimeFromCode(dayCode).plusDays(1).minusMinutes(1).millis / 1000).toInt()
fun getDayEndTS(dayCode: String) = getLocalDateTimeFromCode(dayCode).plusDays(1).minusMinutes(1).seconds()
fun getDayCodeFromTS(ts: Int) = getDateTimeFromTS(ts).toString(DAYCODE_PATTERN)

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.models.Day
import com.simplemobiletools.calendar.models.Event
@ -23,8 +24,8 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
fun updateMonthlyCalendar(targetDate: DateTime) {
mTargetDate = targetDate
val startTS = (mTargetDate.minusMonths(1).millis / 1000).toInt()
val endTS = (mTargetDate.plusMonths(1).millis / 1000).toInt()
val startTS = mTargetDate.minusMonths(1).seconds()
val endTS = mTargetDate.plusMonths(1).seconds()
DBHelper(mContext).getEvents(startTS, endTS, this)
}

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.interfaces.WeeklyCalendar
import com.simplemobiletools.calendar.models.Event
import org.joda.time.DateTime
@ -17,8 +18,8 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) :
fun updateWeeklyCalendar(targetDate: DateTime) {
mTargetDate = targetDate
val startTS = (mTargetDate.millis / 1000).toInt()
val endTS = (mTargetDate.plusWeeks(1).millis / 1000).toInt()
val startTS = mTargetDate.seconds()
val endTS = mTargetDate.plusWeeks(1).seconds()
DBHelper(mContext).getEvents(startTS, endTS, this)
}

View File

@ -2,8 +2,7 @@ package com.simplemobiletools.calendar.helpers
import android.content.Context
import android.util.SparseArray
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.interfaces.YearlyCalendar
import com.simplemobiletools.calendar.models.Event
import org.joda.time.DateTime
@ -13,8 +12,8 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val
fun getEvents(year: Int) {
val startDateTime = DateTime().withTime(0, 0, 0, 0).withDate(year, 1, 1)
val startTS = (startDateTime.millis / 1000).toInt()
val endTS = (startDateTime.plusYears(1).minusSeconds(1).millis / 1000).toInt()
val startTS = startDateTime.seconds()
val endTS = startDateTime.plusYears(1).minusSeconds(1).seconds()
DBHelper(context).getEvents(startTS, endTS, this)
}

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.calendar.models
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.*
import org.joda.time.DateTime
import java.io.Serializable
@ -21,7 +22,7 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
MONTH -> currStart.plusMonths(1)
else -> currStart.plusYears(1)
}
val newStartTS = (newStart.millis / 1000).toInt()
val newStartTS = newStart.seconds()
val newEndTS = newStartTS + (endTS - startTS)
startTS = newStartTS
endTS = newEndTS