Fix the events order
This commit is contained in:
parent
6150dd7e22
commit
6ea97e7724
@ -18,7 +18,7 @@ import kotlin.collections.ArrayList
|
||||
class EventRepository(val context: Context) {
|
||||
private val realm by lazy { Realm.getDefaultInstance() }
|
||||
|
||||
fun saveEvents(eventList: ArrayList<Event>) {
|
||||
fun saveEvents(eventList: List<Event>) {
|
||||
realm.executeTransaction { realm ->
|
||||
realm.where(Event::class.java).findAll().deleteAllFromRealm()
|
||||
realm.copyToRealm(eventList)
|
||||
|
@ -80,4 +80,27 @@ object CalendarHelper {
|
||||
.filter { (!Preferences.showOnlyBusyEvents || it.availability != CalendarContract.EventsEntity.AVAILABILITY_FREE) }
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun List<Event>.sortEvents(): List<Event> {
|
||||
return sortedWith { event: Event, event1: Event ->
|
||||
val date = Calendar.getInstance().apply { timeInMillis = event.startDate }
|
||||
val date1 = Calendar.getInstance().apply { timeInMillis = event1.startDate }
|
||||
|
||||
if (date.get(Calendar.DAY_OF_YEAR) == date1.get(Calendar.DAY_OF_YEAR) && date.get(
|
||||
Calendar.YEAR) == date1.get(Calendar.YEAR)
|
||||
) {
|
||||
if (event.allDay && event1.allDay) {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
} else if (event.allDay) {
|
||||
1
|
||||
} else if (event1.allDay) {
|
||||
-1
|
||||
} else {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
}
|
||||
} else {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -37,9 +37,6 @@ object GlanceProviderHelper {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Log.d("ciao", providers.toList().toString())
|
||||
|
||||
return ArrayList(providers.toList())
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper.applyFilters
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper.sortEvents
|
||||
import com.tommasoberlose.anotherwidget.models.Event
|
||||
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||
import com.tommasoberlose.anotherwidget.ui.fragments.MainFragment
|
||||
@ -44,8 +45,17 @@ class UpdateCalendarJob : JobIntentService() {
|
||||
set(Calendar.HOUR_OF_DAY, 0)
|
||||
}
|
||||
val limit = Calendar.getInstance().apply {
|
||||
timeInMillis = begin.timeInMillis
|
||||
add(Calendar.DAY_OF_YEAR, 2)
|
||||
when (Preferences.showUntil) {
|
||||
0 -> add(Calendar.HOUR, 3)
|
||||
1 -> add(Calendar.HOUR, 6)
|
||||
2 -> add(Calendar.HOUR, 12)
|
||||
3 -> add(Calendar.DAY_OF_MONTH, 1)
|
||||
4 -> add(Calendar.DAY_OF_MONTH, 3)
|
||||
5 -> add(Calendar.DAY_OF_MONTH, 7)
|
||||
6 -> add(Calendar.MINUTE, 30)
|
||||
7 -> add(Calendar.HOUR, 1)
|
||||
else -> add(Calendar.HOUR, 6)
|
||||
}
|
||||
}
|
||||
|
||||
if (!checkGrantedPermission(
|
||||
@ -95,34 +105,16 @@ class UpdateCalendarJob : JobIntentService() {
|
||||
}
|
||||
}
|
||||
|
||||
val filteredEventList = eventList
|
||||
val sortedEvents = eventList.sortEvents()
|
||||
val filteredEventList = sortedEvents
|
||||
.applyFilters()
|
||||
|
||||
if (filteredEventList.isEmpty()) {
|
||||
eventRepository.resetNextEventData()
|
||||
eventRepository.clearEvents()
|
||||
} else {
|
||||
eventList.sortWith(Comparator { event: Event, event1: Event ->
|
||||
val date = Calendar.getInstance().apply { timeInMillis = event.startDate }
|
||||
val date1 = Calendar.getInstance().apply { timeInMillis = event1.startDate }
|
||||
|
||||
if (date.get(Calendar.DAY_OF_YEAR) == date1.get(Calendar.DAY_OF_YEAR) && date.get(Calendar.YEAR) == date1.get(Calendar.YEAR)) {
|
||||
if (event.allDay && event1.allDay) {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
} else if (event.allDay) {
|
||||
1
|
||||
} else if (event1.allDay) {
|
||||
-1
|
||||
} else {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
}
|
||||
} else {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
}
|
||||
})
|
||||
|
||||
eventRepository.saveEvents(
|
||||
eventList
|
||||
sortedEvents
|
||||
)
|
||||
eventRepository.saveNextEventData(filteredEventList.first())
|
||||
}
|
||||
|
@ -350,8 +350,12 @@ class MainWidget : AppWidgetProvider() {
|
||||
Constants.GlanceProviderId.NOTIFICATIONS -> {
|
||||
if (Preferences.showNotifications && ActiveNotificationsHelper.showLastNotification()) {
|
||||
try {
|
||||
val remotePackageContext = context.createPackageContext(Preferences.lastNotificationPackage, 0)
|
||||
val icon = ContextCompat.getDrawable(remotePackageContext, Preferences.lastNotificationIcon)
|
||||
if (Preferences.lastNotificationIcon != 0) {
|
||||
val remotePackageContext = context.createPackageContext(Preferences.lastNotificationPackage, 0)
|
||||
ContextCompat.getDrawable(
|
||||
remotePackageContext,
|
||||
Preferences.lastNotificationIcon)
|
||||
}
|
||||
val notificationIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
@ -606,7 +610,7 @@ class MainWidget : AppWidgetProvider() {
|
||||
dayDiff++
|
||||
} else if (startCal.get(Calendar.HOUR_OF_DAY) == endCal.get(Calendar.HOUR_OF_DAY) && startCal.get(
|
||||
Calendar.MINUTE
|
||||
) >= endCal.get(Calendar.MINUTE)
|
||||
) > endCal.get(Calendar.MINUTE)
|
||||
) {
|
||||
dayDiff++
|
||||
}
|
||||
@ -618,8 +622,14 @@ class MainWidget : AppWidgetProvider() {
|
||||
context.getString(R.string.day_char)
|
||||
)
|
||||
}
|
||||
v.next_event_date.text =
|
||||
String.format("%s - %s%s", startHour, endHour, multipleDay)
|
||||
|
||||
if (nextEvent.startDate != nextEvent.endDate) {
|
||||
v.next_event_date.text =
|
||||
String.format("%s - %s%s", startHour, endHour, multipleDay)
|
||||
} else {
|
||||
v.next_event_date.text =
|
||||
String.format("%s", startHour)
|
||||
}
|
||||
|
||||
} else {
|
||||
val flags: Int =
|
||||
@ -724,8 +734,8 @@ class MainWidget : AppWidgetProvider() {
|
||||
Constants.GlanceProviderId.NOTIFICATIONS -> {
|
||||
if (Preferences.showNotifications && ActiveNotificationsHelper.showLastNotification()) {
|
||||
try {
|
||||
val remotePackageContext = context.createPackageContext(Preferences.lastNotificationPackage, 0)
|
||||
if (Preferences.lastNotificationIcon != 0) {
|
||||
val remotePackageContext = context.createPackageContext(Preferences.lastNotificationPackage, 0)
|
||||
val icon = ContextCompat.getDrawable(remotePackageContext,
|
||||
Preferences.lastNotificationIcon)
|
||||
v.second_row_icon.isVisible = true
|
||||
|
@ -22,7 +22,8 @@
|
||||
android:id="@+id/empty_layout">
|
||||
<TextView
|
||||
android:id="@+id/empty_date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_height="wrap_content"
|
||||
android:lines="1"
|
||||
android:textColor="@color/colorPrimary"
|
||||
@ -36,7 +37,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:visibility="gone"
|
||||
android:layout_marginStart="8dp"
|
||||
android:paddingStart="8dp"
|
||||
android:id="@+id/weather">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
@ -118,7 +119,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:visibility="gone"
|
||||
android:layout_marginStart="8dp"
|
||||
android:paddingStart="8dp"
|
||||
android:id="@+id/special_weather">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
|
@ -205,7 +205,7 @@
|
||||
<string name="battery_low_warning">Low battery level</string>
|
||||
<string name="daily_steps_counter">%d steps so far</string>
|
||||
<string name="charging">Charging</string>
|
||||
<string name="charged">Charged</string>
|
||||
<string name="charged">Fully charged</string>
|
||||
<string name="providers">Providers</string>
|
||||
<string name="glance_info">Glance info will show up only when there are no events displayed and only when a few conditions are verified.</string>
|
||||
<string name="settings_music_players_filter_title">Music Players</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user