Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Remember latest grade to show if there's new grades
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed May 15, 2023
1 parent 035159a commit 439c6e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ object Constants {

const val CHANGELOG_INACTIVE = "changelog_html_inactive"
const val CHANGELOG_ACTIVE = "changelog_html_active"

const val LATEST_GRADE_ID = "latest_grade_id"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.qhy040404.libraryonetap.data.tools

data class Grade(
val id: Int,
val name: String,
val code: String,
val credit: Double,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.absinthe.libraries.utils.extensions.getColorByAttr
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.qhy040404.libraryonetap.LibraryOneTapApp
import com.qhy040404.libraryonetap.R
import com.qhy040404.libraryonetap.constant.Constants
import com.qhy040404.libraryonetap.constant.GlobalValues
import com.qhy040404.libraryonetap.constant.URLManager
import com.qhy040404.libraryonetap.data.tools.Grade
Expand All @@ -30,6 +31,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.saket.cascade.CascadePopupMenu
import org.json.JSONObject
import java.io.File

class GradesActivity : BaseEduActivity(), MenuProvider {
private val semesters = mutableListOf<Semester>()
Expand Down Expand Up @@ -97,6 +99,7 @@ class GradesActivity : BaseEduActivity(), MenuProvider {
val gradesJsonObject = JSONObject(gradesData)
val semesterArray = gradesJsonObject.optJSONArray("semesters")!!
val grades = gradesJsonObject.optJSONObject("semesterId2studentGrades")!!
val gradesIds = mutableListOf<Int>()
for (i in 0 until semesterArray.length()) {
val semesterId = semesterArray.optJSONObject(i).optInt("id")
semesters.add(
Expand All @@ -108,8 +111,10 @@ class GradesActivity : BaseEduActivity(), MenuProvider {
buildList {
for (j in 0 until count) {
val currentCourse = currentSemester.optJSONObject(j)!!
gradesIds.add(currentCourse.optInt("id"))
add(
Grade(
currentCourse.optInt("id"),
currentCourse.optJSONObject("course")!!
.optString("nameZh"),
currentCourse.optJSONObject("course")!!
Expand All @@ -128,6 +133,31 @@ class GradesActivity : BaseEduActivity(), MenuProvider {
)
)
}
File(dataDir, Constants.LATEST_GRADE_ID).apply {
if (exists().not()) {
createNewFile()
writeText(gradesIds.max().toString())
} else {
val latestId = readText().toInt()
val newGrades = buildList {
semesters.forEach { semester ->
addAll(semester.courses.filter { it.id > latestId })
}
}
if (newGrades.isNotEmpty()) {
semesters.add(
Semester(
Int.MAX_VALUE,
"新成绩",
newGrades
)
)
delete()
createNewFile()
writeText(newGrades.maxOf { it.id }.toString())
}
}
}
semesters.sortByDescending { it.id }
syncRecycleView()
}
Expand All @@ -147,19 +177,23 @@ class GradesActivity : BaseEduActivity(), MenuProvider {
Card(
String.format(
R.string.gr_stat.getString(),
semesters.first().courses.sumOf { it.credit },
semesters.first { it.id != Int.MAX_VALUE }.courses.sumOf { it.credit },
GradesUtils.calculateWeightedAverage(
buildList {
semesters.forEach {
addAll(it.courses)
if (it.id != Int.MAX_VALUE) {
addAll(it.courses)
}
}
}
),
GradesUtils.calculateAverageGP(
this@GradesActivity,
buildList {
semesters.forEach {
addAll(it.courses)
if (it.id != Int.MAX_VALUE) {
addAll(it.courses)
}
}
}
)
Expand Down

0 comments on commit 439c6e9

Please sign in to comment.