2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < seil0 @mosad . xyz >
2018-10-24 18:22:05 +02:00
*
* 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
2019-02-17 15:05:03 +01:00
import androidx.fragment.app.Fragment
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-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-03-19 19:51:58 +01:00
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.PreferencesController
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorAccent
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
2018-11-19 12:00:39 +01:00
import org.mosad.seil0.projectlaogai.fragments.*
2019-03-18 19:09:03 +01:00
import java.sql.Date
import java.util.*
2018-11-01 02:12:43 +01:00
import kotlin.system.measureTimeMillis
2018-10-24 18:22:05 +02:00
class MainActivity : AppCompatActivity ( ) , NavigationView . OnNavigationItemSelectedListener {
2019-02-17 15:05:03 +01:00
private var activeFragment : Fragment = HomeFragment ( ) // the currently active fragment, home at the start
2019-02-14 16:25:13 +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 )
2019-01-03 13:46:09 +01:00
colorPrimaryDark ( Color . BLACK )
2019-04-02 13:23:37 +02:00
colorAccent ( Color . parseColor ( " #3F51B5 " ) )
2018-12-14 15:02:19 +01:00
apply ( )
}
2019-03-17 18:12:36 +01:00
2019-03-17 18:38:31 +01:00
SettingsFragment ( ) . selectCourse ( this )
2018-12-14 15:02:19 +01:00
} else {
Aesthetic . config {
2019-03-09 22:15:53 +01:00
colorPrimary ( cColorPrimary )
colorPrimaryDark ( cColorPrimary )
colorAccent ( cColorAccent )
2018-12-14 15:02:19 +01:00
apply ( )
}
}
2019-02-17 15:05:03 +01:00
//init home fragment
2018-10-26 00:50:37 +02:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
2019-02-17 15:05:03 +01:00
fragmentTransaction . replace ( R . id . fragment _container , activeFragment )
2018-10-24 18:22:05 +02:00
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 -> {
2019-02-17 15:05:03 +01:00
activeFragment = HomeFragment ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _mensa -> {
2019-02-17 15:05:03 +01:00
activeFragment = MensaFragment ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _timetable -> {
2019-02-17 15:05:03 +01:00
activeFragment = TimeTableFragment ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _moodle -> {
2019-02-17 15:05:03 +01:00
activeFragment = MoodleFragment ( )
2018-10-24 18:22:05 +02:00
}
R . id . nav _settings -> {
2019-02-17 15:05:03 +01:00
activeFragment = SettingsFragment ( )
2018-10-24 18:22:05 +02:00
}
}
2019-02-17 15:05:03 +01:00
val fragmentTransaction : FragmentTransaction = supportFragmentManager . beginTransaction ( )
fragmentTransaction . replace ( R . id . fragment _container , activeFragment )
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
* /
private fun load ( ) {
2019-03-17 18:12:36 +01:00
val startupTime = measureTimeMillis {
2019-04-06 14:13:01 +02:00
// load the settings
PreferencesController . load ( this ) // this must be finished before doing anything else
2019-03-18 19:09:03 +01:00
val tcor = TCoRAPIController ( this )
val currentTime = System . currentTimeMillis ( ) / 1000
val currentDay = Calendar . getInstance ( ) . get ( Calendar . DAY _OF _WEEK )
val cal = Calendar . getInstance ( )
2019-03-27 13:29:36 +01:00
// timetable sunday workaround
2019-03-18 19:09:03 +01:00
cal . time = Date ( timetableCacheTime * 1000 )
val timetableCacheDay = cal . get ( Calendar . DAY _OF _WEEK )
2019-03-23 21:07:33 +01:00
// TODO this sill backfire if someone has to update before the server finished updating the timetable at 0001/0101
2019-03-27 13:29:36 +01:00
// update blocking if a) it`s monday and the last cache was not on a monday or b) the cache is older than 6 days
if ( ( currentDay == Calendar . MONDAY && timetableCacheDay != Calendar . MONDAY ) || ( System . currentTimeMillis ( ) / 1000 ) - timetableCacheTime > 518400 ) {
2019-03-18 19:09:03 +01:00
println ( " updating timetable after sunday! " )
2019-04-06 14:13:01 +02:00
val jobA = TCoRAPIController . getTimetable ( cCourse . courseName , 0 , this )
val jobB = TCoRAPIController . getTimetable ( cCourse . courseName , 1 , this )
2019-03-18 19:09:03 +01:00
jobA . get ( )
jobB . get ( )
}
2018-11-01 02:12:43 +01:00
2019-03-27 13:29:36 +01:00
// mensa sunday workaround
cal . time = Date ( System . currentTimeMillis ( ) ) // reset to current time
// 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 ) {
2019-04-03 19:21:51 +02:00
val jobA = TCoRAPIController . getMensa ( this )
2019-03-27 13:29:36 +01:00
jobA . get ( )
}
2019-03-17 18:12:36 +01:00
// get the cached files
val cache = CacheController ( this )
cache . readStartCache ( cCourse . courseName )
2019-03-09 22:15:53 +01:00
2019-03-17 18:12:36 +01:00
// check if an update is necessary
if ( currentTime - coursesCacheTime > 86400 )
tcor . getCoursesList ( )
2018-11-02 15:18:52 +01:00
2019-03-17 18:12:36 +01:00
if ( currentTime - mensaCacheTime > 10800 )
2019-04-03 19:21:51 +02:00
TCoRAPIController . getMensa ( this )
2019-01-03 01:45:28 +01:00
2019-03-17 18:12:36 +01:00
if ( currentTime - timetableCacheTime > 10800 ) {
2019-04-06 14:13:01 +02:00
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , this )
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , this )
2019-03-17 18:12:36 +01:00
}
2018-11-01 02:12:43 +01:00
}
2019-04-06 14:13:01 +02:00
println ( " startup completed in $startupTime ms " )
2018-10-29 18:03:20 +01:00
}
2018-11-01 01:06:53 +01:00
2018-10-24 18:22:05 +02:00
}