fixed a couple of bugs, added colors to certain lecture types and fixed issue #5

This commit is contained in:
Odweta
2024-01-31 18:31:27 +01:00
parent d22c275326
commit 210471066b
4 changed files with 160 additions and 89 deletions
@@ -104,6 +104,13 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
rootLayout.addView(nameDateTextView) rootLayout.addView(nameDateTextView)
for (subj in day.subjects) { for (subj in day.subjects) {
val subjectBackground = when (subj.substitute) {
"0" -> if (subj.place != "Volno") { R.color.cal_week_subject } else { R.color.cal_week_free }
"1" -> if (subj.place != "Volno") { R.color.cal_week_substitute } else { R.color.cal_week_free }
"2" -> if (subj.place != "Volno") { R.color.cal_week_event } else { R.color.cal_week_free }
else -> R.color.cal_week_subject
}
val subjectDrawable = GradientDrawable() val subjectDrawable = GradientDrawable()
subjectDrawable.shape = GradientDrawable.RECTANGLE subjectDrawable.shape = GradientDrawable.RECTANGLE
subjectDrawable.cornerRadius = subjectDrawable.cornerRadius =
@@ -111,7 +118,7 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
subjectDrawable.setColor( subjectDrawable.setColor(
ContextCompat.getColor( ContextCompat.getColor(
requireContext(), requireContext(),
R.color.cal_week_subject subjectBackground
) )
) // Use your desired background color ) // Use your desired background color
@@ -256,6 +263,27 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
resources.getDimensionPixelSize(R.dimen.cal_week_separator_height) // Specify separator height in resources resources.getDimensionPixelSize(R.dimen.cal_week_separator_height) // Specify separator height in resources
) )
// drawables
val substituteDrawable = GradientDrawable()
substituteDrawable.shape = GradientDrawable.RECTANGLE
substituteDrawable.cornerRadius = resources.getDimension(R.dimen.corner_radius)
substituteDrawable.setColor(
ContextCompat.getColor(
requireContext(),
R.color.cal_week_substitute
)
)
val eventDrawable = GradientDrawable()
eventDrawable.shape = GradientDrawable.RECTANGLE
eventDrawable.cornerRadius = resources.getDimension(R.dimen.corner_radius)
eventDrawable.setColor(
ContextCompat.getColor(
requireContext(),
R.color.cal_week_event
)
)
// Attach an OnPreDrawListener to the rootLayout's ViewTreeObserver // Attach an OnPreDrawListener to the rootLayout's ViewTreeObserver
rootLayout.viewTreeObserver.addOnPreDrawListener(object : rootLayout.viewTreeObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener { ViewTreeObserver.OnPreDrawListener {
@@ -263,6 +291,15 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
// Remove the listener to avoid multiple callbacks // Remove the listener to avoid multiple callbacks
rootLayout.viewTreeObserver.removeOnPreDrawListener(this) rootLayout.viewTreeObserver.removeOnPreDrawListener(this)
if (dayLayout.height < rootLayout.height) {
val sepr = Space(requireContext())
sepr.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
rootLayout.height - dayLayout.height
)
rootLayout.addView(sepr)
}
// Call your function or perform an action here // Call your function or perform an action here
if (day.subjects[0].substitute != "2") { if (day.subjects[0].substitute != "2") {
subjectPlaceTextView.setPadding( subjectPlaceTextView.setPadding(
@@ -278,6 +315,15 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
subjectNameTextView.gravity = Gravity.CENTER_VERTICAL subjectNameTextView.gravity = Gravity.CENTER_VERTICAL
} }
// check for substitute hours and change the background
/*for (s in day.subjects) {
when (s.substitute) {
"1" -> subjectLayout.background = substituteDrawable
"2" -> subjectLayout.background = eventDrawable
else -> break
}
}*/
// Return true to continue with the drawing, or false to cancel // Return true to continue with the drawing, or false to cancel
return true return true
} }
@@ -319,6 +365,11 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
resources.getDimensionPixelSize(R.dimen.cal_week_separator_height) // Specify separator height in resources resources.getDimensionPixelSize(R.dimen.cal_week_separator_height) // Specify separator height in resources
) )
val dayEndSeparator2 = Space(requireContext())
dayEndSeparator2.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
resources.getDimensionPixelSize(R.dimen.cal_week_separator_height) // Specify separator height in resources
)
val endParams = LinearLayout.LayoutParams( val endParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
@@ -330,6 +381,7 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
if (day.subjects[0].substitute != "2") { if (day.subjects[0].substitute != "2") {
dayLayout.addView(dayEndSeparator) dayLayout.addView(dayEndSeparator)
dayLayout.addView(dayEndTextView) dayLayout.addView(dayEndTextView)
dayLayout.addView(dayEndSeparator2)
} }
scrollView.addView(dayLayout) scrollView.addView(dayLayout)
@@ -1,12 +1,13 @@
package com.odweta.solen package com.odweta.solen
import android.R.attr.maxHeight
import android.graphics.ImageDecoder import android.graphics.ImageDecoder
import android.graphics.drawable.AnimatedImageDrawable import android.graphics.drawable.AnimatedImageDrawable
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.View.MeasureSpec
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.ImageView import android.widget.ImageView
@@ -27,6 +28,7 @@ import java.nio.charset.Charset
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
class MainActivity : AppCompatActivity(), MainActivityListener { class MainActivity : AppCompatActivity(), MainActivityListener {
companion object { companion object {
val dayMap = mutableMapOf<Int, Day>() val dayMap = mutableMapOf<Int, Day>()
@@ -50,14 +52,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
private suspend fun setupAPI() { private suspend fun setupAPI() {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
//Log.d("debug", "setting up API...")
token = JSONObject(getToken()).get("access_token").toString() token = JSONObject(getToken()).get("access_token").toString()
if (token == "") { if (token == "") {
return@withContext return@withContext
} }
userData = getUserDataJSON() userData = getUserDataJSON()
studentId = getStudentId() studentId = getStudentId()
//Log.d("debug", "API set up!")
} }
} }
@@ -137,14 +137,11 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
password = acc.password password = acc.password
val token = getToken() val token = getToken()
//Log.d("debug", "token: '$token'")
!JSONObject(token).has("error") !JSONObject(token).has("error")
} }
} }
private fun onSignIn() { private fun onSignIn() {
//Log.d("debug", "onSignIn called!")
val acc = Account( val acc = Account(
findViewById<EditText>(R.id.usernameField).text.toString(), findViewById<EditText>(R.id.usernameField).text.toString(),
findViewById<EditText>(R.id.passwordField).text.toString(), findViewById<EditText>(R.id.passwordField).text.toString(),
@@ -157,9 +154,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
lifecycleScope.launch { lifecycleScope.launch {
val valid = accountIsValid(acc) val valid = accountIsValid(acc)
//Log.d("debug", "account valid? $valid")
//Log.d("debug", acc.username)
//Log.d("debug", acc.password)
if (valid) { if (valid) {
launch() launch()
@@ -171,7 +165,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
} }
//Log.d("debug", "onSignIn finished!")
} }
} }
@@ -223,7 +216,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
file.createNewFile() file.createNewFile()
false false
} else { } else {
//Log.d("debug", "account file found")
// it exists, so it shall be parsed // it exists, so it shall be parsed
val acc: Account = parseAccountDataFile(file) val acc: Account = parseAccountDataFile(file)
username = acc.username username = acc.username
@@ -257,7 +249,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
private fun launchPhase2(week: List<Day>) { private fun launchPhase2(week: List<Day>) {
days = week days = week
for ((i, day) in week.withIndex()) { for ((i, day) in week.withIndex()) {
//Log.d("debug", "$i, ${day.name}\n${day.date}\n${day.subjects}")
dayMap[i] = day dayMap[i] = day
} }
@@ -282,17 +273,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
*/ */
val accSaved: Boolean = haveSavedAccount() val accSaved: Boolean = haveSavedAccount()
//val valid: Boolean = accountIsValid(Account(username, password, 0))
//Log.d("debug", "username: $username")
//Log.d("debug", "password: $password")
//Log.d("debug", "saved? $accSaved")//; valid? $valid")
if (!accSaved) { if (!accSaved) {
//Log.d("debug", "showing sign in screen") // show the sign in screen
showSignInScreen() showSignInScreen()
} else { } else {
//Log.d("debug", "launching timetable") // launch!
launchPhase1() launchPhase1()
} }
} }
@@ -348,7 +334,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
try { try {
val resp = client.newCall(request).execute() val resp = client.newCall(request).execute()
val respString = resp.body?.string().toString() val respString = resp.body?.string().toString()
//Log.d("debug", "userData: $respString")
respString respString
} catch (e: IOException) { } catch (e: IOException) {
"""{"error": "network"}""" """{"error": "network"}"""
@@ -362,18 +347,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
} }
private suspend fun getTimeTableJSON(): String { private suspend fun getTimeTableJSON(): String {
// I now figured out how to use the SOL API (kinda), yay
//Log.d("debug", "getting timetable")
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
val userIsParent = userIsParent() val userIsParent = userIsParent()
//Log.d("debug", "userIsParent? $userIsParent")
var url = timeTableUrl var url = timeTableUrl
if (userIsParent) { if (userIsParent) {
url += "?studentId=$studentId" url += "?studentId=$studentId"
} }
//Log.d("debug", "url: $url")
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
@@ -384,9 +363,7 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
try { try {
val resp = client.newCall(request).execute() val resp = client.newCall(request).execute()
val respString = resp.body?.string().toString() resp.body?.string().toString()
//Log.d("debug", "timetable: $respString")
respString
} catch (e: IOException) { } catch (e: IOException) {
"""{"error": "network"}""" """{"error": "network"}"""
} }
@@ -405,15 +382,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
val week = mutableListOf<Day>() val week = mutableListOf<Day>()
if (jsonObject.has("error")) { if (jsonObject.has("error")) {
//Log.d("debug error", "network")
return week return week
} }
//Log.d("debug", "no network error\n\n$jsonString")
// Access the "body" array from the main JSON object // Access the "body" array from the main JSON object
val bodyArray = jsonObject.getJSONArray("days") val bodyArray = jsonObject.getJSONArray("days")
// Convert the JSON array to a list of JSON objects // Convert the JSON array to a list of JSON objects
val jsonObjectList = mutableListOf<JSONObject>() val jsonObjectList = mutableListOf<JSONObject>()
for (i in 0 until bodyArray.length()) { for (i in 0 until bodyArray.length()) {
@@ -422,6 +396,7 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
} }
for (obj in jsonObjectList) { for (obj in jsonObjectList) {
// for each day
val dateRAW = obj.get("date").toString() val dateRAW = obj.get("date").toString()
var dayStr = dateRAW.split("T")[0].split("-")[2] var dayStr = dateRAW.split("T")[0].split("-")[2]
var monthStr = dateRAW.split("T")[0].split("-")[1] var monthStr = dateRAW.split("T")[0].split("-")[1]
@@ -455,73 +430,113 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
} }
for (subj in subjectJSONList) { for (subj in subjectJSONList) {
// for each subject in one particular day
val subject = Subject() val subject = Subject()
val kind = subj.getJSONObject("hourKind").get("id") val kind = subj.getJSONObject("hourKind").get("id")
val type = subj.getJSONObject("hourType").get("id")
if ( if (
(subj.getJSONObject("hourType").get("id") == "SUPLOVANA") || (type == "SUPLOVANA") ||
(kind != "SUPLOVANI" && kind != "") (type != "SUPLOVANI" && type != "ROZVRH")
) { ) {
if (kind == "ZRUSENI_VYUKY_ROZVRH") { when (kind) {
subject.name = subj.get("title").toString() "ZRUSENI_VYUKY_ROZVRH" -> {
subject.place = "" subject.name = subj.get("title").toString()
subject.teacher = "" subject.place = ""
subject.substitute = "2" // 2 means school event subject.teacher = ""
subject.substitute = "2" // 2 means school event
val hourspan = subj.getJSONArray("detailHours").length() val hourspan = subj.getJSONArray("detailHours").length()
subject.number = "1 - $hourspan" subject.number = "1 - $hourspan"
subject.start = subj.get("beginTime").toString() subject.start = subj.get("beginTime").toString()
subject.start = subject.start.slice(0..subject.start.length-4) subject.start = subject.start.slice(0..subject.start.length-4)
subject.end = subj.get("endTime").toString() subject.end = subj.get("endTime").toString()
subject.end = subject.end.slice(0..subject.end.length-4) subject.end = subject.end.slice(0..subject.end.length-4)
subjects.add(subject) subjects.add(subject)
}
"S_NAHRADOU_SKOL_AKCE" -> {
subject.name = subj.get("title").toString()
subject.place = ""
subject.teacher = ""
subject.substitute = "2" // 2 means school event
val hourspan = subj.getJSONArray("detailHours").length()
if (hourspan == 1) {
val detailHours = subj.getJSONArray("detailHours")
subject.number = detailHours.getJSONObject(0).get("id").toString()
subject.start = detailHours.getJSONObject(0).get("timeFrom").toString()
subject.start = subject.start.slice(0..subject.start.length - 4)
subject.end = detailHours.getJSONObject(0).get("timeto").toString()
subject.end = subject.end.slice(0..subject.end.length - 4)
subjects.add(subject)
} else {
for (i in 0..<hourspan) {
val newSubject = Subject()
newSubject.name = subject.name
newSubject.place = subject.place
newSubject.teacher = subject.teacher
newSubject.substitute = subject.substitute
val detailHours = subj.getJSONArray("detailHours")
newSubject.number = detailHours.getJSONObject(i).get("id").toString()
newSubject.start =
detailHours.getJSONObject(i).get("timeFrom").toString()
newSubject.start =
newSubject.start.slice(0..newSubject.start.length - 4)
newSubject.end = detailHours.getJSONObject(i).get("timeto").toString()
newSubject.end = newSubject.end.slice(0..newSubject.end.length - 4)
subjects.add(newSubject)
}
}
}
} }
continue
}
//Log.d("debug", subj.toString())
subject.name = subj.getJSONObject("subject").get("abbrev").toString()
subject.place = subj.getJSONArray("rooms").getJSONObject(0).get("abbrev").toString()
subject.teacher = "" +
"${subj.getJSONArray("teachers").getJSONObject(0).get("name")} " +
"${subj.getJSONArray("teachers").getJSONObject(0).get("surname")}"
if (subj.getJSONObject("hourType").get("id").toString() == "ROZVRH") {
subject.substitute = "0"
} else if (subj.getJSONObject("hourType").get("id").toString() == "SUPLOVANI") {
subject.substitute = "1"
}
val hourspan = subj.getJSONArray("detailHours").length()
if (hourspan == 1) {
val detailHours = subj.getJSONArray("detailHours")
subject.number = detailHours.getJSONObject(0).get("id").toString()
subject.start = detailHours.getJSONObject(0).get("timeFrom").toString()
subject.start = subject.start.slice(0..subject.start.length-4)
subject.end = detailHours.getJSONObject(0).get("timeto").toString()
subject.end = subject.end.slice(0..subject.end.length-4)
//Log.d("push", "pushing day...")
subjects.add(subject)
} else { } else {
for (i in 0..<hourspan) { subject.name = subj.getJSONObject("subject").get("abbrev").toString()
val newSubject = Subject() subject.place =
newSubject.name = subject.name subj.getJSONArray("rooms").getJSONObject(0).get("abbrev").toString()
newSubject.place = subject.place subject.teacher = "" +
newSubject.teacher = subject.teacher "${subj.getJSONArray("teachers").getJSONObject(0).get("name")} " +
newSubject.substitute = subject.substitute "${subj.getJSONArray("teachers").getJSONObject(0).get("surname")}"
if (type == "ROZVRH") {
subject.substitute = "0"
} else if (type == "SUPLOVANI") {
subject.substitute = "1"
}
val hourspan = subj.getJSONArray("detailHours").length()
if (hourspan == 1) {
val detailHours = subj.getJSONArray("detailHours") val detailHours = subj.getJSONArray("detailHours")
newSubject.number = detailHours.getJSONObject(i).get("id").toString() subject.number = detailHours.getJSONObject(0).get("id").toString()
//Log.d("debug", "${date}\n\n$detailHours\n\n${subject.number}") subject.start = detailHours.getJSONObject(0).get("timeFrom").toString()
newSubject.start = detailHours.getJSONObject(i).get("timeFrom").toString() subject.start = subject.start.slice(0..subject.start.length - 4)
newSubject.start = newSubject.start.slice(0..newSubject.start.length-4) subject.end = detailHours.getJSONObject(0).get("timeto").toString()
newSubject.end = detailHours.getJSONObject(i).get("timeto").toString() subject.end = subject.end.slice(0..subject.end.length - 4)
newSubject.end = newSubject.end.slice(0..newSubject.end.length-4) subjects.add(subject)
//Log.d("push", "pushing day...") } else {
subjects.add(newSubject) for (i in 0..<hourspan) {
val newSubject = Subject()
newSubject.name = subject.name
newSubject.place = subject.place
newSubject.teacher = subject.teacher
newSubject.substitute = subject.substitute
val detailHours = subj.getJSONArray("detailHours")
newSubject.number = detailHours.getJSONObject(i).get("id").toString()
newSubject.start =
detailHours.getJSONObject(i).get("timeFrom").toString()
newSubject.start =
newSubject.start.slice(0..newSubject.start.length - 4)
newSubject.end = detailHours.getJSONObject(i).get("timeto").toString()
newSubject.end = newSubject.end.slice(0..newSubject.end.length - 4)
subjects.add(newSubject)
}
} }
} }
} }
/* /*
// rust pseudocode
let mut res: Vec<i32> = vec![]; let mut res: Vec<i32> = vec![];
for i in 0..v.len()-1 { for i in 0..v.len()-1 {
if v[i] == v[i+1]-1 { if v[i] == v[i+1]-1 {
@@ -20,7 +20,7 @@
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.Solen.PopupOverlay" app:popupTheme="@style/Theme.Solen.PopupOverlay"
app:theme="@style/Theme.Solen.AppBarOverlay"> android:theme="@style/Theme.Solen.AppBarOverlay">
</androidx.appcompat.widget.Toolbar> </androidx.appcompat.widget.Toolbar>
+6 -2
View File
@@ -8,6 +8,10 @@
<color name="teal_700">#FFF6F1EE</color> <color name="teal_700">#FFF6F1EE</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="cal_week_bg">#696969</color>
<color name="cal_week_subject">#f1f1f1</color> <color name="cal_week_bg">#FF696969</color>
<color name="cal_week_subject">#FFF1F1F1</color>
<color name="cal_week_substitute">#FFDD2200</color>
<color name="cal_week_event">#FFFDAA22</color>
<color name="cal_week_free">#FFAAFF66</color>
</resources> </resources>