added refresh to timetable screen

This commit is contained in:
Jannik 2019-04-06 14:13:01 +02:00
parent 2bd86ff6bb
commit fe111ac56b
7 changed files with 108 additions and 69 deletions

View File

@ -13,7 +13,7 @@ android {
minSdkVersion 21
targetSdkVersion 28
versionCode 12
versionName "0.4.90"
versionName "0.4.91"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resValue "string", "build_time", buildTime()
setProperty("archivesBaseName", "projectlaogai-$versionName")

View File

@ -162,10 +162,10 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
* load the mensa menus of the current week
*/
private fun load() {
val startupTime = measureTimeMillis {
// load the settings
PreferencesController.load(this) // this must be finished before doing anything else
val startupTime = measureTimeMillis {
val tcor = TCoRAPIController(this)
val currentTime = System.currentTimeMillis() / 1000
val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
@ -179,8 +179,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// update blocking if a) it`s monday and the last cache was not on a monday or b) the cache is older than 6 days
if((currentDay == Calendar.MONDAY && timetableCacheDay != Calendar.MONDAY) || (System.currentTimeMillis() / 1000) - timetableCacheTime > 518400) {
println("updating timetable after sunday!")
val jobA = tcor.getTimetable(cCourse.courseName, 0)
val jobB = tcor.getTimetable(cCourse.courseName, 1)
val jobA = TCoRAPIController.getTimetable(cCourse.courseName, 0, this)
val jobB = TCoRAPIController.getTimetable(cCourse.courseName, 1, this)
jobA.get()
jobB.get()
@ -208,11 +208,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
TCoRAPIController.getMensa(this)
if(currentTime - timetableCacheTime > 10800) {
tcor.getTimetable(cCourse.courseName, 0)
tcor.getTimetable(cCourse.courseName, 1)
TCoRAPIController.getTimetable(cCourse.courseName, 0, this)
TCoRAPIController.getTimetable(cCourse.courseName, 1, this)
}
}
println("Completed in $startupTime ms")
println("startup completed in $startupTime ms")
}
}

View File

@ -65,6 +65,30 @@ class CacheController(cont: Context) {
mensaCurrentWeek = Gson().fromJson(currentWeek, MensaWeek().javaClass)
mensaNextWeek = Gson().fromJson(nextWeek, MensaWeek().javaClass)
}
/**
* read the weeks timetable from the cached file
* @param courseName the course name (e.g AI1)
* @param week the week to read (0 for the current and so on)
*/
fun readTimetable(courseName: String, week: Int, context: Context) {
val file = File(context.filesDir, "timetable-$courseName-$week.json")
// make sure the file exists
if (!file.exists())
TCoRAPIController.getTimetable(courseName, week, context).get()
val fileReader = FileReader(file)
val bufferedReader = BufferedReader(fileReader)
val timetableObject = JsonParser().parse(bufferedReader.readLine()).asJsonObject
// make sure you add the single weeks in the exact order!
if (timetables.size == week) {
timetables.add(Gson().fromJson(timetableObject.getAsJsonObject("timetable"), TimetableWeek().javaClass))
} else if (timetables.size >= week) {
timetables[week] = Gson().fromJson(timetableObject.getAsJsonObject("timetable"), TimetableWeek().javaClass)
}
}
}
/**
@ -74,8 +98,8 @@ class CacheController(cont: Context) {
fun readStartCache(courseName: String) {
readCoursesList()
readMensa(context)
readTimetable(courseName, 0)
readTimetable(courseName, 1)
readTimetable(courseName, 0, context)
readTimetable(courseName, 1, context)
}
/**
@ -96,29 +120,4 @@ class CacheController(cont: Context) {
coursesList = Gson().fromJson(coursesObject.getAsJsonArray("courses"), object : TypeToken<List<Course>>() {}.type)
}
/**
* read the weeks timetable from the cached file
* @param courseName the course name (e.g AI1)
* @param week the week to read (0 for the current and so on)
*/
fun readTimetable(courseName: String, week: Int) {
val file = File(context.filesDir, "timetable-$courseName-$week.json")
// make sure the file exists
if (!file.exists())
TCoRAPIController(context).getTimetable(courseName, week).get()
val fileReader = FileReader(file)
val bufferedReader = BufferedReader(fileReader)
val timetableObject = JsonParser().parse(bufferedReader.readLine()).asJsonObject
// make sure you add the single weeks in the exact order!
if (timetables.size == week) {
timetables.add(Gson().fromJson(timetableObject.getAsJsonObject("timetable"), TimetableWeek().javaClass))
} else if (timetables.size >= week) {
timetables[week] = Gson().fromJson(timetableObject.getAsJsonObject("timetable"), TimetableWeek().javaClass)
}
}
}

View File

@ -54,6 +54,26 @@ class TCoRAPIController(cont: Context) {
mensaCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getTimetable(courseName: String, week: Int, context: Context) = doAsync {
val url = URL("https://tcor.mosad.xyz/timetable?courseName=$courseName&week=$week")
val file = File(context.filesDir, "timetable-$courseName-$week.json")
// read data from the API
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
// write the json object to a file
val writer = BufferedWriter(FileWriter(file))
writer.write(mensaObject.toString())
writer.close()
// update cache time
timetableCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
}
/**
@ -75,24 +95,4 @@ class TCoRAPIController(cont: Context) {
coursesCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getTimetable(courseName: String, week: Int) = doAsync {
val url = URL("https://tcor.mosad.xyz/timetable?courseName=$courseName&week=$week")
val file = File(context.filesDir, "timetable-$courseName-$week.json")
// read data from the API
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
// write the json object to a file
val writer = BufferedWriter(FileWriter(file))
writer.write(mensaObject.toString())
writer.close()
// update cache time
timetableCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
}

View File

@ -176,11 +176,11 @@ class SettingsFragment : Fragment() {
PreferencesController.save(context)
// update current & next weeks timetable
TCoRAPIController(context).getTimetable(cCourse.courseName, 0)
TCoRAPIController(context).getTimetable(cCourse.courseName, 1)
TCoRAPIController.getTimetable(cCourse.courseName, 0, context).get() // blocking since we want the new data
TCoRAPIController.getTimetable(cCourse.courseName, 1, context).get() // blocking since we want the new data
CacheController(context).readTimetable(cCourse.courseName, 0)
CacheController(context).readTimetable(cCourse.courseName, 1)
CacheController.readTimetable(cCourse.courseName, 0, context)
CacheController.readTimetable(cCourse.courseName, 1, context)
uiThread {
dialog.dismiss()

View File

@ -32,7 +32,10 @@ import kotlinx.android.synthetic.main.fragment_timetable.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.hsoparser.TimetableWeek
@ -52,6 +55,9 @@ class TimeTableFragment : Fragment() {
val view: View = inflater.inflate(R.layout.fragment_timetable, container, false)
// init actions
refreshAction()
if (timetables[0].days.isNotEmpty() && timetables[1].days.isNotEmpty()) {
addInitWeeks()
} else {
@ -71,8 +77,6 @@ class TimeTableFragment : Fragment() {
val dayIndex = NotRetardedCalendar().getDayOfWeekIndex()
val calendar = Calendar.getInstance()
println(formatter.format(calendar.time))
// add current week
addWeek(dayIndex, 5, timetables[0], calendar).get()
@ -87,7 +91,6 @@ class TimeTableFragment : Fragment() {
var helpLesson = LessonLinearLayout(context)
val dayCardView = DayCardView(context!!)
dayCardView.setDayHeading(formatter.format(calendar.time))
println("1: $day , ${formatter.format(calendar.time)}")
// for each timeslot of the day
for ((tsIndex, timeslot) in timetable.days[day].timeslots.withIndex()) {
@ -108,8 +111,6 @@ class TimeTableFragment : Fragment() {
}
}
println("2: $day , ${formatter.format(calendar.time)}")
helpLesson.disableDivider()
calendar.add(Calendar.DATE, 1)
@ -123,4 +124,43 @@ class TimeTableFragment : Fragment() {
}
}
/**
* initialize the refresh action
*/
private fun refreshAction() = doAsync {
uiThread {
// set the refresh listener
refreshLayout_Timetable.setOnRefreshListener {
updateTimetableScreen()
}
}
}
private fun updateTimetableScreen() = doAsync {
// update the cache
TCoRAPIController.getTimetable(cCourse.courseName, 0, context!!).get() // blocking since we want the new data
TCoRAPIController.getTimetable(cCourse.courseName, 1, context!!).get() // blocking since we want the new data
CacheController.readTimetable(cCourse.courseName, 0, context!!)
CacheController.readTimetable(cCourse.courseName, 1, context!!)
uiThread {
// remove all menus from the layout
linLayout_Timetable.removeAllViews()
// add the refreshed timetables
val dayIndex = NotRetardedCalendar().getDayOfWeekIndex()
val calendar = Calendar.getInstance()
// add current week
addWeek(dayIndex, 5, timetables[0], calendar).get()
// add next week
addWeek(0, dayIndex - 1, timetables[1], calendar)
refreshLayout_Timetable.isRefreshing = false
}
}
}

View File

@ -5,9 +5,10 @@
android:layout_height="match_parent"
tools:context=".fragments.TimeTableFragment">
<androidx.constraintlayout.widget.ConstraintLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:id="@+id/refreshLayout_Timetable">
<ScrollView
android:layout_width="match_parent"
@ -20,6 +21,5 @@
android:background="@color/themePrimary"
android:id="@+id/linLayout_Timetable"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>