Skip to content

Commit

Permalink
fix widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Sep 18, 2022
1 parent 854f40c commit cfed2a2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class TimetableAdapter(
init {
val calendar = Calendar.getInstance()
var day = calendar.get(Calendar.DAY_OF_WEEK)
val firstDay = calendar.firstDayOfWeek
if (firstDay == Calendar.SUNDAY) {
if (calendar.firstDayOfWeek == Calendar.SUNDAY) {
if (day == 1)
day = 7
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.ethosa.ktc.Preferences
import com.ethosa.ktc.R
import com.ethosa.ktc.college.CollegeApi
import com.ethosa.ktc.college.CollegeCallback
import com.ethosa.ktc.college.teacher.TeacherDay
import com.ethosa.ktc.college.teacher.TeacherTimetable
import com.ethosa.ktc.ui.activities.MainActivity
import com.google.gson.Gson
Expand Down Expand Up @@ -104,7 +105,13 @@ class TeacherTimetableWidget : AppWidgetProvider() {
val teacherId = Preferences.teacherId
val branchId = Preferences.branch.id
val calendar = Calendar.getInstance()
val weekday = calendar.get(Calendar.DAY_OF_WEEK)
var weekday = calendar.get(Calendar.DAY_OF_WEEK)
if (calendar.firstDayOfWeek == Calendar.SUNDAY) {
if (weekday == 1)
weekday = 7
else
weekday--
}

CollegeApi.fetchTeacherTimetable(branchId, teacherId, object : CollegeCallback {
@SuppressLint("SetTextI18n")
Expand All @@ -118,24 +125,31 @@ class TeacherTimetableWidget : AppWidgetProvider() {
views.setTextViewText(
R.id.timetable_widget_title, "Ошибка расписания"
)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
} catch (e: Exception) {
views.setTextViewText(
R.id.timetable_widget_title, "Неизвестная ошибка"
)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}
println(weekday)
// Get current day timetable
val day =
when {
weekday >= 2 -> timetable.week[weekday-2]
weekday > 1 -> timetable.week[1]
else -> timetable.week[0]
}
val day: TeacherDay? = when (weekday) {
7 -> null
1 -> timetable.week[0]
else -> timetable.week[weekday-1]
}
if (day == null) {
views.setTextViewText(R.id.timetable_widget_title, "Выходной")
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}

// Setup widget
views.setTextViewText(R.id.timetable_widget_title, "${timetable.teacher} - ${day.title}")
views.setTextViewText(
R.id.timetable_widget_title, "${timetable.teacher} - ${day.title}"
)
views.removeAllViews(R.id.timetable_widget_lessons)

// Setup views
Expand Down
28 changes: 20 additions & 8 deletions app/src/main/java/com/ethosa/ktc/ui/widgets/TimetableWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.ethosa.ktc.Preferences
import com.ethosa.ktc.R
import com.ethosa.ktc.college.CollegeApi
import com.ethosa.ktc.college.CollegeCallback
import com.ethosa.ktc.college.timetable.Day
import com.ethosa.ktc.college.timetable.Week
import com.ethosa.ktc.ui.activities.MainActivity
import com.google.gson.Gson
Expand Down Expand Up @@ -104,7 +105,13 @@ class TimetableWidget : AppWidgetProvider() {
// Load last group ID
val groupId = Preferences.group.id
val calendar = Calendar.getInstance()
val weekday = calendar.get(Calendar.DAY_OF_WEEK)
var weekday = calendar.get(Calendar.DAY_OF_WEEK)
if (calendar.firstDayOfWeek == Calendar.SUNDAY) {
if (weekday == 1)
weekday = 7
else
weekday--
}
val now = "${calendar.get(Calendar.HOUR_OF_DAY)}:${Calendar.MINUTE}"

val dateFormat = SimpleDateFormat("mm:ss")
Expand All @@ -121,21 +128,26 @@ class TimetableWidget : AppWidgetProvider() {
views.setTextViewText(
R.id.timetable_widget_title, "Ошибка расписания"
)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
} catch (e: Exception) {
views.setTextViewText(
R.id.timetable_widget_title, "Неизвестная ошибка"
)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}
println(weekday)
// Get current day timetable
val day =
when {
weekday >= 2 -> timetable.days[weekday-2]
weekday > 1 -> timetable.days[1]
else -> timetable.days[0]
}
val day: Day? = when (weekday) {
7 -> null
1 -> timetable.days[0]
else -> timetable.days[weekday-1]
}
if (day == null) {
views.setTextViewText(R.id.timetable_widget_title, "Выходной")
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}

// Setup widget
views.setTextViewText(R.id.timetable_widget_title, day.title)
Expand Down
26 changes: 13 additions & 13 deletions app/src/main/res/xml/timetable_widget_info.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:description="@string/app_widget_description"
tools:initialKeyguardLayout="@layout/widget_timetable"
tools:initialLayout="@layout/widget_timetable"
tools:minWidth="300dp"
tools:minHeight="140dp"
tools:previewImage="@drawable/ic_globe_alt"
tools:previewLayout="@layout/widget_timetable"
tools:resizeMode="horizontal|vertical"
tools:targetCellWidth="4"
tools:targetCellHeight="3"
tools:updatePeriodMillis="1800000"
tools:widgetCategory="home_screen"
android:description="@string/app_widget_description"
android:initialKeyguardLayout="@layout/widget_timetable"
android:initialLayout="@layout/widget_timetable"
android:minWidth="300dp"
android:minHeight="140dp"
android:previewImage="@drawable/ic_globe_alt"
android:previewLayout="@layout/widget_timetable"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="4"
android:targetCellHeight="3"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen"
tools:targetApi="s" />

0 comments on commit cfed2a2

Please sign in to comment.