From 221a4365dc37d9b49d49e6d64c10294de45bdb07 Mon Sep 17 00:00:00 2001 From: CoolONEOfficial Date: Fri, 15 Mar 2019 14:00:40 +0300 Subject: [PATCH] Added widget support for room location styles :tada: --- .../ranepatimetable/WidgetProvider.java | 22 +++- .../ranepatimetable/WidgetService.java | 113 ++++++++++++------ lib/timeline_element.dart | 31 +++-- 3 files changed, 109 insertions(+), 57 deletions(-) diff --git a/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetProvider.java b/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetProvider.java index 924d308..5bafc21 100644 --- a/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetProvider.java +++ b/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetProvider.java @@ -72,6 +72,11 @@ enum Brightness { Light } + enum RoomLocationStyle { + Text, + Icon + } + AlarmManager manager; PendingIntent updatePendingIntent, deleteOldPendingIntent; @@ -184,14 +189,19 @@ private void createAlarmClock(Context ctx) { - ((int) duration.toMinutes())); var alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM); - alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, firstLesson.getString(firstLesson.getColumnIndex(Timeline.PREFIX_LESSON + Timeline.LessonModel.COLUMN_LESSON_TITLE))); + alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, firstLesson.getString( + firstLesson.getColumnIndex( + Timeline.PREFIX_LESSON + + Timeline.LessonModel.COLUMN_LESSON_TITLE + ))); alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, date.get(Calendar.HOUR)); alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, date.get(Calendar.MINUTE)); alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true); alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(alarmIntent); } - } else Toast.makeText(ctx, R.string.noLessons, Toast.LENGTH_LONG).show(); + } else + Toast.makeText(ctx, R.string.noLessons, Toast.LENGTH_LONG).show(); } private void createCalendarEvents(final Context ctx) { @@ -307,7 +317,8 @@ public void onReceive(Context ctx, Intent intent) { createAlarmClock(ctx); } else if (Objects.equals(intent.getAction(), IntentAction.CreateCalendarEvents.action)) { createCalendarEvents(ctx); - } else if (Objects.equals(intent.getAction(), IntentAction.DayNext.action) || Objects.equals(intent.getAction(), IntentAction.DayPrev.action)) { + } else if (Objects.equals(intent.getAction(), IntentAction.DayNext.action) + || Objects.equals(intent.getAction(), IntentAction.DayPrev.action)) { Calendar now; do { dateOffset += Objects.equals(intent.getAction(), IntentAction.DayNext.action) @@ -316,7 +327,8 @@ public void onReceive(Context ctx, Intent intent) { now = GregorianCalendar.getInstance(); now.add(Calendar.DATE, dateOffset); - } while (now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY); + } + while (now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY); refreshWidget(ctx); } @@ -343,6 +355,8 @@ static class Theme { private static final String FLUTTER_PREFIX = "flutter."; enum PrefsIds { + RoomLocationStyle("room_location_style"), + WidgetTranslucent("widget_translucent"), ThemePrimary("theme_primary"), diff --git a/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetService.java b/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetService.java index 234ae04..f06d1e3 100644 --- a/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetService.java +++ b/android/app/src/main/java/ru/coolone/ranepatimetable/WidgetService.java @@ -21,6 +21,22 @@ import lombok.extern.java.Log; import lombok.var; +import static ru.coolone.ranepatimetable.Timeline.COLUMN_DATE; +import static ru.coolone.ranepatimetable.Timeline.COLUMN_FIRST; +import static ru.coolone.ranepatimetable.Timeline.COLUMN_GROUP; +import static ru.coolone.ranepatimetable.Timeline.COLUMN_LAST; +import static ru.coolone.ranepatimetable.Timeline.COLUMN_MERGE_TOP; +import static ru.coolone.ranepatimetable.Timeline.LessonAction; +import static ru.coolone.ranepatimetable.Timeline.LessonModel; +import static ru.coolone.ranepatimetable.Timeline.Location; +import static ru.coolone.ranepatimetable.Timeline.PREFIX_FINISH; +import static ru.coolone.ranepatimetable.Timeline.PREFIX_LESSON; +import static ru.coolone.ranepatimetable.Timeline.PREFIX_ROOM; +import static ru.coolone.ranepatimetable.Timeline.PREFIX_START; +import static ru.coolone.ranepatimetable.Timeline.PREFIX_TEACHER; +import static ru.coolone.ranepatimetable.Timeline.RoomModel; +import static ru.coolone.ranepatimetable.Timeline.TeacherModel; +import static ru.coolone.ranepatimetable.Timeline.TimeOfDayModel; import static ru.coolone.ranepatimetable.WidgetProvider.defaultTheme; import static ru.coolone.ranepatimetable.WidgetProvider.getPrefs; import static ru.coolone.ranepatimetable.WidgetProvider.widgetSize; @@ -128,7 +144,7 @@ private Bitmap buildItemBitmap(Context ctx) { bgRectPaint.setColor(theme.background); canvas.drawRoundRect(rect, dpScale * rectCornersRadius, dpScale * rectCornersRadius, bgRectPaint); - var mergeTop = cursor.getInt(cursor.getColumnIndex(Timeline.COLUMN_MERGE_TOP)) != 0; + var mergeTop = cursor.getInt(cursor.getColumnIndex(COLUMN_MERGE_TOP)) != 0; if (mergeTop) { var mergePaint = new Paint(); mergePaint.setAntiAlias(true); @@ -150,8 +166,8 @@ private Bitmap buildItemBitmap(Context ctx) { ); } - var first = cursor.getInt(cursor.getColumnIndex(Timeline.COLUMN_FIRST)) != 0; - var last = cursor.getInt(cursor.getColumnIndex(Timeline.COLUMN_LAST)) != 0; + var first = cursor.getInt(cursor.getColumnIndex(COLUMN_FIRST)) != 0; + var last = cursor.getInt(cursor.getColumnIndex(COLUMN_LAST)) != 0; var circleX = dpScale * (rectMargins * 2 + circleRadius + 68); var circleY = h / 2 + dpScale * (rectMargins / 2f); @@ -243,25 +259,29 @@ else if (last) Character.toChars( cursor.getInt( cursor.getColumnIndex( - Timeline.PREFIX_LESSON - + Timeline.LessonModel.COLUMN_LESSON_ICON) + PREFIX_LESSON + + LessonModel.COLUMN_LESSON_ICON) ) ) ), circleX, circleY + dpScale * (10 + translateIcon), iconPaint ); // Draw room location icon - var roomLocation = Timeline.Location.values()[cursor.getInt(cursor.getColumnIndex( - Timeline.PREFIX_ROOM - + Timeline.RoomModel.COLUMN_ROOM_LOCATION) - )]; - iconPaint.setColor(theme.textPrimary); - iconPaint.setTextSize(dpScale * 20); - canvas.drawText( - String.valueOf( - Character.toChars(roomLocation.iconCodePoint) - ), dpScale * 25, dpScale * 70, iconPaint - ); + if (WidgetProvider.RoomLocationStyle.values()[ + (int) getPrefs(ctx).getLong(WidgetProvider.PrefsIds.RoomLocationStyle.prefId, 0) + ] == WidgetProvider.RoomLocationStyle.Icon) { + var roomLocation = Location.values()[cursor.getInt(cursor.getColumnIndex( + PREFIX_ROOM + + RoomModel.COLUMN_ROOM_LOCATION) + )]; + iconPaint.setColor(theme.textPrimary); + iconPaint.setTextSize(dpScale * 20); + canvas.drawText( + String.valueOf( + Character.toChars(roomLocation.iconCodePoint) + ), dpScale * 25, dpScale * 70, iconPaint + ); + } return bitmap; } @@ -282,29 +302,29 @@ public RemoteViews getViewAt(int position) { // Get the data for this position from the content provider if (cursor.moveToPosition(position)) { var date = new GregorianCalendar(); - date.setTimeInMillis(cursor.getLong(cursor.getColumnIndex(Timeline.COLUMN_DATE))); + date.setTimeInMillis(cursor.getLong(cursor.getColumnIndex(COLUMN_DATE))); - var start = new Timeline.TimeOfDayModel( - cursor.getInt(cursor.getColumnIndex(Timeline.PREFIX_START + Timeline.TimeOfDayModel.COLUMN_TIMEOFDAY_HOUR)), - cursor.getInt(cursor.getColumnIndex(Timeline.PREFIX_START + Timeline.TimeOfDayModel.COLUMN_TIMEOFDAY_MINUTE)) + var start = new TimeOfDayModel( + cursor.getInt(cursor.getColumnIndex(PREFIX_START + TimeOfDayModel.COLUMN_TIMEOFDAY_HOUR)), + cursor.getInt(cursor.getColumnIndex(PREFIX_START + TimeOfDayModel.COLUMN_TIMEOFDAY_MINUTE)) ); - var finish = new Timeline.TimeOfDayModel( - cursor.getInt(cursor.getColumnIndex(Timeline.PREFIX_FINISH + Timeline.TimeOfDayModel.COLUMN_TIMEOFDAY_HOUR)), - cursor.getInt(cursor.getColumnIndex(Timeline.PREFIX_FINISH + Timeline.TimeOfDayModel.COLUMN_TIMEOFDAY_MINUTE)) + var finish = new TimeOfDayModel( + cursor.getInt(cursor.getColumnIndex(PREFIX_FINISH + TimeOfDayModel.COLUMN_TIMEOFDAY_HOUR)), + cursor.getInt(cursor.getColumnIndex(PREFIX_FINISH + TimeOfDayModel.COLUMN_TIMEOFDAY_MINUTE)) ); rv.setTextViewText(R.id.widget_item_lesson_title, cursor.getString( cursor.getColumnIndex( - Timeline.PREFIX_LESSON - + Timeline.LessonModel.COLUMN_LESSON_TITLE + PREFIX_LESSON + + LessonModel.COLUMN_LESSON_TITLE ) ) ); - var teacherName = cursor.getString(cursor.getColumnIndex(Timeline.PREFIX_TEACHER + Timeline.TeacherModel.COLUMN_TEACHER_NAME)); - var teacherSurname = cursor.getString(cursor.getColumnIndex(Timeline.PREFIX_TEACHER + Timeline.TeacherModel.COLUMN_TEACHER_SURNAME)); - var teacherPatronymic = cursor.getString(cursor.getColumnIndex(Timeline.PREFIX_TEACHER + Timeline.TeacherModel.COLUMN_TEACHER_PATRONYMIC)); - var group = cursor.getString(cursor.getColumnIndex(Timeline.COLUMN_GROUP)); + var teacherName = cursor.getString(cursor.getColumnIndex(PREFIX_TEACHER + TeacherModel.COLUMN_TEACHER_NAME)); + var teacherSurname = cursor.getString(cursor.getColumnIndex(PREFIX_TEACHER + TeacherModel.COLUMN_TEACHER_SURNAME)); + var teacherPatronymic = cursor.getString(cursor.getColumnIndex(PREFIX_TEACHER + TeacherModel.COLUMN_TEACHER_PATRONYMIC)); + var group = cursor.getString(cursor.getColumnIndex(COLUMN_GROUP)); var user = WidgetProvider.SearchItemTypeId.values()[(int) getPrefs(ctx).getLong( WidgetProvider.PrefsIds.PrimarySearchItemPrefix.prefId + WidgetProvider.PrefsIds.ItemType.prefId, @@ -316,15 +336,36 @@ public RemoteViews getViewAt(int position) { : group); rv.setTextViewText(R.id.widget_item_start, String.format(getCurrentLocale(), "%d:%02d", start.hour, start.minute)); rv.setTextViewText(R.id.widget_item_finish, String.format(getCurrentLocale(), "%d:%02d", finish.hour, finish.minute)); - rv.setTextViewText(R.id.widget_item_room_number, String.valueOf( - cursor.getString(cursor.getColumnIndex( - Timeline.PREFIX_ROOM - + Timeline.RoomModel.COLUMN_ROOM_NUMBER) - ))); - var action = cursor.getString(cursor.getColumnIndex(Timeline.PREFIX_LESSON + Timeline.LessonModel.PREFIX_LESSON_ACTION + Timeline.LessonAction.COLUMN_LESSON_TYPE_TITLE)); + + var prefix = ""; + if (WidgetProvider.RoomLocationStyle.values()[ + (int) getPrefs(ctx).getLong(WidgetProvider.PrefsIds.RoomLocationStyle.prefId, 0) + ] == WidgetProvider.RoomLocationStyle.Text) + switch (Location.values()[cursor.getInt(cursor.getColumnIndex( + PREFIX_ROOM + + RoomModel.COLUMN_ROOM_LOCATION))]) { + case Hotel: + prefix = "П8-"; + break; + case StudyHostel: + prefix = "СО-"; + break; + } + rv.setTextViewText(R.id.widget_item_room_number, + prefix + cursor.getString(cursor.getColumnIndex( + PREFIX_ROOM + + RoomModel.COLUMN_ROOM_NUMBER) + )); + var action = cursor.getString( + cursor.getColumnIndex( + PREFIX_LESSON + + LessonModel.PREFIX_LESSON_ACTION + + LessonAction.COLUMN_LESSON_TYPE_TITLE + )); if (action == null) rv.setViewVisibility(R.id.widget_item_lesson_action, View.GONE); - else rv.setTextViewText(R.id.widget_item_lesson_action, action); + else + rv.setTextViewText(R.id.widget_item_lesson_action, action); rv.setTextColor(R.id.widget_item_lesson_action, theme.textPrimary); rv.setTextColor(R.id.widget_item_lesson_title, theme.textPrimary); diff --git a/lib/timeline_element.dart b/lib/timeline_element.dart index 425d3e7..3d3245b 100644 --- a/lib/timeline_element.dart +++ b/lib/timeline_element.dart @@ -81,25 +81,22 @@ class TimelineElement extends StatelessWidget { ); Widget _buildRoomLocation(BuildContext ctx) { - String prefix; - switch (model.room.location) { - case RoomLocation.Academy: - prefix = ""; - break; - case RoomLocation.StudyHostel: - prefix = "СО-"; - break; - case RoomLocation.Hotel: - prefix = "П8-"; - break; - } + String prefix = ""; + if (RoomLocationStyle + .values[prefs.getInt(PrefsIds.ROOM_LOCATION_STYLE) ?? 0] == + RoomLocationStyle.Text) + switch (model.room.location) { + case RoomLocation.StudyHostel: + prefix = "СО-"; + break; + case RoomLocation.Hotel: + prefix = "П8-"; + break; + default: + } return Text( - RoomLocationStyle - .values[prefs.getInt(PrefsIds.ROOM_LOCATION_STYLE) ?? 0] == - RoomLocationStyle.Text - ? prefix + model.room.number - : model.room.number, + prefix + model.room.number, style: Theme.of(ctx).textTheme.subtitle, textAlign: RoomLocationStyle .values[prefs.getInt(PrefsIds.ROOM_LOCATION_STYLE) ?? 0] ==