added ability to refresh the mensa menus

* some minor clean ups
This commit is contained in:
2019-04-03 19:21:51 +02:00
parent 9fc897e194
commit 77326a8ed6
12 changed files with 112 additions and 65 deletions

View File

@ -192,7 +192,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// update blocking if it's sunday after 1500
// TODO and the last update was before 1500
if(currentDay == Calendar.SUNDAY && cal.get(Calendar.HOUR_OF_DAY) >= 15) {
val jobA = tcor.getMensa()
val jobA = TCoRAPIController.getMensa(this)
jobA.get()
}
@ -205,7 +205,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
tcor.getCoursesList()
if(currentTime - mensaCacheTime > 10800)
tcor.getMensa()
TCoRAPIController.getMensa(this)
if(currentTime - timetableCacheTime > 10800) {
tcor.getTimetable(cCourse.courseName, 0)

View File

@ -42,6 +42,29 @@ class CacheController(cont: Context) {
var mensaCurrentWeek = MensaWeek()
var mensaNextWeek = MensaWeek()
var timetables = ArrayList<TimetableWeek>()
/**
* read current and next weeks mensa menus from the cached file
*/
fun readMensa(context: Context) {
val file = File(context.filesDir, "mensa.json")
// make sure the file exists
if (!file.exists()) {
TCoRAPIController.getMensa(context).get()
}
val fileReader = FileReader(file)
val bufferedReader = BufferedReader(fileReader)
val mensaObject = JsonParser().parse(bufferedReader.readLine()).asJsonObject
val currentWeek = mensaObject.getAsJsonObject("currentWeek")
val nextWeek = mensaObject.getAsJsonObject("nextWeek")
mensaCurrentWeek = Gson().fromJson(currentWeek, MensaWeek().javaClass)
mensaNextWeek = Gson().fromJson(nextWeek, MensaWeek().javaClass)
}
}
/**
@ -50,7 +73,7 @@ class CacheController(cont: Context) {
*/
fun readStartCache(courseName: String) {
readCoursesList()
readMensa()
readMensa(context)
readTimetable(courseName, 0)
readTimetable(courseName, 1)
}
@ -59,7 +82,7 @@ class CacheController(cont: Context) {
* read the courses list from the cached file
* add them to the coursesList object
*/
fun readCoursesList() {
private fun readCoursesList() {
val file = File(context.filesDir, "courses.json")
// make sure the file exists
@ -73,28 +96,7 @@ class CacheController(cont: Context) {
coursesList = Gson().fromJson(coursesObject.getAsJsonArray("courses"), object : TypeToken<List<Course>>() {}.type)
}
/**
* read current and next weeks mensa menus from the cached file
*/
fun readMensa() {
val file = File(context.filesDir, "mensa.json")
// make sure the file exists
if (!file.exists()) {
TCoRAPIController(context).getMensa().get()
}
val fileReader = FileReader(file)
val bufferedReader = BufferedReader(fileReader)
val mensaObject = JsonParser().parse(bufferedReader.readLine()).asJsonObject
val currentWeek = mensaObject.getAsJsonObject("currentWeek")
val nextWeek = mensaObject.getAsJsonObject("nextWeek")
mensaCurrentWeek = Gson().fromJson(currentWeek, MensaWeek().javaClass)
mensaNextWeek = Gson().fromJson(nextWeek, MensaWeek().javaClass)
}
/**
* read the weeks timetable from the cached file

View File

@ -34,6 +34,28 @@ import java.net.URL
class TCoRAPIController(cont: Context) {
private val context = cont
companion object {
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getMensa(context: Context) = doAsync {
val url = URL("https://tcor.mosad.xyz/mensamenu")
val file = File(context.filesDir, "mensa.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
mensaCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
}
/**
* get the json object from tcor api and write it as file (cache)
*/
@ -54,26 +76,6 @@ class TCoRAPIController(cont: Context) {
PreferencesController.save(context)
}
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getMensa() = doAsync {
val url = URL("https://tcor.mosad.xyz/mensamenu")
val file = File(context.filesDir, "mensa.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
mensaCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
}
/**
* get the json object from tcor api and write it as file (cache)
*/

View File

@ -141,7 +141,7 @@ class HomeFragment : Fragment() {
} else {
MaterialDialog(context!!)
.title(R.string.error)
.message(R.string.gen_tt_error)
.message(R.string.timetable_error)
.show()
// TODO log the error and send feedback
}

View File

@ -26,15 +26,15 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_mensa.*
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.mensaCurrentWeek
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaNextWeek
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.hsoparser.MensaWeek
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
@ -46,13 +46,12 @@ import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
*/
class MensaFragment : Fragment() {
private lateinit var linLayoutMensaFragment: LinearLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_mensa, container, false)
val view: View = inflater.inflate(org.mosad.seil0.projectlaogai.R.layout.fragment_mensa, container, false)
linLayoutMensaFragment = view.findViewById(R.id.linLayout_Mensa)
// init actions
refreshAction()
// add the current week (week starts on sunday)
val dayCurrent = if(NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex()
@ -61,11 +60,15 @@ class MensaFragment : Fragment() {
// add the next week
addWeek(mensaNextWeek, 0).get()
// TODO should we show a info if there is no more food this & next week?
return view
}
/**
* add all menus from dayStart to Friday for a given week
* @param menusWeek menu of type MensaWeek you want to add
* @param dayStart the first day of the week to add
*/
private fun addWeek(menusWeek: MensaWeek, dayStart: Int) = doAsync {
@ -97,4 +100,41 @@ class MensaFragment : Fragment() {
}
/**
* initialize the refresh action
*/
private fun refreshAction() = doAsync {
uiThread {
// set the refresh listener
refreshLayout_Mensa.setOnRefreshListener {
updateMensaScreen()
refreshLayout_Mensa.isRefreshing = false
}
}
}
/**
* refresh the mensa cache and update the mensa screen
* TODO some nice animations
*/
private fun updateMensaScreen() {
// update the cache
TCoRAPIController.getMensa(context!!)
CacheController.readMensa(context!!)
// remove all menus from the layout
linLayout_Mensa.removeAllViews()
// add the refreshed menus
val dayCurrent = if (NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex()
addWeek(mensaCurrentWeek, dayCurrent).get()
// add the next week
addWeek(mensaNextWeek, 0).get()
}
}