/** * 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.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.sql.Date 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() // 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 { colorPrimary(Color.BLACK) colorPrimaryDark(Color.BLACK) colorAccent(Color.parseColor("#FF1744")) apply() } SettingsFragment().selectCourse(this) } else { Aesthetic.config { colorPrimary(cColorPrimary) colorPrimaryDark(cColorPrimary) colorAccent(cColorAccent) apply() } } //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. when (item.itemId) { R.id.nav_home -> { activeFragment = HomeFragment() } R.id.nav_mensa -> { activeFragment = MensaFragment() } R.id.nav_timetable -> { activeFragment = TimeTableFragment() } R.id.nav_moodle -> { activeFragment = MoodleFragment() } R.id.nav_settings -> { activeFragment = SettingsFragment() } } 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() { // load the settings PreferencesController.load(this) // this must be finished before doing anything else val startupTime = measureTimeMillis { val tcor = TCoRAPIController(this) val currentTime = System.currentTimeMillis() / 1000 val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) val cal = Calendar.getInstance() // timetable sunday workaround cal.time = Date(timetableCacheTime * 1000) val timetableCacheDay = cal.get(Calendar.DAY_OF_WEEK) // TODO this sill backfire if someone has to update before the server finished updating the timetable at 0001/0101 // 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) { println("updating timetable after sunday!") val jobA = tcor.getTimetable(cCourse.courseName, 0) val jobB = tcor.getTimetable(cCourse.courseName, 1) jobA.get() jobB.get() } // 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) { val jobA = tcor.getMensa() jobA.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) tcor.getMensa() if(currentTime - timetableCacheTime > 10800) { tcor.getTimetable(cCourse.courseName, 0) tcor.getTimetable(cCourse.courseName, 1) } } println("Completed in $startupTime ms") } }