/** * ProjectLaogai * * Copyright 2019 * * 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.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.GravityCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import com.afollestad.aesthetic.Aesthetic import com.google.android.material.navigation.NavigationView import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.app_bar_main.* import org.mosad.seil0.projectlaogai.controller.CacheController import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaMenu import org.mosad.seil0.projectlaogai.controller.PreferencesController 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 import org.mosad.seil0.projectlaogai.fragments.* import java.util.* import kotlin.system.measureTimeMillis class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { private var activeFragment: Fragment = HomeFragment() // the currently active fragment, home at the start override fun onCreate(savedInstanceState: Bundle?) { Aesthetic.attach(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) // load mensa, timetable and color load() initAesthetic() //init home fragment val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction() fragmentTransaction.replace(R.id.fragment_container, activeFragment) 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 onResume() { super.onResume() Aesthetic.resume(this) } override fun onPause() { super.onPause() Aesthetic.pause(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. activeFragment = when(item.itemId) { R.id.nav_home -> HomeFragment() R.id.nav_mensa -> MensaFragment() R.id.nav_timetable -> TimeTableFragment() R.id.nav_moodle -> MoodleFragment() R.id.nav_settings -> SettingsFragment() else -> HomeFragment() } val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction() fragmentTransaction.replace(R.id.fragment_container, activeFragment) fragmentTransaction.commit() drawer_layout.closeDrawer(GravityCompat.START) return true } /** * load the mensa menus of the current week */ private fun load() { val startupTime = measureTimeMillis { // load the settings PreferencesController.load(this) // this must be finished before doing anything else val tcor = TCoRAPIController(this) val currentTime = System.currentTimeMillis() / 1000 val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) val cal = Calendar.getInstance() // TODO this will backfire if someone has to update before the server finished updating the timetable at 0001/0101 // timetable sunday workaround cal.time = Date(timetableCacheTime * 1000) // 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 && cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) || currentTime - timetableCacheTime > 518400) { println("updating timetable after sunday!") val jobA = TCoRAPIController.getTimetable(cCourse.courseName, 0, this) val jobB = TCoRAPIController.getTimetable(cCourse.courseName, 1, this) jobA.get() jobB.get() } // new sunday bug fix CacheController.readMensa(this) cal.time = Date(mensaMenu.meta.updateTime * 1000) // if it's monday and the last cache update was on sunday or is older than 24hr, update blocking if ((currentDay == Calendar.MONDAY && cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || currentTime - mensaMenu.meta.updateTime > 86400) { println("update mensa blocking") TCoRAPIController.getMensa(this).get() } // get the cached files val cache = CacheController(this) cache.readStartCache(cCourse.courseName) // check if an update is necessary if(currentTime - coursesCacheTime > 86400) tcor.getCoursesList() if(currentTime - mensaCacheTime > 10800) TCoRAPIController.getMensa(this) if(currentTime - timetableCacheTime > 10800) { TCoRAPIController.getTimetable(cCourse.courseName, 0, this) TCoRAPIController.getTimetable(cCourse.courseName, 1, this) } } println("startup completed in $startupTime ms") } private fun initAesthetic() { // 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 { activityTheme(R.style.AppTheme) colorPrimary(Color.BLACK) colorPrimaryDark(Color.BLACK) colorAccent(Color.parseColor("#3f51b5")) apply() } SettingsFragment().selectCourse(this) } else { Aesthetic.config { // activityTheme(R.style.AppTheme_Light) colorPrimary(cColorPrimary) colorPrimaryDark(cColorPrimary) colorAccent(cColorAccent) apply() } } } }