ProjectLaogai/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt

152 lines
5.6 KiB
Kotlin
Raw Normal View History

2018-11-06 16:17:41 +01:00
/**
* ProjectLaogai
*
* Copyright 2019-2020 <seil0@mosad.xyz>
2018-11-06 16:17:41 +01: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.
*
*/
2018-11-06 16:17:41 +01:00
package org.mosad.seil0.projectlaogai.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import androidx.fragment.app.Fragment
2019-04-03 20:37:17 +02:00
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.getActionButton
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_timetable.*
import kotlinx.coroutines.*
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.PreferencesController
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController.Companion.timetable
2020-06-11 21:51:46 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.AddSubjectDialog
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.util.NotRetardedCalendar
/**
* The timetable controller class
* contains all needed parts to display and the timetable detail screen
*/
class TimeTableFragment : Fragment() {
private lateinit var scrollViewTimetable: ScrollView
2020-06-11 21:51:46 +02:00
private lateinit var faBtnAddSubject: FloatingActionButton
2019-04-03 20:37:17 +02:00
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2019-03-20 17:55:30 +01:00
val view: View = inflater.inflate(R.layout.fragment_timetable, container, false)
scrollViewTimetable = view.findViewById(R.id.scrollView_Timetable)
2020-06-11 21:51:46 +02:00
faBtnAddSubject = view.findViewById(R.id.faBtnAddSubject)
2019-04-06 14:13:01 +02:00
// init actions
initActions()
2019-04-06 14:13:01 +02:00
if (timetable.size > 1 && timetable[0].days.isNotEmpty() && timetable[1].days.isNotEmpty()) {
2020-06-05 20:56:37 +02:00
initTimetable()
//println(timetable)
} else {
2019-04-03 20:37:17 +02:00
MaterialDialog(context!!)
.title(R.string.error)
.message(R.string.timetable_error)
.show()
}
return view
}
/**
* initialize the actions
*/
2020-06-05 20:56:37 +02:00
private fun initActions() = GlobalScope.launch(Dispatchers.Main) {
2020-06-05 20:56:37 +02:00
refreshLayout_Timetable.setOnRefreshListener {
runBlocking { TimetableController.update(context!!).joinAll() }
reloadTimetableUI()
2020-06-05 20:56:37 +02:00
}
2020-06-05 20:56:37 +02:00
// show the AddLessonDialog if the ftaBtn is clicked
2020-06-11 21:51:46 +02:00
faBtnAddSubject.setOnClickListener {
AddSubjectDialog(context!!).initialize(this@TimeTableFragment).show{
getActionButton(WhichButton.POSITIVE).updateTextColor(PreferencesController.cColorAccent)
getActionButton(WhichButton.NEGATIVE).updateTextColor(PreferencesController.cColorAccent)
}
2020-06-05 20:56:37 +02:00
}
2020-06-05 20:56:37 +02:00
// hide the btnCardValue if the user is scrolling down
scrollViewTimetable.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
if (scrollY > oldScrollY) {
2020-06-11 21:51:46 +02:00
faBtnAddSubject.hide()
2020-06-05 20:56:37 +02:00
} else {
2020-06-11 21:51:46 +02:00
faBtnAddSubject.show()
}
}
}
/**
2019-04-03 20:37:17 +02:00
* add the current and next weeks lessons
*/
2020-06-05 20:56:37 +02:00
private fun initTimetable() = GlobalScope.launch(Dispatchers.Default) {
val currentDayIndex = NotRetardedCalendar.getDayOfWeekIndex()
2020-06-05 20:56:37 +02:00
addTimetableWeek(currentDayIndex, 5, 0).join() // add current week
addTimetableWeek(0, currentDayIndex - 1, 1) // add next week
}
2020-06-05 20:56:37 +02:00
private fun addTimetableWeek(dayBegin: Int, dayEnd: Int, week: Int) = GlobalScope.launch(Dispatchers.Main) {
for (dayIndex in dayBegin..dayEnd) {
val dayCardView = DayCardView(context!!)
2018-11-06 16:17:41 +01:00
// some wired calendar magic, calculate the correct date to be shown
// ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
val daysToAdd = ((timetable[week].weekNumberYear - NotRetardedCalendar.getWeekOfYear())
* 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex()))
dayCardView.addTimetableDay(timetable[week].days[dayIndex], daysToAdd)
2020-06-05 20:56:37 +02:00
// if there are no lessons don't show the dayCardView
if (dayCardView.getLinLayoutDay().childCount > 1)
linLayout_Timetable.addView(dayCardView)
2018-11-06 16:17:41 +01:00
}
2019-04-06 14:13:01 +02:00
}
/**
* clear linLayout_Timetable, add the updated timetable
*/
fun reloadTimetableUI() = GlobalScope.launch(Dispatchers.Default) {
withContext(Dispatchers.Main) {
// remove all lessons from the layout
2019-04-06 14:13:01 +02:00
linLayout_Timetable.removeAllViews()
// add the refreshed timetables
val dayIndex = NotRetardedCalendar.getDayOfWeekIndex()
2019-04-06 14:13:01 +02:00
2020-06-05 20:56:37 +02:00
addTimetableWeek(dayIndex, 5, 0).join() // add current week
addTimetableWeek(0, dayIndex - 1, 1) // add next week
2019-04-06 14:13:01 +02:00
refreshLayout_Timetable.isRefreshing = false
}
}
}