Browse Source

added ability to refresh the mensa menus

* some minor clean ups
pull/22/head
Jannik 2 years ago
parent
commit
77326a8ed6
  1. 3
      app/build.gradle
  2. 1
      app/src/main/AndroidManifest.xml
  3. 4
      app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt
  4. 48
      app/src/main/java/org/mosad/seil0/projectlaogai/controller/CacheController.kt
  5. 42
      app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt
  6. 2
      app/src/main/java/org/mosad/seil0/projectlaogai/fragments/HomeFragment.kt
  7. 52
      app/src/main/java/org/mosad/seil0/projectlaogai/fragments/MensaFragment.kt
  8. 11
      app/src/main/res/layout/fragment_mensa.xml
  9. 3
      app/src/main/res/values-de-rDE/strings.xml
  10. 3
      app/src/main/res/values/colors.xml
  11. 3
      app/src/main/res/values/strings.xml
  12. 5
      app/src/main/res/xml/backup_descriptor.xml

3
app/build.gradle

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

1
app/src/main/AndroidManifest.xml

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

4
app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt

@ -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)

48
app/src/main/java/org/mosad/seil0/projectlaogai/controller/CacheController.kt

@ -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,29 +96,8 @@ 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
* @param courseName the course name (e.g AI1)

42
app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt

@ -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)
*/

2
app/src/main/java/org/mosad/seil0/projectlaogai/fragments/HomeFragment.kt

@ -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
}

52
app/src/main/java/org/mosad/seil0/projectlaogai/fragments/MensaFragment.kt

@ -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()
}
}

11
app/src/main/res/layout/fragment_mensa.xml

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

3
app/src/main/res/values-de-rDE/strings.xml

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

3
app/src/main/res/values/colors.xml

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

3
app/src/main/res/values/strings.xml

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

5
app/src/main/res/xml/backup_descriptor.xml

@ -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>
Loading…
Cancel
Save