added color saving

** WARNING this contains issues, many issues **
This commit is contained in:
2019-01-03 01:45:28 +01:00
parent e69354af96
commit 87bf614d28
8 changed files with 124 additions and 19 deletions

View File

@ -35,6 +35,9 @@ 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 kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.fragments.*
@ -48,10 +51,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private var weekMenus = ArrayList<Meal>()
private var courseTTLinkList = ArrayList<CourseTTLink>()
private var timeTableCurrentWeek = arrayOf<Array<Lesson>>()
private var timeTableNextWeek = arrayOf<Array<Lesson>>()
//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>>
private lateinit var course: CourseTTLink
private var color: Int = Color.BLACK
private var ls = Lesson("","","", "")
override fun onCreate(savedInstanceState: Bundle?) {
Aesthetic.attach(this)
@ -59,24 +67,26 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
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.YELLOW)
colorPrimary(Color.BLACK)
apply()
}
} else {
Aesthetic.config {
colorPrimary(Color.YELLOW)
colorPrimaryDark(Color.YELLOW)
colorPrimary(color)
colorPrimaryDark(color)
apply()
}
}
// load mensa and timetable
load()
//init home fragment TODO make a abstract fragment class
val homeFragment = HomeFragment()
@ -193,6 +203,17 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
timeTableCurrentWeek = timeTableParser.getTimeTable(course.courseTTLink.replace("http", "https"))
}
/**
* 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()
}
}
/**
* load the mensa menus of the current week
*/
@ -206,11 +227,53 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
sharedPref.getString(getString(R.string.save_key_course), "AI3")!!
)
// load saved color
color = sharedPref.getInt(getString(R.string.save_key_colorPrimary), Color.BLACK)
/**
* 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 {
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
/* getting the course list should be faster than the timetable,
* we need have time until the user opens the dialog
*/
@ -218,9 +281,14 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
courseTTLinkList = timeTableParser.getCourseTTLinks()
}
doAsync {
val t0 = doAsync {
try {
timeTableNextWeek = timeTableParser.getTimeTable(course.courseTTLink.replace("week=0","week=1"))
ls = timeTableNextWeek[0][0]
println("next: " + timeTableNextWeek.size)
println(timeTableNextWeek)
println(ls.lessonSubject)
timeTableParser.printTimeTableWeek(timeTableNextWeek)
} catch (e: Exception) {
e.stackTrace
}
@ -233,6 +301,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
val t2 = doAsync {
try {
timeTableCurrentWeek = timeTableParser.getTimeTable(course.courseTTLink)
println("current: " + timeTableCurrentWeek.size)
timeTableParser.printTimeTableWeek(timeTableCurrentWeek)
} catch (e: Exception) {
uiThread {
@ -246,11 +316,17 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
}
t0.get()
t1.get()
t2.get()
println("next end:" + timeTableNextWeek.size)
println(timeTableNextWeek)
timeTableParser.printTimeTableWeek(timeTableNextWeek)
println(ls.lessonSubject)
}
println("Completed in $time ms")
println(ls.lessonSubject)
}
fun getCourseTTLinkList(): ArrayList<CourseTTLink>{
@ -261,7 +337,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
return timeTableCurrentWeek
}
fun getTimeTableNextWeek(): Array<Array<Lesson>> {
fun getTimeTableNextWeek(): Array<Array<Lesson>> {
return timeTableNextWeek
}
@ -273,4 +349,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
return course
}
fun getColorPrimary(): Int {
return color
}
}

View File

@ -130,13 +130,15 @@ class SettingsFragment : Fragment() {
MaterialDialog(context!!)
.title(R.string.primary_color)
.colorChooser(colors, initialSelection = Color.BLACK) { _, color ->
.colorChooser(colors, initialSelection = mainActivity.getColorPrimary()) { _, color ->
viewPrimaryColor.setBackgroundColor(color)
Aesthetic.config {
colorPrimary(color)
colorPrimaryDark(color)
apply()
}
mainActivity.saveColorPrimary(color)
}
.positiveButton(R.string.select)
.show()

View File

@ -35,6 +35,7 @@ import org.mosad.seil0.projectlaogai.MainActivity
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.hsoparser.TimeTableParser
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import java.text.SimpleDateFormat
@ -72,6 +73,9 @@ class TimeTableFragment : Fragment() {
val formatter = SimpleDateFormat("E dd.MM", Locale.GERMANY) // TODO change to android call when min api is 24
val calendar = Calendar.getInstance()
println(mainActivity.getTimeTableCurrentWeek().size)
println(mainActivity.getTimeTableNextWeek().size)
doAsync {
uiThread {
@ -91,11 +95,21 @@ class TimeTableFragment : Fragment() {
lessonCardView.getTxtViewLesson().append(lesson.lessonRoom)
lessonCardView.getTxtViewTime().text = DataTypes().getTime()[i]
// only add the lesson if it contains data
if(lessonCardView.getTxtViewLesson().text.length > 2)
cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView)
}
calendar.add(Calendar.DATE,1)
// if the day contains no lessons add a text "No lesson today"
if (cardViewTimeTableDay.getLinLayoutMensaDay().childCount <= 1) {
val lessonCardView = LessonCardView(context!!, null)
lessonCardView.setBackgroundColor(Color.TRANSPARENT)
lessonCardView.getTxtViewLesson().text = resources.getString(R.string.no_lesson_today)
cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView)
}
linLayoutTTFragment.addView(cardViewTimeTableDay)
}
@ -108,6 +122,9 @@ class TimeTableFragment : Fragment() {
val cardViewTimeTableDay = MensaDayCardView(context!!, null)
cardViewTimeTableDay.setDayHeading(formatter.format(calendar.time))
//val ttp = TimeTableParser()
//ttp.printTimeTableWeek(mainActivity.getTimeTableNextWeek())
// for each lessen of the day
for((i, lesson) in mainActivity.getTimeTableNextWeek()[day].withIndex()) {
val lessonCardView = LessonCardView(context!!, null)