Расписание ИСП-24/9 с achtng.ru и большие карточки

This commit is contained in:
Beluga737 committed 2026-12-04 22:22:23 +07:00
1 parent 10f53a173a
commit 7d69cf5a74
6 files changed
+420 -144

No files matched your search

+1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
@@ -1,25 +1,253 @@
package com.example.helponizer
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class Raspisanie : BaseActivity() {
private lateinit var scheduleList: LinearLayout
private lateinit var scheduleProgress: ProgressBar
private lateinit var scheduleStatus: TextView
private lateinit var scheduleRetryButton: MaterialButton
private val executor: ExecutorService = Executors.newSingleThreadExecutor()
private val ruLocale = Locale.forLanguageTag("ru")
private val dayDateFormat = DateTimeFormatter.ofPattern("dd.MM", ruLocale)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_raspisanie)
scheduleList = findViewById(R.id.scheduleList)
scheduleProgress = findViewById(R.id.scheduleProgress)
scheduleStatus = findViewById(R.id.scheduleStatus)
scheduleRetryButton = findViewById(R.id.scheduleRetryButton)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { view, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
view.setPadding(systemBars.left, systemBars.top, systemBars.right, 0)
insets
}
scheduleRetryButton.setOnClickListener { loadSchedule() }
loadSchedule()
}
override fun onResume() {
super.onResume()
TextSizeManager.applyTo(findViewById(R.id.main))
}
override fun onDestroy() {
executor.shutdownNow()
super.onDestroy()
}
private fun loadSchedule() {
scheduleProgress.visibility = View.VISIBLE
scheduleStatus.visibility = View.GONE
scheduleRetryButton.visibility = View.GONE
scheduleList.removeAllViews()
executor.execute {
try {
val monday = LocalDate.now().with(DayOfWeek.MONDAY)
val days = ScheduleRepository.fetchWeek(monday, monday.plusDays(5))
runOnUiThread { renderSchedule(days) }
} catch (e: Exception) {
runOnUiThread { renderError() }
}
}
}
private fun renderSchedule(days: List<ScheduleDay>) {
scheduleProgress.visibility = View.GONE
scheduleList.removeAllViews()
if (days.isEmpty()) {
scheduleStatus.visibility = View.VISIBLE
scheduleStatus.setText(R.string.schedule_empty)
scheduleRetryButton.visibility = View.VISIBLE
applyTextSize()
return
}
for (day in days) {
addDayHeader(day)
for (lesson in day.lessons) {
scheduleList.addView(createLessonCard(lesson))
}
}
scheduleStatus.visibility = View.GONE
scheduleRetryButton.visibility = View.GONE
applyTextSize()
}
private fun renderError() {
scheduleProgress.visibility = View.GONE
scheduleList.removeAllViews()
scheduleStatus.visibility = View.VISIBLE
scheduleStatus.setText(R.string.schedule_error)
scheduleRetryButton.visibility = View.VISIBLE
applyTextSize()
}
private fun applyTextSize() {
TextSizeManager.applyTo(findViewById(R.id.main))
}
private fun addDayHeader(day: ScheduleDay) {
val header = TextView(this).apply {
layoutParams = marginParams(topDp = 18, bottomDp = 8)
text = getString(R.string.schedule_day_header, getString(dayNameRes(day.dayOfWeek)), day.date.format(dayDateFormat))
setTextColor(resolveColor(com.google.android.material.R.attr.colorOnSurface, Color.BLACK))
setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
setTypeface(typeface, Typeface.BOLD)
gravity = Gravity.START
}
scheduleList.addView(header)
}
private fun createLessonCard(lesson: ScheduleLesson): MaterialCardView {
val card = MaterialCardView(this).apply {
layoutParams = marginParams(topDp = 8, bottomDp = 8)
radius = 18f * resources.displayMetrics.density
cardElevation = 1f * resources.displayMetrics.density
strokeWidth = (1 * resources.displayMetrics.density).toInt()
strokeColor = resolveColor(com.google.android.material.R.attr.colorSurfaceVariant, Color.LTGRAY)
setCardBackgroundColor(resolveColor(com.google.android.material.R.attr.colorSurface, Color.WHITE))
isClickable = false
}
val content = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(18.dp, 16.dp, 18.dp, 16.dp)
}
val titleRow = LinearLayout(this).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
}
titleRow.addView(
TextView(this).apply {
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
text = getString(R.string.schedule_pair, lesson.number)
setTextColor(resolveColor(androidx.appcompat.R.attr.colorPrimary, Color.DKGRAY))
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
setTypeface(typeface, Typeface.BOLD)
}
)
titleRow.addView(
TextView(this).apply {
text = getString(R.string.schedule_time, lesson.startAt, lesson.endAt)
setTextColor(resolveColor(com.google.android.material.R.attr.colorOnSurfaceVariant, Color.DKGRAY))
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
}
)
content.addView(titleRow)
for (line in lesson.lines) {
content.addView(createLineView(line))
}
card.addView(content)
return card
}
private fun createLineView(line: ScheduleLine): LinearLayout {
val container = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(0, 10.dp, 0, 0)
}
container.addView(
TextView(this).apply {
text = line.subject.ifBlank { getString(R.string.schedule_unknown) }
setTextColor(resolveColor(com.google.android.material.R.attr.colorOnSurface, Color.BLACK))
setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
setTypeface(typeface, Typeface.BOLD)
setLineSpacing(3f * resources.displayMetrics.density, 1f)
}
)
val room = listOf(line.roomNumber, line.roomName).filter { it.isNotBlank() }.joinToString(" · ")
if (room.isNotBlank()) {
container.addView(metaText(getString(R.string.schedule_room, room)))
}
if (line.teacher.isNotBlank()) {
container.addView(metaText(getString(R.string.schedule_teacher, line.teacher)))
}
if (line.subgroup > 0) {
container.addView(metaText(getString(R.string.schedule_subgroup, line.subgroup)))
}
if (line.lineCode.isNotBlank()) {
container.addView(metaText(getString(R.string.schedule_line, line.lineCode)))
}
if (line.volName.isNotBlank()) {
container.addView(metaText(line.volName))
}
return container
}
private fun metaText(text: String): TextView {
return TextView(this).apply {
this.text = text
setTextColor(resolveColor(com.google.android.material.R.attr.colorOnSurfaceVariant, Color.DKGRAY))
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
setLineSpacing(2f * resources.displayMetrics.density, 1f)
setPadding(0, 3.dp, 0, 0)
}
}
private fun marginParams(topDp: Int, bottomDp: Int): LinearLayout.LayoutParams {
return LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
).apply {
topMargin = topDp.dp
bottomMargin = bottomDp.dp
}
}
private fun dayNameRes(day: DayOfWeek): Int = when (day) {
DayOfWeek.MONDAY -> R.string.schedule_monday
DayOfWeek.TUESDAY -> R.string.schedule_tuesday
DayOfWeek.WEDNESDAY -> R.string.schedule_wednesday
DayOfWeek.THURSDAY -> R.string.schedule_thursday
DayOfWeek.FRIDAY -> R.string.schedule_friday
DayOfWeek.SATURDAY -> R.string.schedule_saturday
DayOfWeek.SUNDAY -> R.string.schedule_sunday
}
private fun resolveColor(attr: Int, fallback: Int): Int {
val value = TypedValue()
if (!theme.resolveAttribute(attr, value, true)) return fallback
return if (value.resourceId != 0) ContextCompat.getColor(this, value.resourceId) else value.data
}
private val Int.dp: Int
get() = (this * resources.displayMetrics.density).toInt()
}
@@ -0,0 +1,120 @@
package com.example.helponizer
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter
data class ScheduleLine(
val subject: String,
val roomNumber: String,
val roomName: String,
val teacher: String,
val subgroup: Int,
val lineCode: String,
val volName: String
)
data class ScheduleLesson(
val number: Int,
val startAt: String,
val endAt: String,
val lines: List<ScheduleLine>
)
data class ScheduleDay(
val date: LocalDate,
val dayOfWeek: DayOfWeek,
val lessons: List<ScheduleLesson>
)
object ScheduleRepository {
private const val BASE_URL = "https://www.achtng.ru"
private const val GROUP_ID = 10 // ИСП-24/9
private val dateFormat = DateTimeFormatter.ISO_LOCAL_DATE
class ScheduleException(message: String) : Exception(message)
@Throws(ScheduleException::class)
fun fetchWeek(startDate: LocalDate, endDate: LocalDate): List<ScheduleDay> {
val days = mutableListOf<ScheduleDay>()
var totalDays = 0
var failedDays = 0
var date = startDate
while (!date.isAfter(endDate)) {
totalDays++
val day = fetchDay(date)
if (day == null) {
failedDays++
} else if (day.lessons.isNotEmpty()) {
days.add(day)
}
date = date.plusDays(1)
}
if (days.isEmpty() && totalDays > 0 && failedDays == totalDays) {
throw ScheduleException("Не удалось загрузить расписание с achtng.ru")
}
return days
}
private fun fetchDay(date: LocalDate): ScheduleDay? {
val url = "$BASE_URL/rest/v1/profile/schedule/g/$GROUP_ID/${date.format(dateFormat)}"
var connection: HttpURLConnection? = null
return try {
connection = URL(url).openConnection() as HttpURLConnection
connection.apply {
requestMethod = "GET"
connectTimeout = 10_000
readTimeout = 10_000
setRequestProperty("Accept", "application/json")
setRequestProperty("User-Agent", "Helponizer/1.0")
}
if (connection.responseCode !in 200..299) return null
val body = connection.inputStream.bufferedReader().use { it.readText() }
parseDay(date, body)
} catch (e: Exception) {
null
} finally {
connection?.disconnect()
}
}
private fun parseDay(date: LocalDate, body: String): ScheduleDay {
val json = JSONObject(body)
val lessons = mutableListOf<ScheduleLesson>()
val keys = json.keys()
while (keys.hasNext()) {
val key = keys.next()
val lessonJson = json.optJSONObject(key) ?: continue
val linesJson = lessonJson.optJSONArray("lines") ?: JSONArray()
val lines = mutableListOf<ScheduleLine>()
for (i in 0 until linesJson.length()) {
val line = linesJson.optJSONObject(i) ?: continue
lines.add(
ScheduleLine(
subject = line.optString("subj_name", ""),
roomNumber = line.optString("room_num", ""),
roomName = line.optString("room_name", ""),
teacher = line.optString("employee_name", ""),
subgroup = line.optInt("sub_group", 0),
lineCode = line.optString("line_code", ""),
volName = line.optString("vol_name", "")
)
)
}
lessons.add(
ScheduleLesson(
number = key.toIntOrNull() ?: lessons.size + 1,
startAt = lessonJson.optString("start_at", ""),
endAt = lessonJson.optString("end_at", ""),
lines = lines
)
)
}
lessons.sortBy { it.number }
return ScheduleDay(date, date.dayOfWeek, lessons)
}
}
@@ -42,6 +42,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="@string/home_title" />
<ImageButton
+53 -138
View File
@@ -1,155 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:fillViewport="true"
android:orientation="vertical"
android:paddingStart="24dp"
android:paddingTop="32dp"
android:paddingEnd="24dp"
android:paddingBottom="24dp"
tools:context=".Raspisanie">
<LinearLayout
<TextView
style="@style/ScreenTitle"
android:text="@string/schedule_title" />
<TextView
android:id="@+id/scheduleSource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="24dp"
android:paddingTop="32dp"
android:paddingEnd="24dp"
android:paddingBottom="24dp">
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/schedule_source_achtng"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="13sp" />
<TextView
style="@style/ScreenTitle"
android:text="@string/schedule_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/schedule_monday"
android:textColor="?attr/colorOnSurface"
android:textSize="19sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_monday_lesson_1"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<ProgressBar
android:id="@+id/scheduleProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="18dp"
android:visibility="gone" />
<TextView
android:id="@+id/scheduleStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:gravity="center"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="15sp"
android:visibility="gone" />
<com.google.android.material.button.MaterialButton
android:id="@+id/scheduleRetryButton"
style="@style/PrimaryButton"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:text="@string/schedule_retry"
android:visibility="gone" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/scheduleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_monday_lesson_2"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/schedule_tuesday"
android:textColor="?attr/colorOnSurface"
android:textSize="19sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_tuesday_lesson_1"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_tuesday_lesson_2"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/schedule_wednesday"
android:textColor="?attr/colorOnSurface"
android:textSize="19sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_wednesday_lesson_1"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/bg_tips_card">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:text="@string/schedule_wednesday_lesson_2"
android:textColor="?attr/colorOnSurface"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
+17 -6
View File
@@ -16,15 +16,26 @@
<!-- Schedule screen -->
<string name="schedule_title">Расписание занятий</string>
<string name="schedule_source_achtng">Источник: achtng.ru, группа ИСП-24/9</string>
<string name="schedule_monday">Понедельник</string>
<string name="schedule_monday_lesson_1">"1 пара 08:30 - 10:00 \n Информатика \n Кабинет 205"</string>
<string name="schedule_monday_lesson_2">"2 пара 10:10 - 11:40 \n Математика \n Кабинет 310"</string>
<string name="schedule_tuesday">Вторник</string>
<string name="schedule_tuesday_lesson_1">"1 пара 08:30 - 10:00 \n Основы алгоритмизации \n Кабинет 208"</string>
<string name="schedule_tuesday_lesson_2">"2 пара 10:10 - 11:40 \n Разработка приложений \n Кабинет 215"</string>
<string name="schedule_wednesday">Среда</string>
<string name="schedule_wednesday_lesson_1">"1 пара 08:30 - 10:00 \n Базы данных \n Кабинет 212"</string>
<string name="schedule_wednesday_lesson_2">"2 пара 10:10 - 11:40 \n Иностранный язык \n Кабинет 305"</string>
<string name="schedule_thursday">Четверг</string>
<string name="schedule_friday">Пятница</string>
<string name="schedule_saturday">Суббота</string>
<string name="schedule_sunday">Воскресенье</string>
<string name="schedule_loading">Загружаю расписание…</string>
<string name="schedule_error">Не удалось загрузить расписание с achtng.ru</string>
<string name="schedule_retry">Попробовать снова</string>
<string name="schedule_empty">На эту неделю пар нет</string>
<string name="schedule_day_header">%1$s, %2$s</string>
<string name="schedule_pair">%1$d пара</string>
<string name="schedule_time">%1$s - %2$s</string>
<string name="schedule_room">Кабинет: %1$s</string>
<string name="schedule_teacher">Преподаватель: %1$s</string>
<string name="schedule_subgroup">Подгруппа %1$d</string>
<string name="schedule_line">Линия: %1$s</string>
<string name="schedule_unknown">Без названия</string>
<!-- Tips screen -->
<string name="tips_title">Полезная Информация</string>