From 210471066bded99e0dc8c9255e3919d27d075cdc Mon Sep 17 00:00:00 2001 From: Odweta Date: Wed, 31 Jan 2024 18:31:27 +0100 Subject: [PATCH] fixed a couple of bugs, added colors to certain lecture types and fixed issue #5 --- .../java/com/odweta/solen/CalWeekFragment.kt | 54 ++++- .../java/com/odweta/solen/MainActivity.kt | 185 ++++++++++-------- .../app/src/main/res/layout/activity_main.xml | 2 +- mobile/app/src/main/res/values/colors.xml | 8 +- 4 files changed, 160 insertions(+), 89 deletions(-) diff --git a/mobile/app/src/main/java/com/odweta/solen/CalWeekFragment.kt b/mobile/app/src/main/java/com/odweta/solen/CalWeekFragment.kt index 24e54a1..7e6bf37 100644 --- a/mobile/app/src/main/java/com/odweta/solen/CalWeekFragment.kt +++ b/mobile/app/src/main/java/com/odweta/solen/CalWeekFragment.kt @@ -104,6 +104,13 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) { rootLayout.addView(nameDateTextView) 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() subjectDrawable.shape = GradientDrawable.RECTANGLE subjectDrawable.cornerRadius = @@ -111,7 +118,7 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) { subjectDrawable.setColor( ContextCompat.getColor( requireContext(), - R.color.cal_week_subject + subjectBackground ) ) // 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 ) + // 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 rootLayout.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener { @@ -263,6 +291,15 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) { // Remove the listener to avoid multiple callbacks 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 if (day.subjects[0].substitute != "2") { subjectPlaceTextView.setPadding( @@ -278,6 +315,15 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) { 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 } @@ -319,6 +365,11 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) { LinearLayout.LayoutParams.MATCH_PARENT, 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( 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") { dayLayout.addView(dayEndSeparator) dayLayout.addView(dayEndTextView) + dayLayout.addView(dayEndSeparator2) } scrollView.addView(dayLayout) diff --git a/mobile/app/src/main/java/com/odweta/solen/MainActivity.kt b/mobile/app/src/main/java/com/odweta/solen/MainActivity.kt index 7001092..4fa88e9 100644 --- a/mobile/app/src/main/java/com/odweta/solen/MainActivity.kt +++ b/mobile/app/src/main/java/com/odweta/solen/MainActivity.kt @@ -1,12 +1,13 @@ package com.odweta.solen +import android.R.attr.maxHeight import android.graphics.ImageDecoder import android.graphics.drawable.AnimatedImageDrawable import android.os.Bundle -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View +import android.view.View.MeasureSpec import android.widget.Button import android.widget.EditText import android.widget.ImageView @@ -27,6 +28,7 @@ import java.nio.charset.Charset import java.time.LocalDateTime import java.time.format.DateTimeFormatter + class MainActivity : AppCompatActivity(), MainActivityListener { companion object { val dayMap = mutableMapOf() @@ -50,14 +52,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener { private suspend fun setupAPI() { return withContext(Dispatchers.IO) { - //Log.d("debug", "setting up API...") token = JSONObject(getToken()).get("access_token").toString() if (token == "") { return@withContext } userData = getUserDataJSON() studentId = getStudentId() - //Log.d("debug", "API set up!") } } @@ -137,14 +137,11 @@ class MainActivity : AppCompatActivity(), MainActivityListener { password = acc.password val token = getToken() - //Log.d("debug", "token: '$token'") !JSONObject(token).has("error") } } private fun onSignIn() { - //Log.d("debug", "onSignIn called!") - val acc = Account( findViewById(R.id.usernameField).text.toString(), findViewById(R.id.passwordField).text.toString(), @@ -157,9 +154,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener { lifecycleScope.launch { val valid = accountIsValid(acc) - //Log.d("debug", "account valid? $valid") - //Log.d("debug", acc.username) - //Log.d("debug", acc.password) if (valid) { launch() @@ -171,7 +165,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener { Toast.LENGTH_LONG ).show() } - //Log.d("debug", "onSignIn finished!") } } @@ -223,7 +216,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener { file.createNewFile() false } else { - //Log.d("debug", "account file found") // it exists, so it shall be parsed val acc: Account = parseAccountDataFile(file) username = acc.username @@ -257,7 +249,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener { private fun launchPhase2(week: List) { days = week for ((i, day) in week.withIndex()) { - //Log.d("debug", "$i, ${day.name}\n${day.date}\n${day.subjects}") dayMap[i] = day } @@ -282,17 +273,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener { */ 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) { - //Log.d("debug", "showing sign in screen") + // show the sign in screen showSignInScreen() } else { - //Log.d("debug", "launching timetable") + // launch! launchPhase1() } } @@ -348,7 +334,6 @@ class MainActivity : AppCompatActivity(), MainActivityListener { try { val resp = client.newCall(request).execute() val respString = resp.body?.string().toString() - //Log.d("debug", "userData: $respString") respString } catch (e: IOException) { """{"error": "network"}""" @@ -362,18 +347,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener { } 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) { val userIsParent = userIsParent() - //Log.d("debug", "userIsParent? $userIsParent") var url = timeTableUrl if (userIsParent) { url += "?studentId=$studentId" } - //Log.d("debug", "url: $url") val request = Request.Builder() .url(url) @@ -384,9 +363,7 @@ class MainActivity : AppCompatActivity(), MainActivityListener { try { val resp = client.newCall(request).execute() - val respString = resp.body?.string().toString() - //Log.d("debug", "timetable: $respString") - respString + resp.body?.string().toString() } catch (e: IOException) { """{"error": "network"}""" } @@ -405,15 +382,12 @@ class MainActivity : AppCompatActivity(), MainActivityListener { val week = mutableListOf() if (jsonObject.has("error")) { - //Log.d("debug error", "network") return week } - //Log.d("debug", "no network error\n\n$jsonString") // Access the "body" array from the main JSON object val bodyArray = jsonObject.getJSONArray("days") - // Convert the JSON array to a list of JSON objects val jsonObjectList = mutableListOf() for (i in 0 until bodyArray.length()) { @@ -422,6 +396,7 @@ class MainActivity : AppCompatActivity(), MainActivityListener { } for (obj in jsonObjectList) { + // for each day val dateRAW = obj.get("date").toString() var dayStr = dateRAW.split("T")[0].split("-")[2] var monthStr = dateRAW.split("T")[0].split("-")[1] @@ -455,73 +430,113 @@ class MainActivity : AppCompatActivity(), MainActivityListener { } for (subj in subjectJSONList) { + // for each subject in one particular day val subject = Subject() val kind = subj.getJSONObject("hourKind").get("id") + val type = subj.getJSONObject("hourType").get("id") + if ( - (subj.getJSONObject("hourType").get("id") == "SUPLOVANA") || - (kind != "SUPLOVANI" && kind != "") + (type == "SUPLOVANA") || + (type != "SUPLOVANI" && type != "ROZVRH") ) { - if (kind == "ZRUSENI_VYUKY_ROZVRH") { - subject.name = subj.get("title").toString() - subject.place = "" - subject.teacher = "" - subject.substitute = "2" // 2 means school event + when (kind) { + "ZRUSENI_VYUKY_ROZVRH" -> { + subject.name = subj.get("title").toString() + subject.place = "" + subject.teacher = "" + subject.substitute = "2" // 2 means school event - val hourspan = subj.getJSONArray("detailHours").length() - subject.number = "1 - $hourspan" - subject.start = subj.get("beginTime").toString() - subject.start = subject.start.slice(0..subject.start.length-4) - subject.end = subj.get("endTime").toString() - subject.end = subject.end.slice(0..subject.end.length-4) + val hourspan = subj.getJSONArray("detailHours").length() + subject.number = "1 - $hourspan" + subject.start = subj.get("beginTime").toString() + subject.start = subject.start.slice(0..subject.start.length-4) + subject.end = subj.get("endTime").toString() + 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.. = vec![]; for i in 0..v.len()-1 { if v[i] == v[i+1]-1 { diff --git a/mobile/app/src/main/res/layout/activity_main.xml b/mobile/app/src/main/res/layout/activity_main.xml index e524e81..4d07f95 100644 --- a/mobile/app/src/main/res/layout/activity_main.xml +++ b/mobile/app/src/main/res/layout/activity_main.xml @@ -20,7 +20,7 @@ android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/Theme.Solen.PopupOverlay" - app:theme="@style/Theme.Solen.AppBarOverlay"> + android:theme="@style/Theme.Solen.AppBarOverlay"> diff --git a/mobile/app/src/main/res/values/colors.xml b/mobile/app/src/main/res/values/colors.xml index 4022e64..b480877 100644 --- a/mobile/app/src/main/res/values/colors.xml +++ b/mobile/app/src/main/res/values/colors.xml @@ -8,6 +8,10 @@ #FFF6F1EE #FF000000 #FFFFFFFF - #696969 - #f1f1f1 + + #FF696969 + #FFF1F1F1 + #FFDD2200 + #FFFDAA22 + #FFAAFF66 \ No newline at end of file