fixed issue #2, #3 and #4

This commit is contained in:
Odweta
2024-01-28 14:41:43 +01:00
parent 3c65acacb8
commit b089a04c4b
399 changed files with 174 additions and 70487 deletions
@@ -22,14 +22,14 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import java.util.Calendar
interface MainActivityListener {
fun onLaunch(view: View)
}
@Suppress("INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION_WARNING")
class CalWeekFragment : Fragment() {
private var mainActivityListener: MainActivityListener? = null
//private var mainActivityListener: MainActivityListener? = null
override fun onCreateView(
inflater: LayoutInflater,
@@ -46,18 +46,11 @@ class CalWeekFragment : Fragment() {
.toInt())).commit()
}
for (i in 0 until 5) {
for (i in 0 until MainActivity.days.size) {
adapter.addFragment(DayFragment(i))
}
viewPager.adapter = adapter
// set the correct day to display as default
val day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
if (day in Calendar.MONDAY..Calendar.FRIDAY) {
viewPager.currentItem = day - 2
} else {
viewPager.currentItem = 4
}
return view
}
@@ -165,7 +158,9 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
frameLayout.addView(subjectNumberTextView)
subjectLayout.addView(frameLayout)
if (day.subjects[0].substitute != "2") {
subjectLayout.addView(frameLayout)
}
val subjectNameTextView = TextView(requireContext())
if (subj.name != "Volno") {
@@ -206,7 +201,9 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
)
)
subjectLayout.addView(subjectPlaceTextView)
if (day.subjects[0].substitute != "2") {
subjectLayout.addView(subjectPlaceTextView)
}
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
@@ -249,7 +246,9 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
timeTeacherLayout.addView(subjectTeacherTextView)
}
subjectLayout.addView(timeTeacherLayout)
if (day.subjects[0].substitute != "2") {
subjectLayout.addView(timeTeacherLayout)
}
val subjectSeparator = Space(requireContext())
subjectSeparator.layoutParams = LinearLayout.LayoutParams(
@@ -258,13 +257,26 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
)
// Attach an OnPreDrawListener to the rootLayout's ViewTreeObserver
rootLayout.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
rootLayout.viewTreeObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
// Remove the listener to avoid multiple callbacks
rootLayout.viewTreeObserver.removeOnPreDrawListener(this)
// Call your function or perform an action here
subjectPlaceTextView.setPadding(400-frameLayout.width-subjectNameTextView.width, 20, 20, 20)
if (day.subjects[0].substitute != "2") {
subjectPlaceTextView.setPadding(
400 - frameLayout.width - subjectNameTextView.width,
20,
20,
20
)
} else {
subjectNameTextView.width = subjectLayout.width
subjectNameTextView.setPadding(30, 35, 30, 35)
subjectNameTextView.textAlignment = View.TEXT_ALIGNMENT_CENTER
subjectNameTextView.gravity = Gravity.CENTER_VERTICAL
}
// Return true to continue with the drawing, or false to cancel
return true
@@ -315,8 +327,10 @@ class DayFragment(private val position: Int) : Fragment(R.layout.fragment_day) {
endParams.setMargins(20, 0, 20, 0)
dayEndTextView.layoutParams = endParams
dayLayout.addView(dayEndSeparator)
dayLayout.addView(dayEndTextView)
if (day.subjects[0].substitute != "2") {
dayLayout.addView(dayEndSeparator)
dayLayout.addView(dayEndTextView)
}
scrollView.addView(dayLayout)
@@ -332,6 +346,22 @@ class Subject {
var number: String = ""
var start: String = ""
var end: String = ""
companion object {
fun freeSubject(number: String, start: String, end: String): Subject {
val s = Subject()
s.name = "Volno"
s.place = "Volno"
s.teacher = "Volno"
s.substitute = "0"
s.number = number
s.start = start
s.end = end
return s
}
}
}
class Day {
@@ -1,7 +1,5 @@
package com.odweta.solen
import android.R.id.input
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.util.Log
@@ -15,10 +13,8 @@ import kotlinx.coroutines.withContext
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
@@ -26,7 +22,7 @@ import java.time.format.DateTimeFormatter
class MainActivity : AppCompatActivity(), MainActivityListener {
companion object {
val dayMap = mutableMapOf<Int, Day>()
val dateMap = mutableMapOf<String, Int>()
var days = listOf<Day>()
}
private val client = OkHttpClient()
@@ -37,19 +33,24 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
private val tokenUrl = "$apiBaseUrl/connect/token"
private var token = ""
private val userDataUrl = "$apiBaseUrl/v1/user"
private var userData = JSONObject()
private var userData = ""
private var studentId = ""
private val timeTableUrl = "$apiBaseUrl/v1/timeTable"
private val marksUrl = "$apiBaseUrl/v1/students/$studentId/marks/list"
//private val marksUrl = "$apiBaseUrl/v1/students/$studentId/marks/list"
private suspend fun setupAPI() {
return withContext(Dispatchers.IO) {
//Log.d("debug", "setting up API...")
token = getToken()
if (token == "") {
return@withContext
}
userData = getUserDataJSON()
studentId = getStudentId()
}
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cal_week)
@@ -60,21 +61,21 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
}
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onLaunch(view: View) {
launch()
}
@RequiresApi(Build.VERSION_CODES.O)
private fun launch() {
lifecycleScope.launch {
val week = parseJSON(getTimeTableJSON())
days = week
for ((i, day) in week.withIndex()) {
//Log.d("debug", "$i, ${day.name}\n${day.date}\n${day.subjects}")
dayMap[i] = day
}
//val currentDate = SimpleDateFormat("", Locale.getDefault()).format(Date())
//val dayOfWeek = Calendar.DAY_OF_WEEK
//dateMap[currentDate] = dayOfWeek - 2
val mainFragment = CalWeekFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, mainFragment)
@@ -105,16 +106,17 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
val token = respJson?.get("access_token").toString()
token
} catch (e: Exception) {
"0"
"""{"error": "network"}"""
}
}
}
private fun getStudentId(): String {
return userData.get("personID").toString()
//Log.d("debug", userData)
return JSONObject(userData).get("personID").toString()
}
private suspend fun getUserDataJSON(): JSONObject {
private suspend fun getUserDataJSON(): String {
return withContext(Dispatchers.IO) {
val request = Request.Builder()
.url(userDataUrl)
@@ -125,10 +127,9 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
try {
val resp = client.newCall(request).execute()
val respJSON = JSONObject(resp.body?.string().toString())
respJSON
resp.body?.string().toString()
} catch (e: IOException) {
JSONObject("""0""")
"""{"error": "network"}"""
}
}
}
@@ -136,6 +137,8 @@ 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 request = Request.Builder()
.url(timeTableUrl)
@@ -150,7 +153,7 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
//Log.d("d", JSONObject(resp.body?.string().toString()).get("subject").toString())
resp.body?.string().toString()
} catch (e: IOException) {
"0"
"""{"error": "network"}"""
}
}
}
@@ -165,10 +168,17 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
* ["Po", "8.1.", ["D", "207", "Strakova S.", 0, 1, "08:00", "08:45" ]]
*/
val week = mutableListOf<Day>()
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")
val week = mutableListOf<Day>()
// Convert the JSON array to a list of JSON objects
val jsonObjectList = mutableListOf<JSONObject>()
@@ -182,8 +192,8 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
var dayStr = dateRAW.split("T")[0].split("-")[2]
var monthStr = dateRAW.split("T")[0].split("-")[1]
if (dayStr[0] == '0') { dayStr = dayStr.slice(1..dayStr.length-1) }
if (monthStr[0] == '0') { monthStr = monthStr.slice(1..monthStr.length-1) }
if (dayStr[0] == '0') { dayStr = dayStr.slice(1..<dayStr.length) }
if (monthStr[0] == '0') { monthStr = monthStr.slice(1..<monthStr.length) }
val date = "$dayStr. $monthStr."
@@ -191,17 +201,18 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
val localDateTime = LocalDateTime.parse(dateRAW, formatter)
val name = when (localDateTime.dayOfWeek.toString()) {
"Monday" -> "Pondělí"
"Tuesday" -> "Úterý"
"Wednesday" -> "Středa"
"Thursday" -> "Čtvrtek"
"Friday" -> "Pátek"
"Saturday" -> "Sobota"
"Sunday" -> "Neděle"
else -> "Invalid day"
"MONDAY" -> "Pondělí"
"TUESDAY" -> "Úterý"
"WEDNESDAY" -> "Středa"
"THURSDAY" -> "Čtvrtek"
"FRIDAY" -> "Pátek"
"SATURDAY" -> "Sobota"
"SUNDAY" -> "Neděle"
else -> localDateTime.dayOfWeek.toString()
}
val subjects = mutableListOf<Subject>()
val auxSubjects = mutableListOf<Subject>()
val subjectJSONList = mutableListOf<JSONObject>()
val subjectArray = obj.getJSONArray("schedules")
@@ -209,24 +220,37 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
subjectJSONList.add(subjectArray.getJSONObject(i))
}
var i = 0
for (subj in subjectJSONList) {
val subject = Subject()
val kind = subj.getJSONObject("hourKind").get("id")
if (
(subj.getJSONObject("hourType").get("id") == "SUPLOVANA") ||
(subj.getJSONObject("hourKind").get("id") != "SUPLOVANI" && subj.getJSONObject("hourKind").get("id") != "")
(kind != "SUPLOVANI" && kind != "")
) {
Log.d("skip", "skipping $i times | kind: $kind")
i += 1
if (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)
subjects.add(subject)
}
continue
}
Log.d("skip", "did not skip!")
//Log.d("debug", subj.toString())
val subject = Subject()
subject.name = subj.getJSONObject("subject").get("name").toString()
Log.d("debug", subject.name)
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("displayName").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") {
@@ -240,26 +264,81 @@ class MainActivity : AppCompatActivity(), MainActivityListener {
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...")
//Log.d("push", "pushing day...")
subjects.add(subject)
} else {
for (i in 0..<hourspan) {
val detailHours = subj.get("detailHours") as JSONArray
subject.number = (detailHours.get(i) as JSONObject).get("id").toString()
subject.start = (detailHours.get(i) as JSONObject).get("timeFrom").toString()
subject.start = subject.start.slice(0..subject.start.length-4)
subject.end = (detailHours.get(i) as JSONObject).get("timeto").toString()
subject.end = subject.end.slice(0..subject.end.length-4)
Log.d("push", "pushing day...")
subjects.add(subject)
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()
//Log.d("debug", "${date}\n\n$detailHours\n\n${subject.number}")
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)
//Log.d("push", "pushing day...")
subjects.add(newSubject)
}
}
}
/*
let mut res: Vec<i32> = vec![];
for i in 0..v.len()-1 {
if v[i] == v[i+1]-1 {
res.push(v[i]);
} else {
res.push(v[i]);
while res[res.len()-1]+1 != v[i+1] {
res.push(res[res.len()-1]+1);
}
}
}
res.push(v[v.len()-1]);
res
*/
if (subjects.size > 1) {
for (i in 0..<subjects.size-1) {
if (subjects[i].number == ((subjects[i + 1].number.toInt() - 1).toString())) {
auxSubjects.add(subjects[i])
} else {
auxSubjects.add(subjects[i])
while ((auxSubjects[auxSubjects.size - 1].number.toInt() + 1).toString() != subjects[i + 1].number) {
auxSubjects.add(
Subject.freeSubject(
(auxSubjects[auxSubjects.size - 1].number.toInt() + 1).toString(),
"",
""
)
)
}
}
}
auxSubjects.add(subjects[subjects.size - 1])
} else {
auxSubjects.add(subjects[0])
}
for ((i, s) in auxSubjects.withIndex()) {
if (s.place == "Volno") {
s.start = auxSubjects[i-1].end
s.end = auxSubjects[i+1].start
}
}
val day = Day()
day.name = name
day.date = date
day.subjects = subjects
day.subjects = auxSubjects
week.add(day)
}