added ability to refresh the mensa menus

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

View File

@ -13,7 +13,7 @@ android {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 28 targetSdkVersion 28
versionCode 12 versionCode 12
versionName "0.4.0" versionName "0.4.90"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resValue "string", "build_time", buildTime() resValue "string", "build_time", buildTime()
setProperty("archivesBaseName", "projectlaogai-$versionName") setProperty("archivesBaseName", "projectlaogai-$versionName")
@ -38,6 +38,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.afollestad:aesthetic:1.0.0-beta05' implementation 'com.afollestad:aesthetic:1.0.0-beta05'

View File

@ -5,6 +5,7 @@
<application <application
android:allowBackup="true" android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_laogai_icon" android:icon="@mipmap/ic_laogai_icon"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_laogai_icon" android:roundIcon="@mipmap/ic_laogai_icon"

View File

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

View File

@ -42,6 +42,29 @@ class CacheController(cont: Context) {
var mensaCurrentWeek = MensaWeek() var mensaCurrentWeek = MensaWeek()
var mensaNextWeek = MensaWeek() var mensaNextWeek = MensaWeek()
var timetables = ArrayList<TimetableWeek>() 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) { fun readStartCache(courseName: String) {
readCoursesList() readCoursesList()
readMensa() readMensa(context)
readTimetable(courseName, 0) readTimetable(courseName, 0)
readTimetable(courseName, 1) readTimetable(courseName, 1)
} }
@ -59,7 +82,7 @@ class CacheController(cont: Context) {
* read the courses list from the cached file * read the courses list from the cached file
* add them to the coursesList object * add them to the coursesList object
*/ */
fun readCoursesList() { private fun readCoursesList() {
val file = File(context.filesDir, "courses.json") val file = File(context.filesDir, "courses.json")
// make sure the file exists // make sure the file exists
@ -73,28 +96,7 @@ class CacheController(cont: Context) {
coursesList = Gson().fromJson(coursesObject.getAsJsonArray("courses"), object : TypeToken<List<Course>>() {}.type) 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 * read the weeks timetable from the cached file

View File

@ -34,6 +34,28 @@ import java.net.URL
class TCoRAPIController(cont: Context) { class TCoRAPIController(cont: Context) {
private val context = cont 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) * get the json object from tcor api and write it as file (cache)
*/ */
@ -54,26 +76,6 @@ class TCoRAPIController(cont: Context) {
PreferencesController.save(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) * get the json object from tcor api and write it as file (cache)
*/ */

View File

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

View File

@ -26,15 +26,15 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_mensa.* import kotlinx.android.synthetic.main.fragment_mensa.*
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread 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.mensaCurrentWeek
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaNextWeek import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaNextWeek
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet 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.MensaWeek
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
@ -46,13 +46,12 @@ import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
*/ */
class MensaFragment : Fragment() { class MensaFragment : Fragment() {
private lateinit var linLayoutMensaFragment: LinearLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 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) // add the current week (week starts on sunday)
val dayCurrent = if(NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex() val dayCurrent = if(NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex()
@ -61,11 +60,15 @@ class MensaFragment : Fragment() {
// add the next week // add the next week
addWeek(mensaNextWeek, 0).get() addWeek(mensaNextWeek, 0).get()
// TODO should we show a info if there is no more food this & next week?
return view return view
} }
/** /**
* add all menus from dayStart to Friday for a given week * 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 { 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()
}
} }

View File

@ -5,9 +5,10 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".fragments.MensaFragment"> tools:context=".fragments.MensaFragment">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:id="@+id/refreshLayout_Mensa">
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
@ -16,9 +17,9 @@
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/linLayout_Mensa" android:layout_height="wrap_content" android:id="@+id/linLayout_Mensa"
android:background="@color/themePrimary"> android:background="@color/themePrimary"/>
</LinearLayout>
</ScrollView> </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout> </FrameLayout>

View File

@ -12,8 +12,7 @@
<string name="no_more_food">Diese Woche keine weitere Essensausgabe</string> <string name="no_more_food">Diese Woche keine weitere Essensausgabe</string>
<string name="no_lesson_today">heute keine Vorlesung!</string> <string name="no_lesson_today">heute keine Vorlesung!</string>
<string name="error">Fehler</string> <string name="error">Fehler</string>
<string name="no_tt_error">Stundenplan konnte nicht geladen werden!</string> <string name="timetable_error">Stundenplan konnte nicht geladen werden!</string>
<string name="gen_tt_error">Allgemeiner Stundenplan Fehler!"</string>
<string name="info">Info</string> <string name="info">Info</string>
<string name="user">Benutzer</string> <string name="user">Benutzer</string>
<string name="course_desc">Tippen, um den Kurs zu ändern</string> <string name="course_desc">Tippen, um den Kurs zu ändern</string>

View File

@ -3,7 +3,6 @@
<color name="colorPrimary">#000000</color> <color name="colorPrimary">#000000</color>
<color name="colorPrimaryDark">#000000</color> <color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#3F51B5</color> <color name="colorAccent">#3F51B5</color>
<color name="colorMensaDay">#EBE8E9</color>
<color name="ic_launcher_background">#FFFFFF</color> <color name="ic_launcher_background">#FFFFFF</color>
<color name="themePrimary">#FFFFFF</color> <color name="themePrimary">#FFFFFF</color>
@ -27,6 +26,4 @@
<color name="themeSecondaryLight">#FFFFFF</color> <color name="themeSecondaryLight">#FFFFFF</color>
<color name="themeTertiaryLight">#424242</color> <!-- TODO find a better color --> <color name="themeTertiaryLight">#424242</color> <!-- TODO find a better color -->
<color name="test">#424242</color>
</resources> </resources>

View File

@ -20,8 +20,7 @@
<string name="no_lesson_today">"No lecture today!"</string> <string name="no_lesson_today">"No lecture today!"</string>
<string name="error">Error</string> <string name="error">Error</string>
<string name="no_tt_error">Could not load timetable!"</string> <string name="timetable_error">Could not load timetable!"</string>
<string name="gen_tt_error">There was an error with the timetable!"</string>
<string name="sample_user" translatable="false">SampleUser@stud.hs-offenburg.de</string> <string name="sample_user" translatable="false">SampleUser@stud.hs-offenburg.de</string>
<string name="sample_course" translatable="false">SampleCourse 3</string> <string name="sample_course" translatable="false">SampleCourse 3</string>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="file" path="mensa.json"/>
<exclude domain="file" path="courses.json"/>
</full-backup-content>