Skip to content

Commit

Permalink
feat: add schedule fetching and display in ReglabView
Browse files Browse the repository at this point in the history
This commit includes changes in ReglabView.kt and MainViewModel.kt. It adds functionality to fetch schedule data and display it in the ReglabView. It also handles exceptions when fetching the schedule data fails.
  • Loading branch information
IRedDragonICY committed Mar 15, 2024
1 parent 02d56b9 commit a51ecf1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
22 changes: 22 additions & 0 deletions app/src/main/java/com/uad/portal/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
Expand Down Expand Up @@ -99,6 +102,7 @@ data class Hari(
val updated_at: String
)


class MainViewModel : ViewModel() {
private lateinit var sessionManager: SessionManager
private val auth = Auth()
Expand Down Expand Up @@ -174,6 +178,24 @@ class MainViewModel : ViewModel() {
return response.statusCode() == 200
}

suspend fun fetchScheduleData(): Response = withContext(Dispatchers.IO) {
val session = sessionManager.loadReglabSession()
val username = session?.credentials?.username

if (session != null && username != null) {
val url = "https://reglab.tif.uad.ac.id/ajax/pemilihan-jadwal-praktikum/$username"
val response = Jsoup.connect(url)
.cookie("remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d", session.remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d)
.ignoreContentType(true)
.execute()
val json = response.body()
val gson = Gson()
return@withContext gson.fromJson(json, Response::class.java)
}
throw Exception("Failed to load session")
}


private fun connectJsoup(
url: String,
cookie: String,
Expand Down
33 changes: 30 additions & 3 deletions app/src/main/java/com/uad/portal/ReglabView.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
package com.uad.portal

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun ReglabView(mainViewModel: MainViewModel) {
val scheduleData = remember { mutableStateOf<Response?>(null) }

LaunchedEffect(Unit) {
try {
scheduleData.value = mainViewModel.fetchScheduleData()
println(scheduleData.value)
} catch (e: Exception) {
println("Failed to fetch schedule data: ${e.message}")
}
}

Column(modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp)) {


scheduleData.value?.data?.forEach { dataItem ->
Text(text = "Mata Kuliah: ${dataItem.jadwal_praktikum.praktikum_aktif.matakuliah.nama}")
Text(text = "Dosen: ${dataItem.jadwal_praktikum.dosen.nama}")
Text(text = "Hari: ${dataItem.jadwal_praktikum.hari.nama}")
Text(text = "Jam Mulai: ${dataItem.jadwal_praktikum.jam_mulai}")
Text(text = "Jam Selesai: ${dataItem.jadwal_praktikum.jam_selesai}")
Spacer(modifier = Modifier.height(16.dp))
}
}

Column(modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp)) {
Button(onClick = { mainViewModel.navigate(Screen.Home) }) {
Text("Back")
}
}
}


0 comments on commit a51ecf1

Please sign in to comment.