215 lines
7.4 KiB
Kotlin
215 lines
7.4 KiB
Kotlin
/**
|
|
* ProjectLaogai
|
|
*
|
|
* Copyright 2019 <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.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.afollestad.materialdialogs.MaterialDialog
|
|
import com.google.android.material.navigation.NavigationView
|
|
import kotlinx.android.synthetic.main.activity_main.*
|
|
import kotlinx.android.synthetic.main.app_bar_main.*
|
|
import org.jetbrains.anko.doAsync
|
|
import org.jetbrains.anko.uiThread
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColor
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourse
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourseTTLinkList
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableCurrentWeek
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableNextWeek
|
|
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cWeekMenus
|
|
import org.mosad.seil0.projectlaogai.fragments.*
|
|
import org.mosad.seil0.projectlaogai.hsoparser.MensaParser
|
|
import org.mosad.seil0.projectlaogai.hsoparser.TimeTableParser
|
|
import kotlin.system.measureTimeMillis
|
|
|
|
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
|
|
|
|
private val mensaParser = MensaParser()
|
|
private val timeTableParser = TimeTableParser()
|
|
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)
|
|
apply()
|
|
}
|
|
} else {
|
|
Aesthetic.config {
|
|
colorPrimary(cColor)
|
|
colorPrimaryDark(cColor)
|
|
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
|
|
|
|
/**
|
|
* load mensa, course timetable and courselist from the swfr/hso website
|
|
* TODO make an API see https://git.mosad.xyz/Seil0/TheCitadelofRicks
|
|
*/
|
|
val time = measureTimeMillis {
|
|
|
|
/* getting the course list should be faster than the timetable,
|
|
* we need have time until the user opens the dialog
|
|
*/
|
|
doAsync {
|
|
cCourseTTLinkList = timeTableParser.getCourseTTLinks()
|
|
}
|
|
|
|
val jobMenus = doAsync {
|
|
cWeekMenus = mensaParser.getMensaMenu()
|
|
}
|
|
|
|
val jobTTCurrentWeek = doAsync {
|
|
try {
|
|
cTimeTableCurrentWeek = timeTableParser.getTimeTable(cCourse.courseLink)
|
|
} catch (e: Exception) {
|
|
|
|
uiThread {
|
|
MaterialDialog(this@MainActivity)
|
|
.title(R.string.error)
|
|
.message(R.string.no_tt_error)
|
|
.show()
|
|
}
|
|
|
|
e.stackTrace
|
|
}
|
|
}
|
|
|
|
val jobTTNextWeek = doAsync {
|
|
try {
|
|
cTimeTableNextWeek = timeTableParser.getTimeTable(cCourse.courseLink.replace("week=0","week=1"))
|
|
} catch (e: Exception) {
|
|
e.stackTrace
|
|
}
|
|
}
|
|
|
|
jobMenus.get()
|
|
jobTTCurrentWeek.get()
|
|
jobTTNextWeek.get()
|
|
}
|
|
println("Completed in $time ms")
|
|
}
|
|
|
|
}
|