2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2018 < seil0 @mosad . xyz >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
package org.mosad.seil0.projectlaogai
import android.os.Bundle
2018-10-26 00:50:37 +02:00
import com.google.android.material.navigation.NavigationView
import androidx.core.view.GravityCompat
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
2018-10-24 18:22:05 +02:00
import android.view.Menu
import android.view.MenuItem
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.FragmentTransaction
2018-10-24 18:22:05 +02:00
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
2018-10-29 18:03:20 +01:00
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.*
2018-10-24 18:22:05 +02:00
class MainActivity : AppCompatActivity ( ) , NavigationView . OnNavigationItemSelectedListener {
//TODO make toolbar and navbar global
2018-10-29 18:03:20 +01:00
private val mensaParser = MensaParser ( )
private val timeTableParser = TimeTableParser ( )
private var weekMenus = ArrayList < Meal > ( )
private lateinit var timeTableWeek : Array < Array < Lesson > >
2018-10-24 18:22:05 +02:00
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity _main )
setSupportActionBar ( toolbar )
2018-10-29 18:03:20 +01:00
// load mensa and timetable
load ( )
2018-10-24 18:22:05 +02:00
//init home fragment TODO make a abstract fragment class
val homeFragment = HomeFragment ( )
2018-10-29 18:03:20 +01:00
homeFragment . setCurrentMenu ( weekMenus )
homeFragment . setCurrentTimeTableWeek ( timeTableWeek )
2018-10-26 00:50:37 +02:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
2018-10-24 18:22:05 +02:00
fragmentTransaction . replace ( R . id . fragment _container , homeFragment )
fragmentTransaction . commit ( )
val toggle = ActionBarDrawerToggle (
this , drawer _layout , toolbar , R . string . navigation _drawer _open , R . string . navigation _drawer _close
)
drawer _layout . addDrawerListener ( toggle )
toggle . syncState ( )
nav _view . setNavigationItemSelectedListener ( this )
}
override fun onBackPressed ( ) {
if ( drawer _layout . isDrawerOpen ( GravityCompat . START ) ) {
drawer _layout . closeDrawer ( GravityCompat . START )
} else {
super . onBackPressed ( )
}
}
override fun onCreateOptionsMenu ( menu : Menu ) : Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater . inflate ( R . menu . main , menu )
return true
}
override fun onOptionsItemSelected ( item : MenuItem ) : Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when ( item . itemId ) {
R . id . action _settings -> true
else -> super . onOptionsItemSelected ( item )
}
}
override fun onNavigationItemSelected ( item : MenuItem ) : Boolean {
// Handle navigation view item clicks here.
when ( item . itemId ) {
R . id . nav _home -> {
2018-10-25 18:52:33 +02:00
val homeFragment = HomeFragment ( )
2018-10-29 18:03:20 +01:00
homeFragment . setCurrentMenu ( weekMenus )
homeFragment . setCurrentTimeTableWeek ( timeTableWeek )
2018-10-26 00:50:37 +02:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
2018-10-25 18:52:33 +02:00
fragmentTransaction . replace ( R . id . fragment _container , homeFragment )
fragmentTransaction . commit ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _mensa -> {
2018-10-27 00:39:56 +02:00
val mensaFragment = MensaFragment ( )
2018-10-29 18:03:20 +01:00
mensaFragment . setWeekMenu ( weekMenus )
2018-10-26 00:50:37 +02:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
2018-10-25 18:52:33 +02:00
fragmentTransaction . replace ( R . id . fragment _container , mensaFragment )
fragmentTransaction . commit ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _timetable -> {
2018-10-27 14:11:47 +02:00
val timeTableFragment = TimeTableFragment ( )
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
fragmentTransaction . replace ( R . id . fragment _container , timeTableFragment )
fragmentTransaction . commit ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _moodle -> {
}
R . id . nav _email -> {
}
R . id . nav _settings -> {
2018-10-25 18:52:33 +02:00
val settingsFragment = SettingsFragment ( )
2018-10-26 00:50:37 +02:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
2018-10-25 18:52:33 +02:00
fragmentTransaction . replace ( R . id . fragment _container , settingsFragment )
fragmentTransaction . commit ( )
2018-10-24 18:22:05 +02:00
}
}
drawer _layout . closeDrawer ( GravityCompat . START )
return true
}
2018-10-29 18:03:20 +01:00
/ * *
* 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 ( )
}
2018-10-24 18:22:05 +02:00
}