Browse Source

added TimeTableParser

* you can access the current timetable and mensa menu now
* you can access the weeks mensa menu now
pull/20/head
Jannik 4 years ago
parent
commit
f9a122558d
  1. 82
      app/src/main/java/org/mosad/seil0/projectlaogai/HomeFragment.kt
  2. 34
      app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt
  3. 10
      app/src/main/java/org/mosad/seil0/projectlaogai/MensaFragment.kt
  4. 4
      app/src/main/java/org/mosad/seil0/projectlaogai/SettingsFragment.kt
  5. 6
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/DataTypes.kt
  6. 15
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/MensaParser.kt
  7. 28
      app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/TimeTableParser.kt
  8. 9
      app/src/main/res/layout/fragment_home.xml
  9. 9
      app/src/main/res/values/strings.xml

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

@ -22,19 +22,18 @@
package org.mosad.seil0.projectlaogai
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.Lesson
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.MensaParser
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
@ -46,37 +45,29 @@ import java.util.*
*/
class HomeFragment : Fragment() {
private lateinit var txtViewMenu1: TextView
private lateinit var txtViewMenu2: TextView
private lateinit var scrollViewTimeTable: ScrollView
val times = arrayOf("8.00 - 9.30", "9.45 - 11.15" ,"11.35 - 13.05", "14.00 -15.30", "15.45 - 17.15", "17.30 - 19.00")
private lateinit var linLayoutTimeTable: LinearLayout
private lateinit var currentMenus: ArrayList<Meal>
private lateinit var timeTableWeek: Array<Array<Lesson>>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
// init UI elements
txtViewMenu1 = view.findViewById(R.id.txtView_Menu1)
txtViewMenu2 = view.findViewById(R.id.txtView_Menu2)
scrollViewTimeTable = view.findViewById(R.id.scrollView_TimeTable)
linLayoutTimeTable = view.findViewById(R.id.linLayout_TimeTable)
linLayoutTimeTable = view.findViewById(R.id.linLayoutTimeTable)
//setText()
addCurentMensaMenu()
addLessons()
addCurrentMensaMenu()
addCurrentTimeTable()
// Inflate the layout for this fragment
return view
}
private fun addCurentMensaMenu() {
doAsync {
val mensaParser = MensaParser()
val dayMenus: ArrayList<Meal> = mensaParser.getMensaMenuDay(mensaParser.getMensaMenu(), Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
private fun addCurrentMensaMenu() {
doAsync {
val dayMenus: ArrayList<Meal> = MensaParser().getMensaMenuDay(currentMenus, Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
uiThread {
for(part in dayMenus[0].parts) {
@ -87,46 +78,33 @@ class HomeFragment : Fragment() {
txtViewMenu2.append(part)
}
}
}
}
@SuppressLint("SetTextI18n")
private fun addLessons() {
// for each lesson to display on homescreen create a cardview
val lessonCardView1 = LessonCardView(context!!, null)
val lessonCardView2 = LessonCardView(context!!, null)
val lessonCardView3 = LessonCardView(context!!, null)
val lessonCardView4 = LessonCardView(context!!, null)
val lessonCardView5 = LessonCardView(context!!, null)
private fun addCurrentTimeTable() {
val timeTableDay = timeTableWeek[Calendar.getInstance().get(Calendar.DAY_OF_WEEK) -2]
lessonCardView1.getTxtViewLesson().text = "Computernetze 1\nMayer E.\nA213"
lessonCardView1.getTxtViewTime().text = "8.00 - 9.30"
for (i in 0..5) {
val lessonCardView = LessonCardView(context!!, null)
lessonCardView2.getTxtViewLesson().text = "Computernetze 1\nMayer E.\nA213"
lessonCardView2.getTxtViewTime().text = "8.00 - 9.30"
lessonCardView.getTxtViewLesson().text = """${timeTableDay[i].lessonSubject}
"""
lessonCardView.getTxtViewLesson().append(timeTableDay[i].lessonTeacher + "\n")
lessonCardView.getTxtViewLesson().append(timeTableDay[i].lessonRoom)
lessonCardView.getTxtViewTime().text = DataTypes().getTime()[i]
lessonCardView3.getTxtViewLesson().text = "Computernetze 1\nMayer E.\nA213"
lessonCardView3.getTxtViewTime().text = "8.00 - 9.30"
lessonCardView4.getTxtViewLesson().text = "Computernetze 1\nMayer E.\nA213"
lessonCardView4.getTxtViewTime().text = "8.00 - 9.30"
lessonCardView5.getTxtViewLesson().text = "Computernetze 1\nMayer E.\nA213"
lessonCardView5.getTxtViewTime().text = "8.00 - 9.30"
if(lessonCardView.getTxtViewLesson().text.length > 2)
linLayoutTimeTable.addView(lessonCardView)
}
}
lessonCardView1.setBackgroundColor(Color.parseColor("#3F51B5"))
// ff00ddff
fun setCurrentMenu(weekMenus: ArrayList<Meal>){
this.currentMenus = weekMenus
}
linLayoutTimeTable.run {
addView(lessonCardView1)
addView(lessonCardView2)
addView(lessonCardView3)
addView(lessonCardView4)
addView(lessonCardView5)
}
fun setCurrentTimeTableWeek(timeTableWeek: Array<Array<Lesson>>) {
this.timeTableWeek = timeTableWeek
}
}

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

@ -32,18 +32,34 @@ import android.view.MenuItem
import androidx.fragment.app.FragmentTransaction
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import org.jetbrains.anko.doAsync
import org.mosad.seil0.projectlaogai.hsoparser.Lesson
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.MensaParser
import org.mosad.seil0.projectlaogai.hsoparser.TimeTableParser
import java.util.*
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
//TODO make toolbar and navbar global
private val mensaParser = MensaParser()
private val timeTableParser = TimeTableParser()
private var weekMenus = ArrayList<Meal>()
private lateinit var timeTableWeek: Array<Array<Lesson>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// load mensa and timetable
load()
//init home fragment TODO make a abstract fragment class
val homeFragment = HomeFragment()
homeFragment.setCurrentMenu(weekMenus)
homeFragment.setCurrentTimeTableWeek(timeTableWeek)
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, homeFragment)
fragmentTransaction.commit()
@ -86,18 +102,20 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
when (item.itemId) {
R.id.nav_home -> {
val homeFragment = HomeFragment()
homeFragment.setCurrentMenu(weekMenus)
homeFragment.setCurrentTimeTableWeek(timeTableWeek)
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, homeFragment)
fragmentTransaction.commit()
}
R.id.nav_mensa -> {
val mensaFragment = MensaFragment()
mensaFragment.setWeekMenu(weekMenus)
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, mensaFragment)
fragmentTransaction.commit()
}
R.id.nav_timetable -> {
val timeTableFragment = TimeTableFragment()
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, timeTableFragment)
@ -121,4 +139,18 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
/**
* load the mensa menus of the current week
* TODO show loading screen while loading
* TODO make mensa nad timetable query parallel
* TODO evaluate if we should use a timeout here
*/
private fun load() {
doAsync {
weekMenus = mensaParser.getMensaMenu()
timeTableWeek = timeTableParser.getTimeTable("https://www.hs-offenburg.de/index.php?id=6627&class=class&iddV=DA64F6FE-9DDB-429E-A677-05D0D40CB636&week=0")
}.get()
}
}

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

@ -22,14 +22,12 @@
package org.mosad.seil0.projectlaogai
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.mensaday_cardview.view.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.hsoparser.Meal
@ -45,6 +43,7 @@ import java.util.*
class MensaFragment : Fragment() {
private lateinit var linLayoutMensaFragment: LinearLayout
private lateinit var weekMenus: ArrayList<Meal>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -53,16 +52,14 @@ class MensaFragment : Fragment() {
linLayoutMensaFragment = view.findViewById(R.id.linLayout_MensaFragment)
addCurrentWeek()
//addDay(Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
return view
}
fun addCurrentWeek() {
private fun addCurrentWeek() {
doAsync {
val mensaParser = MensaParser()
val weekMenus: ArrayList<Meal> = mensaParser.getMensaMenu()
uiThread {
@ -135,5 +132,8 @@ class MensaFragment : Fragment() {
}
fun setWeekMenu(weekMenus: ArrayList<Meal>){
this.weekMenus = weekMenus
}
}

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

@ -74,7 +74,7 @@ class SettingsFragment : Fragment() {
// open a new dialog
val courses = listOf("AI1", "AI2", "AI3", "AI4", "AI5", "AI6", "AI7")
MaterialDialog(context!!)
.listItems(items = courses){ dialog, index, text ->
.listItems(items = courses){ _, _, text ->
// TODO save selected course
// update txtView
@ -97,7 +97,7 @@ class SettingsFragment : Fragment() {
MaterialDialog(context!!)
.title(R.string.primary_color)
.colorChooser(colors, initialSelection = Color.parseColor("#3F51B5")) { dialog, color ->
.colorChooser(colors, initialSelection = Color.parseColor("#3F51B5")) { _, color ->
viewPrimaryColor.setBackgroundColor(color)
}
.positiveButton(R.string.select)

6
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/DataTypes.kt

@ -1,10 +1,16 @@
package org.mosad.seil0.projectlaogai.hsoparser
class DataTypes {
val times = arrayOf("8.00 - 9.30", "9.45 - 11.15" ,"11.35 - 13.05", "14.00 -15.30", "15.45 - 17.15", "17.30 - 19.00")
init {
// do something
}
fun getTime(): Array<String> {
return times
}
}
data class Lesson(val lessonSubject: String, val lessonTeacher: String, val lessonRoom:String, val lessonRemark: String)

15
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/MensaParser.kt

@ -38,13 +38,13 @@ class MensaParser {
fun getMensaMenu(): ArrayList<Meal> {
val menuHTML = Jsoup.connect("https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/").get()
menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan").forEachIndexed { index, element ->
var day = element.select("h3").text()
menuHTML.select("#speiseplan-tabs").select("div.tab-content").select("div.menu-tagesplan").forEachIndexed { _, element ->
val day = element.select("h3").text()
for (i in 0 .. (element.select("div.row h4").size - 1)) {
try {
var heading = element.select("div.row h4")[i].text()
var parts = ArrayList<String>(element.select("div.row").select("div.menu-info")[i].html().substringBefore("<span").replace("<br>", "|").split("|"))
var additives = element.select("div.row").select("div.menu-info")[i].select("span.show-with-allergenes").text()
val heading = element.select("div.row h4")[i].text()
val parts = ArrayList<String>(element.select("div.row").select("div.menu-info")[i].html().substringBefore("<span").replace("<br>", "|").split("|"))
val additives = element.select("div.row").select("div.menu-info")[i].select("span.show-with-allergenes").text()
mealList.add(Meal(day, heading, parts, additives))
} catch (e: Exception) {
@ -60,8 +60,8 @@ class MensaParser {
* return the mensa menu of a given day (Mon - Sat)
*/
fun getMensaMenuDay(mealList: ArrayList<Meal>, day: Int): ArrayList<Meal> {
var dayMenus = ArrayList<Meal>()
var strDay: String = when(day) {
val dayMenus = ArrayList<Meal>()
val strDay: String = when(day) {
Calendar.MONDAY -> "Mon"
Calendar.TUESDAY -> "Die"
Calendar.WEDNESDAY -> "Mit"
@ -73,7 +73,6 @@ class MensaParser {
for (meal in mealList) {
if (meal.day.contains(strDay)) {
println(meal.day)
dayMenus.add(meal)
}
}

28
app/src/main/java/org/mosad/seil0/projectlaogai/hsoparser/TimeTableParser.kt

@ -25,15 +25,15 @@ package org.mosad.seil0.projectlaogai.hsoparser
import org.jsoup.Jsoup
class TimeTableParser {
val days = arrayOf("Monday", "Tuesday" ,"Wednesday", "Thursday", "Friday", "Saturday")
var courseTTLinkList = ArrayList<CourseTTLink>()
var timeTableWeek = arrayOf<Array<Lesson>>()
private val days = arrayOf("Monday", "Tuesday" ,"Wednesday", "Thursday", "Friday", "Saturday")
private var courseTTLinkList = ArrayList<CourseTTLink>()
private var timeTableWeek = arrayOf<Array<Lesson>>()
init {
// create the timetable array
for (i in 0..5) {
var timeTableDay = arrayOf<Lesson>()
for (j in 0..6) {
for (j in 0..5) {
timeTableDay += Lesson("", "","","")
}
timeTableWeek += timeTableDay
@ -45,15 +45,16 @@ class TimeTableParser {
* the timetable is organised per row not per column;
* Mon 1, Tue 1, Wed 1, Thur 1, Fri 1, Sat 1, Mon 2 and so on
*/
fun getTimeTable(courseTTURL: String) {
fun getTimeTable(courseTTURL: String): Array<Array<Lesson>> {
val scheduleHTML = Jsoup.connect(courseTTURL).get()
val week = scheduleHTML.select("h1.timetable-caption").text()
println("$week successful!\n")
//val week = scheduleHTML.select("h1.timetable-caption").text()
//println("$week successful!\n")
scheduleHTML.select("table.timetable").select("td.lastcol").forEachIndexed { index, element ->
timeTableWeek[index % 6][index / 6] = Lesson(element.select("div.lesson-subject").text(), element.select("div.lesson-teacher").text(), element.select("div.lesson-room").text(), element.select("div.lesson-remark").text())
}
return timeTableWeek
}
/**
@ -74,7 +75,7 @@ class TimeTableParser {
for (j in 0..5) print("-".padEnd(26 + (j.toFloat().div(j).toInt()), '-') + "+")
println()
for (i in 0..6) {
for (i in 0..5) {
for (j in 0..5) print(timeTableWeek[j][i].lessonSubject.padEnd(25 ,' ').substring(0,25) + " | ")
println()
for (j in 0..5) print(timeTableWeek[j][i].lessonTeacher.padEnd(25 ,' ').substring(0,25) + " | ")
@ -87,15 +88,4 @@ class TimeTableParser {
println()
}
fun printTimeTableDay(timeTableDay: Array<Lesson>, day: Int) {
println(days[day])
for (i in 0..6) {
println("-".padEnd(22, '-'))
println(timeTableDay[i].lessonSubject)
println(timeTableDay[i].lessonTeacher)
println(timeTableDay[i].lessonRoom)
}
println("-".padEnd(22, '-'))
println()
}
}

9
app/src/main/res/layout/fragment_home.xml

@ -37,14 +37,13 @@
android:textStyle="bold" android:textAlignment="center" android:textSize="16sp"
android:typeface="sans" android:fontFamily="sans-serif" android:paddingBottom="5dp"/>
<TextView
android:id="@+id/txtView_Menu1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="sans"/>
android:typeface="sans" android:id="@+id/txtViewMenu1"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
@ -72,7 +71,7 @@
android:textAlignment="center" android:textStyle="bold" android:textSize="16sp"
android:typeface="sans" android:fontFamily="sans-serif" android:paddingBottom="5dp"/>
<TextView
android:id="@+id/txtView_Menu2"
android:id="@+id/txtViewMenu2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
@ -88,11 +87,11 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2" android:id="@+id/scrollView_TimeTable"
app:layout_constraintTop_toBottomOf="@+id/cardView2" android:id="@+id/scrollViewTimeTable"
android:background="@color/colorPrimary" android:paddingTop="6dp">
<LinearLayout
android:id="@+id/linLayout_TimeTable"
android:id="@+id/linLayoutTimeTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"

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

@ -2,7 +2,7 @@
<string name="app_name">Project Laogai</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">hso App 0.0.3</string>
<string name="nav_header_title">hso App 0.1.0</string>
<string name="nav_header_subtitle">seil0@mosad.xyz</string>
<string name="nav_header_desc">Navigation header</string>
@ -18,7 +18,7 @@
<string name="essen_2">Essen 2</string>
<string name="a_lesson">a lesson\na teacher\na room</string>
<string name="a_time">0.00 - 23.59</string>
<string name="a_time">0.00 23.59</string>
<string name="info">Info</string>
<string name="user">User</string>
@ -29,13 +29,12 @@
<string name="primary_color">primary color</string>
<string name="main_color_desc">The primary color, default is indigo</string>
<string name="select">select</string>
<string name="version">version 0.0.2</string>
<string name="version">version 0.1.0</string>
<string name="about">about</string>
<string name="about_text">This software is made by @Seil0 and is published under the terms and conditions of GPL 3.
For further information visit https://git.mosad.xyz/Seil0/ProjectLaogai\n\n(c) 2018 seil0@mosad.xyz
For further information visit git.mosad.xyz/Seil0/ProjectLaogai\n\n© 2018 seil0@mosad.xyz
</string>
<string name="about_txtView">hso App by @Seil0</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close