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
2018-12-14 15:02:19 +01:00
import android.graphics.Color
2018-10-24 18:22:05 +02:00
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
2018-11-05 20:59:41 +01:00
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.FragmentTransaction
2018-12-14 15:02:19 +01:00
import com.afollestad.aesthetic.Aesthetic
2018-11-11 21:12:28 +01:00
import com.afollestad.materialdialogs.MaterialDialog
2018-11-05 20:59:41 +01:00
import com.google.android.material.navigation.NavigationView
2018-10-24 18:22:05 +02:00
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
2019-01-03 01:45:28 +01:00
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
2018-10-29 18:03:20 +01:00
import org.jetbrains.anko.doAsync
2018-11-11 21:12:28 +01:00
import org.jetbrains.anko.uiThread
2018-11-19 12:00:39 +01:00
import org.mosad.seil0.projectlaogai.fragments.*
2018-11-01 02:12:43 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.*
import kotlin.system.measureTimeMillis
2018-10-24 18:22:05 +02:00
class MainActivity : AppCompatActivity ( ) , NavigationView . OnNavigationItemSelectedListener {
2018-10-29 18:03:20 +01:00
private val mensaParser = MensaParser ( )
private val timeTableParser = TimeTableParser ( )
private var weekMenus = ArrayList < Meal > ( )
2018-11-01 02:12:43 +01:00
private var courseTTLinkList = ArrayList < CourseTTLink > ( )
2019-01-03 01:45:28 +01:00
//private var timeTableCurrentWeek = arrayOf<Array<Lesson>>()
//private var timeTableNextWeek = arrayOf<Array<Lesson>>()
private lateinit var timeTableCurrentWeek : Array < Array < Lesson > >
private lateinit var timeTableNextWeek : Array < Array < Lesson > >
2018-10-24 18:22:05 +02:00
2018-11-07 14:17:33 +01:00
private lateinit var course : CourseTTLink
2019-01-03 01:45:28 +01:00
private var color : Int = Color . BLACK
private var ls = Lesson ( " " , " " , " " , " " )
2018-11-07 14:17:33 +01:00
2018-10-24 18:22:05 +02:00
override fun onCreate ( savedInstanceState : Bundle ? ) {
2018-12-14 15:02:19 +01:00
Aesthetic . attach ( this )
2018-10-24 18:22:05 +02:00
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity _main )
setSupportActionBar ( toolbar )
2019-01-03 01:45:28 +01:00
// load mensa, timetable and color
load ( )
2018-12-14 15:02:19 +01:00
// If we haven't set any defaults, do that now
if ( Aesthetic . isFirstTime ) {
// this is executed on the first app start, use this to show tutorial etc.
Aesthetic . config {
2019-01-03 01:45:28 +01:00
colorPrimary ( Color . BLACK )
2018-12-14 15:02:19 +01:00
apply ( )
}
} else {
Aesthetic . config {
2019-01-03 01:45:28 +01:00
colorPrimary ( color )
colorPrimaryDark ( color )
2018-12-14 15:02:19 +01:00
apply ( )
}
}
2018-11-19 12:00:39 +01:00
2019-01-03 01:45:28 +01:00
2018-10-29 18:03:20 +01:00
2018-10-24 18:22:05 +02:00
//init home fragment TODO make a abstract fragment class
val homeFragment = HomeFragment ( )
2018-11-01 02:12:43 +01:00
homeFragment . setMainActivity ( this )
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 )
}
2018-12-14 15:02:19 +01:00
override fun onResume ( ) {
super . onResume ( )
Aesthetic . resume ( this )
}
override fun onPause ( ) {
super . onPause ( )
Aesthetic . pause ( this )
}
2018-10-24 18:22:05 +02:00
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-11-01 02:12:43 +01:00
homeFragment . setMainActivity ( this )
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-11-01 02:12:43 +01:00
mensaFragment . setMainActivity ( this )
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 ( )
2018-11-06 16:17:41 +01:00
timeTableFragment . setMainActivity ( this )
2018-10-27 14:11:47 +02:00
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 -> {
2018-11-30 13:54:39 +01:00
val moodleFragment = MoodleFragment ( )
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
fragmentTransaction . replace ( R . id . fragment _container , moodleFragment )
fragmentTransaction . commit ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _settings -> {
2018-10-25 18:52:33 +02:00
val settingsFragment = SettingsFragment ( )
2018-11-01 02:12:43 +01:00
settingsFragment . setMainActivity ( this )
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
2018-11-01 02:12:43 +01:00
/ * *
2018-11-07 14:17:33 +01:00
* update the gui with the data of the new selected course
* save selected course and courseTTLink
2018-11-01 02:12:43 +01:00
* /
fun updateCourse ( course : CourseTTLink ) {
println ( course . course )
println ( course . courseTTLink )
2018-11-07 14:17:33 +01:00
this . course = course
// save new selected course
val sharedPref = getPreferences ( MODE _PRIVATE ) ?: return
with ( sharedPref . edit ( ) ) {
putString ( getString ( R . string . save _key _course ) , course . course )
putString ( getString ( R . string . save _key _courseTTLink ) , course . courseTTLink . replace ( " http " , " https " ) )
apply ( )
}
2018-11-12 12:32:42 +01:00
timeTableCurrentWeek = timeTableParser . getTimeTable ( course . courseTTLink . replace ( " http " , " https " ) )
2018-11-01 02:12:43 +01:00
}
2019-01-03 01:45:28 +01:00
/ * *
* save the primary color
* /
fun saveColorPrimary ( color : Int ) {
val sharedPref = getPreferences ( MODE _PRIVATE ) ?: return
with ( sharedPref . edit ( ) ) {
putInt ( getString ( R . string . save _key _colorPrimary ) , color )
apply ( )
}
}
2018-10-29 18:03:20 +01:00
/ * *
* load the mensa menus of the current week
* /
private fun load ( ) {
2018-11-07 14:17:33 +01:00
// load saved course
val sharedPref = getPreferences ( MODE _PRIVATE ) ?: return
course = CourseTTLink (
sharedPref . getString ( getString ( R . string . save _key _courseTTLink ) ,
" https://www.hs-offenburg.de/index.php?id=6627&class=class&iddV=DA64F6FE-9DDB-429E-A677-05D0D40CB636&week=0 " ) !! ,
sharedPref . getString ( getString ( R . string . save _key _course ) , " AI3 " ) !!
)
2019-01-03 01:45:28 +01:00
// load saved color
color = sharedPref . getInt ( getString ( R . string . save _key _colorPrimary ) , Color . BLACK )
2018-11-07 14:17:33 +01:00
/ * *
* load mensa , course timetable and courselist from the swfr / hso website
* TODO make an API see https : //git.mosad.xyz/Seil0/TheCitadelofRicks
* /
2018-11-01 02:12:43 +01:00
val time = measureTimeMillis {
2019-01-03 01:45:28 +01:00
runBlocking {
/ * getting the course list should be faster than the timetable ,
* we need have time until the user opens the dialog
* /
val jobTTLinkList = GlobalScope . launch {
courseTTLinkList = timeTableParser . getCourseTTLinks ( )
}
val jobTTNextWeek = GlobalScope . launch {
timeTableNextWeek = timeTableParser . getTimeTable ( course . courseTTLink . replace ( " week=0 " , " week=1 " ) )
println ( " next: " + timeTableNextWeek . size )
println ( timeTableNextWeek )
println ( ls . lessonSubject )
timeTableParser . printTimeTableWeek ( timeTableNextWeek ) // Wieso sind die daten hier noch im array
}
val jobTTCurrentWeek = GlobalScope . launch {
timeTableCurrentWeek = timeTableParser . getTimeTable ( course . courseTTLink )
}
val jobMenus = GlobalScope . launch {
weekMenus = mensaParser . getMensaMenu ( )
}
jobTTLinkList . join ( ) // wait until child coroutine completes
jobTTNextWeek . join ( )
jobTTCurrentWeek . join ( )
jobMenus . join ( )
println ( " next end: " + timeTableNextWeek . size )
println ( timeTableNextWeek )
timeTableParser . printTimeTableWeek ( timeTableNextWeek ) // und hier nicht mehr
}
return
2018-11-01 02:12:43 +01:00
/ * getting the course list should be faster than the timetable ,
* we need have time until the user opens the dialog
* /
doAsync {
courseTTLinkList = timeTableParser . getCourseTTLinks ( )
}
2019-01-03 01:45:28 +01:00
val t0 = doAsync {
2018-11-12 12:32:42 +01:00
try {
timeTableNextWeek = timeTableParser . getTimeTable ( course . courseTTLink . replace ( " week=0 " , " week=1 " ) )
2019-01-03 01:45:28 +01:00
ls = timeTableNextWeek [ 0 ] [ 0 ]
println ( " next: " + timeTableNextWeek . size )
println ( timeTableNextWeek )
println ( ls . lessonSubject )
timeTableParser . printTimeTableWeek ( timeTableNextWeek )
2018-11-12 12:32:42 +01:00
} catch ( e : Exception ) {
e . stackTrace
}
}
2018-11-02 15:18:52 +01:00
val t1 = doAsync {
2018-11-01 02:12:43 +01:00
weekMenus = mensaParser . getMensaMenu ( )
2018-11-02 15:18:52 +01:00
}
val t2 = doAsync {
2018-11-06 10:47:51 +01:00
try {
2018-11-12 12:32:42 +01:00
timeTableCurrentWeek = timeTableParser . getTimeTable ( course . courseTTLink )
2019-01-03 01:45:28 +01:00
println ( " current: " + timeTableCurrentWeek . size )
timeTableParser . printTimeTableWeek ( timeTableCurrentWeek )
2018-11-11 21:12:28 +01:00
} catch ( e : Exception ) {
uiThread {
MaterialDialog ( this @MainActivity )
. title ( R . string . error )
. message ( R . string . no _tt _error )
. show ( )
}
2018-11-06 10:47:51 +01:00
e . stackTrace
}
2018-11-02 15:18:52 +01:00
}
2019-01-03 01:45:28 +01:00
t0 . get ( )
2018-11-02 15:18:52 +01:00
t1 . get ( )
t2 . get ( )
2019-01-03 01:45:28 +01:00
println ( " next end: " + timeTableNextWeek . size )
println ( timeTableNextWeek )
timeTableParser . printTimeTableWeek ( timeTableNextWeek )
println ( ls . lessonSubject )
2018-11-01 02:12:43 +01:00
}
println ( " Completed in $time ms " )
2019-01-03 01:45:28 +01:00
println ( ls . lessonSubject )
2018-10-29 18:03:20 +01:00
}
2018-11-01 01:06:53 +01:00
2018-11-01 02:12:43 +01:00
fun getCourseTTLinkList ( ) : ArrayList < CourseTTLink > {
return courseTTLinkList
}
2018-11-12 12:32:42 +01:00
fun getTimeTableCurrentWeek ( ) : Array < Array < Lesson > > {
return timeTableCurrentWeek
}
2019-01-03 01:45:28 +01:00
fun getTimeTableNextWeek ( ) : Array < Array < Lesson > > {
2018-11-12 12:32:42 +01:00
return timeTableNextWeek
2018-11-01 02:12:43 +01:00
}
fun getWeekMenu ( ) : ArrayList < Meal > {
return weekMenus
}
2018-11-02 15:18:52 +01:00
2018-11-07 14:17:33 +01:00
fun getCourse ( ) : CourseTTLink {
return course
}
2019-01-03 01:45:28 +01:00
fun getColorPrimary ( ) : Int {
return color
}
2018-10-24 18:22:05 +02:00
}