Skip to content

Commit

Permalink
Added widget support for room location styles 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolONEOfficial committed Mar 15, 2019
1 parent 64690cb commit 221a436
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ enum Brightness {
Light
}

enum RoomLocationStyle {
Text,
Icon
}

AlarmManager manager;
PendingIntent updatePendingIntent, deleteOldPendingIntent;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand All @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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,
Expand All @@ -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);
Expand Down
31 changes: 14 additions & 17 deletions lib/timeline_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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] ==
Expand Down

0 comments on commit 221a436

Please sign in to comment.