Skip to content

Commit

Permalink
feat: add timetable block
Browse files Browse the repository at this point in the history
TODO: change block color
  • Loading branch information
callasio committed Oct 4, 2024
1 parent 948e07b commit 0ce5817
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.widget.RemoteViews
import org.sparcs.otlplus.api.Data
import org.sparcs.otlplus.api.NextLectureData

/**
* Implementation of App Widget functionality.
Expand Down Expand Up @@ -38,10 +38,10 @@ internal fun updateNextLectureWidget(
) {
// Construct the RemoteViews object
RemoteViews(context.packageName, R.layout.next_lecture_widget).let {
it.setTextViewText(R.id.nextLectureDate, Data.nextLectureDate)
it.setTextViewText(R.id.nextLectureName, Data.nextLectureName)
it.setTextViewText(R.id.nextLecturePlace, Data.nextLecturePlace)
it.setTextViewText(R.id.nextLectureProfessor, Data.nextLectureProfessor)
it.setTextViewText(R.id.nextLectureDate, NextLectureData.nextLectureDate)
it.setTextViewText(R.id.nextLectureName, NextLectureData.nextLectureName)
it.setTextViewText(R.id.nextLecturePlace, NextLectureData.nextLecturePlace)
it.setTextViewText(R.id.nextLectureProfessor, NextLectureData.nextLectureProfessor)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, it)
}
Expand Down
75 changes: 71 additions & 4 deletions android/app/src/main/java/org/sparcs/otlplus/TimetableWidget.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package org.sparcs.otlplus

import android.annotation.SuppressLint
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.util.TypedValue
import android.widget.RemoteViews
import org.sparcs.otlplus.api.Lecture
import org.sparcs.otlplus.api.LocalTime
import org.sparcs.otlplus.api.TimetableData
import org.sparcs.otlplus.api.WeekDays

val timeTableColumns = listOf(
R.id.time_table_column_1,
R.id.time_table_column_2,
R.id.time_table_column_3,
R.id.time_table_column_4,
R.id.time_table_column_5,
)

data class TimeTableElement(
val length: Float,
val lecture: Lecture?
)

/**
* Implementation of App Widget functionality.
Expand All @@ -29,16 +48,64 @@ class TimetableWidget : AppWidgetProvider() {
}
}

@SuppressLint("NewApi")
internal fun updateTimetableWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetText = context.getString(R.string.appwidget_text)
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.timetable_widget)
views.setTextViewText(R.id.appwidget_text, widgetText)

for (timetableColumn in timeTableColumns) {
views.removeAllViews(timetableColumn)
}

val weekTimetable = createTimeTable(TimetableData.lectures)

for ((weekday, dayTimetable) in weekTimetable.withIndex()) {
for (timeTableElement in dayTimetable) {
val blockView = when(timeTableElement.lecture) {
null -> RemoteViews(context.packageName, R.layout.blank_view)
else -> RemoteViews(context.packageName, R.layout.timetable_block).apply {
setTextViewText(R.id.timetable_block_lecture_name, timeTableElement.lecture.name)
}
}

blockView.setViewLayoutHeight(
R.id.timetable_block_root,
timeTableElement.length * 36,
TypedValue.COMPLEX_UNIT_DIP)

views.addView(timeTableColumns[weekday], blockView)
}
}

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

fun createTimeTable(lectures: List<Lecture>): List<List<TimeTableElement>> {
val timetable = List(5) { mutableListOf<TimeTableElement>() }

for (dayIndex in WeekDays.entries.toTypedArray().indices) {
val day = WeekDays.entries[dayIndex]

val dailyLectures = lectures.flatMap { lecture ->
lecture.timeBlocks.filter { it.weekday == day }.map { it to lecture }
}.sortedBy { it.first.start.hoursFloat }

var currentTime = LocalTime(9, 0)

for ((timeBlock, lecture) in dailyLectures) {
if (timeBlock.start.hoursFloat > currentTime.hoursFloat) {
val freeTimeLength = timeBlock.start.hoursFloat - currentTime.hoursFloat
timetable[dayIndex].add(TimeTableElement(freeTimeLength, null))
}
val lectureLength = timeBlock.end.hoursFloat - timeBlock.start.hoursFloat
timetable[dayIndex].add(TimeTableElement(lectureLength, lecture))
currentTime = timeBlock.end
}
}

return timetable
}
30 changes: 30 additions & 0 deletions android/app/src/main/java/org/sparcs/otlplus/api/Lecture.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sparcs.otlplus.api

enum class WeekDays {
Mon,
Tue,
Wed,
Thu,
Fri,
}

data class LocalTime(
val hours: Int,
val minutes: Int,
) {
val hoursFloat: Float
get() = hours + minutes / 60f
}

data class TimeBlock(
val weekday: WeekDays,
val start: LocalTime,
val end: LocalTime
)

data class Lecture(
val name: String,
val timeBlocks: List<TimeBlock>,
val place: String,
val professor: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sparcs.otlplus.api

object Data { // TODO: Get data using api
object NextLectureData { // TODO: Get data using api
val nextLectureDate
get() = "다음주 월요일"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sparcs.otlplus.api

object TimelineData {
}
41 changes: 41 additions & 0 deletions android/app/src/main/java/org/sparcs/otlplus/api/TimetableData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.sparcs.otlplus.api

object TimetableData {
val lectures: List<Lecture>
get() = listOf(
Lecture(
name = "프로그래밍 기초",
place = "(E11)창의학습관 308",
professor = "김교수",
timeBlocks = listOf(
TimeBlock(
weekday = WeekDays.Mon,
start = LocalTime(9, 0),
end = LocalTime(12, 0),
),
TimeBlock(
weekday = WeekDays.Wed,
start = LocalTime(10, 30),
end = LocalTime(12, 0),
),
)
),
Lecture(
name = "지속가능 사회 인프라 시스템과 환경의 이해",
place = "(E11) 창의학습관 208",
professor = "김교수",
timeBlocks = listOf(
TimeBlock(
weekday = WeekDays.Mon,
start = LocalTime(16, 0),
end = LocalTime(17, 30),
),
TimeBlock(
weekday = WeekDays.Wed,
start = LocalTime(16, 0),
end = LocalTime(17, 30),
),
)
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#E6CBEC"/> <!-- Background color -->
<corners android:radius="1.66dp"/> <!-- Corner radius -->
</shape>
11 changes: 11 additions & 0 deletions android/app/src/main/res/layout/blank_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/timetable_block_root">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>
18 changes: 18 additions & 0 deletions android/app/src/main/res/layout/timetable_block.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
android:id="@+id/timetable_block_root">

<TextView
android:id="@+id/timetable_block_lecture_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="11sp"
android:ellipsize="end"
android:textColor="@color/gray0"
android:padding="4dp"
android:background="@drawable/timetable_block_rounded_corner"/>

</RelativeLayout>
Loading

0 comments on commit 0ce5817

Please sign in to comment.