removed all redundant bits (variables and stuff) from Global.kt and converted them into the global struct

This commit is contained in:
Odweta
2024-12-22 12:21:38 +01:00
parent 8b7301a30d
commit e5d807b6d8
7 changed files with 20 additions and 112 deletions
@@ -19,44 +19,12 @@ import okhttp3.OkHttpClient
import org.json.JSONObject
import java.io.File
// Global values (dimens, modifiers, ...)
val roundedCornerDimen = 8.dp
val borderWidthDimen = 5.dp
fun makeStatusBarBlack(window: Window) {
window.statusBarColor = Color.Black.toArgb()
}
const val accountDataFilePath: String = "account_data.txt"
const val timeTableFilePath: String = "timetable.json"
const val userDataFilePath: String = "userdata.json"
const val marksFilePath: String = "marks.json"
const val settingsDataFilePath: String = "settings.txt"
const val lectureNotesFilePath: String = "lecture_notes.csv"
var width: Int = 0
var height: Int = 0
var loaded: Boolean = false
enum class IsShowing {
Timetable,
Marks,
Menu,
About,
Settings
}
enum class Screen {
Main,
Splash,
SignIn
}
var screenToShow: MutableState<Screen> = mutableStateOf(Screen.Main)
var isShowing by mutableStateOf(IsShowing.Timetable)
// default settings (will get overwritten by the saved settings)
class Setting(auxId: SettingsId, auxName: String, auxChecked: Boolean) {
var id: SettingsId = auxId
@@ -100,14 +68,6 @@ class SettingsData {
override fun toString() = "$useSvobodaMode\n$useAmoledMode\n$showRefreshButton\n$tagNewMarks"
}
var settings : SettingsData = SettingsData()
var internetAwaitListenerRunning: Boolean = false
var days = listOf<Day>()
val client = OkHttpClient()
enum class LectureNoteType {
Normal, // obycejna poznamka
Writing, // pisemny test
@@ -125,13 +85,7 @@ fun String.toLectureNoteType(): LectureNoteType {
}
}
class LectureNote(
id: Int,
note: MutableState<String>,
type: MutableState<LectureNoteType>,
number: Int,
lectureName: String
) {
class LectureNote(id: Int, note: MutableState<String>, type: MutableState<LectureNoteType>, number: Int, lectureName: String) {
var id: Int = 0
var note: MutableState<String> =
mutableStateOf("")
@@ -248,11 +202,13 @@ class Global {
var timetable: List<Day> = listOf()
var lectureNotes: HashMap<Int, LectureNote> = hashMapOf()
var subjectNameMap: HashMap<String, String> = hashMapOf()
var client: OkHttpClient = OkHttpClient()
var username: String = ""
var password: String = ""
var noSignInBackHandle: Boolean = false
var internetAwaitListenerRunning: Boolean = false
var selectedLecture: Int = 0
var currentLectureNumber: Int = 0
@@ -310,3 +266,5 @@ class Global {
}
val global = Global()
var settings = SettingsData()
@@ -20,7 +20,7 @@ fun launchStart(ctx: Context) {
}
} else {
global.noSignInBackHandle = true
screenToShow.value = Screen.SignIn
global.currentScreen = Screen.SignIn
}
}
@@ -55,7 +55,7 @@ fun localLoad(ctx: Context) {
global.studentClass = Network.getStudentClass()
global.apiEndPoints.marks = "${global.apiEndPoints.apiBase}/v1/students/${global.studentId}/marks/list"
val localTimetable = JSONObject(Util.fileToString(global.filePaths.timetable))
val localMarks = JSONObject(Util.fileToString(marksFilePath))
val localMarks = JSONObject(Util.fileToString(global.filePaths.marks))
launchContinue(
ctx,
@@ -80,11 +80,10 @@ fun launchContinue(
global.settings = settingsData
// load the lecture notes
val lectureNotes = Util.fileToString(global.filePaths.lectureNotes)
Parsing.parseLectureNotes(lectureNotes)
Util.csvToLectureNotes()
// listen for internet availability in case of disconnection
if (!internetAwaitListenerRunning)
if (!global.internetAwaitListenerRunning)
setOnInternetAvailListener(ctx)
if (!global.launched) {
@@ -108,7 +107,7 @@ fun launchContinue(
}
private fun setOnInternetAvailListener(ctx: Context) {
internetAwaitListenerRunning = true
global.internetAwaitListenerRunning = true
var internetAvail = Util.internetAvail(ctx)
CoroutineScope(Dispatchers.IO).launch {
while (true) {
@@ -14,6 +14,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowCompat
import com.odweta.solon.ui.theme.SolonTheme
@@ -23,7 +24,7 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
makeStatusBarBlack(window)
window.statusBarColor = Color.Black.toArgb()
global.fileDir = applicationContext.filesDir
launchStart(applicationContext)
@@ -133,7 +133,7 @@ fun MarksAll() {
.fillMaxWidth()
.height(70.dp)
.border(
width = borderWidthDimen,
width = global.dimens.borderWidth,
color = getAvgColor(Util.avg(markList), true), // decide on the color (transparent)
shape = RoundedCornerShape(global.dimens.roundedCorner)
)
@@ -24,7 +24,7 @@ class Network {
.build()
return try {
val resp = client.newCall(request).execute()
val resp = global.client.newCall(request).execute()
val respString = resp.body?.string().toString()
if (JSONObject(respString).has("access_token")) {
JSONObject(respString).getString("access_token")
@@ -94,7 +94,7 @@ class Network {
.build()
return try {
val resp = client.newCall(request).execute()
val resp = global.client.newCall(request).execute()
val respString = resp.body?.string().toString()
JSONObject(respString)
} catch (e: JSONException) {
@@ -117,7 +117,7 @@ class Network {
.build()
return try {
val resp = client.newCall(request).execute()
val resp = global.client.newCall(request).execute()
JSONObject(resp.body?.string().toString())
} catch (e: JSONException) {
JSONObject("""{"error": "json"}""")
@@ -135,7 +135,7 @@ class Network {
.build()
return try {
val resp = client.newCall(request).execute()
val resp = global.client.newCall(request).execute()
JSONObject(resp.body?.string().toString())
} catch (e: JSONException) {
JSONObject("""{"error": "json"}""")
@@ -385,38 +385,5 @@ class Parsing {
return marks
}
fun parseLectureNotes(lectureNotes: String) {
for (day in global.timetable) {
for ((subjectIndex, subject) in day.subjects.withIndex()) {
val id = getLectureId(day.date, subjectIndex)
global.lectureNotes[id] = LectureNote(
id = id,
type = mutableStateOf(LectureNoteType.Normal),
note = mutableStateOf(""),
number = subject.number.toInt(),
lectureName = subject.name
)
}
}
val lines = lectureNotes.lines()
for (line in lines) {
if (line.isBlank()) continue
val parts = line.split(",")
if (parts.size >= 3) {
val id = parts[0].trim().toInt()
val type = parts[1].trim().toLectureNoteType()
var note = parts[2].trim()
for (part in 3.until(parts.size)) {
note += ",${parts[part]}"
}
global.lectureNotes[id]?.note = mutableStateOf(note)
global.lectureNotes[id]?.type = mutableStateOf(type)
}
}
}
}
}
@@ -17,23 +17,6 @@ import kotlin.math.abs
class Util {
companion object {
private fun getDisplayMetrics(context: Context): DisplayMetrics {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics
}
fun getDisplayHeight(context: Context): Int {
return getDisplayMetrics(context).heightPixels
}
fun getDisplayWidth(context: Context): Int {
return getDisplayMetrics(context).widthPixels
}
fun saveStringToFile(filePath: String, s: String): Boolean {
// get a file object with the specified path
val f = File(global.fileDir, filePath)
@@ -266,7 +249,7 @@ class Util {
}
fun csvToLectureNotes() {
val lines = fileToString(lectureNotesFilePath).lines()
val lines = fileToString(global.filePaths.lectureNotes).lines()
for (line in lines) {
if (line.isBlank()) continue
@@ -298,7 +281,7 @@ class Util {
}
fun loadLectureNotes() {
for (day in days) {
for (day in global.timetable) {
for ((subjectIndex, subject) in day.subjects.withIndex()) {
val id = getLectureId(day.date, subjectIndex)
global.lectureNotes[id] = LectureNote(