2020-08-14 21:41:43 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2019 - 2020 < 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.fragments
2020-08-16 21:30:18 +02:00
import android.graphics.Rect
2020-08-14 21:41:43 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2020-08-15 18:13:29 +02:00
import android.widget.TextView
2020-08-16 21:30:18 +02:00
import androidx.core.content.res.ResourcesCompat
2020-08-14 21:41:43 +02:00
import androidx.fragment.app.Fragment
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import kotlinx.android.synthetic.main.fragment_grades.*
2020-08-16 21:30:18 +02:00
import kotlinx.coroutines.*
2020-08-14 21:41:43 +02:00
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.QISPOSParser
2020-08-15 18:13:29 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
2020-08-14 21:41:43 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.GradeLinearLayout
2020-08-15 18:13:29 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.LoginDialog
2020-08-14 21:41:43 +02:00
/ * *
2020-08-15 18:13:29 +02:00
* The grades fragment class
* contains all needed parts to display and the grades screen
2020-08-14 21:41:43 +02:00
* /
class GradesFragment : Fragment ( ) {
private lateinit var refreshLayoutGrades : SwipeRefreshLayout
2020-08-16 21:30:18 +02:00
private lateinit var parser : QISPOSParser
2020-08-14 21:41:43 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _grades , container , false )
refreshLayoutGrades = view . findViewById ( R . id . refreshLayout _Grades )
2020-08-14 21:57:09 +02:00
refreshLayoutGrades . isEnabled = false // disable swipe
2020-08-14 21:41:43 +02:00
2020-08-16 21:30:18 +02:00
parser = QISPOSParser ( context !! ) // init the parser
return view
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
if ( checkCredentials ( ) && checkQisposStatus ( ) ) {
2020-08-15 18:13:29 +02:00
GlobalScope . launch ( Dispatchers . Default ) {
2020-08-14 21:41:43 +02:00
addGrades ( )
2020-08-15 18:13:29 +02:00
}
2020-08-14 21:41:43 +02:00
}
}
2020-08-15 18:13:29 +02:00
/ * *
* check if the credentials are set , if not show a login dialog
* /
private fun checkCredentials ( ) : Boolean {
val credentials = EncryptedPreferences . readCredentials ( context !! )
var credentialsPresent = false
// if there is no password set, show the login dialog
if ( credentials . first . isEmpty ( ) || credentials . second . isEmpty ( ) ) {
LoginDialog ( this . context !! )
. positiveButton {
EncryptedPreferences . saveCredentials ( email , password , context )
addGrades ( )
}
. negativeButton {
txtView _Loading . text = resources . getString ( R . string . credentials _missing )
}
. show {
email = EncryptedPreferences . email
password = " "
}
} else {
credentialsPresent = true
}
return credentialsPresent
}
2020-08-16 21:30:18 +02:00
/ * *
* check if qispos is available , if not show an error
* /
private fun checkQisposStatus ( ) : Boolean {
val statusCode = parser . checkQISPOSStatus ( )
val infoText = resources . getString ( when ( statusCode ) {
503 -> R . string . qispos _unavailable
else -> R . string . qispos _generic _error
} )
val img = ResourcesCompat . getDrawable ( resources , R . drawable . ic _error _outline _black _24dp , null ) ?. apply {
bounds = Rect ( 0 , 0 , 75 , 75 )
}
txtView _Loading ?. apply {
text = infoText
setCompoundDrawables ( null , null , null , img )
}
return statusCode == 200
}
2020-08-15 18:13:29 +02:00
// add the grades to the layout, async
2020-08-14 21:41:43 +02:00
private fun addGrades ( ) = GlobalScope . launch ( Dispatchers . Default ) {
withContext ( Dispatchers . Main ) {
refreshLayout _Grades . isRefreshing = true
}
2020-08-15 18:13:29 +02:00
val grades = parser . parseGrades ( )
2020-08-14 21:41:43 +02:00
withContext ( Dispatchers . Main ) {
linLayout _Grades . removeAllViews ( ) // clear layout
}
// for each semester add a new card
grades . forEach { semester ->
2020-08-15 18:13:29 +02:00
val usedSubjects = ArrayList < String > ( )
2020-08-14 21:41:43 +02:00
val semesterCard = DayCardView ( context !! )
semesterCard . setDayHeading ( semester . key )
// for each subject add a new linLayout
semester . value . forEachIndexed { index , subject ->
2020-08-15 18:13:29 +02:00
if ( usedSubjects . contains ( subject . name ) ) {
return @forEachIndexed
}
// get the first sub subjects
val subSubject = semester . value . firstOrNull {
it . name . contains ( subject . name ) && it . name != subject . name
}
// if sub subject is not null, add it to used subjects
subSubject ?. let {
usedSubjects . add ( it . name )
}
2020-08-14 21:41:43 +02:00
val subjectLayout = GradeLinearLayout ( context ) . set {
subjectName = subject . name
grade = subject . grade
2020-08-15 18:13:29 +02:00
subSubjectName = subSubject ?. name . toString ( )
subGrade = subSubject ?. grade . toString ( )
2020-08-14 21:41:43 +02:00
}
2020-08-15 18:13:29 +02:00
// disable sub-subject if not set
if ( subSubject == null )
subjectLayout . disableSubSubject ( )
2020-08-16 21:30:18 +02:00
2020-08-15 18:13:29 +02:00
// disable divider if last element
2020-08-15 18:15:36 +02:00
if ( index == semester . value . lastIndex || semester . value . indexOf ( subSubject ) == semester . value . lastIndex )
2020-08-14 21:41:43 +02:00
subjectLayout . disableDivider ( )
semesterCard . getLinLayoutDay ( ) . addView ( subjectLayout )
}
// without context we can't access the view
withContext ( Dispatchers . Main ) {
linLayout _Grades . addView ( semesterCard )
}
}
2020-08-15 18:13:29 +02:00
2020-08-16 21:30:18 +02:00
2020-08-15 18:13:29 +02:00
val txtViewLegal = TextView ( context ) . apply {
text = resources . getString ( R . string . without _guarantee )
textAlignment = View . TEXT _ALIGNMENT _CENTER
}
// stop refreshing and show legal warning
2020-08-14 21:41:43 +02:00
withContext ( Dispatchers . Main ) {
2020-08-15 18:13:29 +02:00
linLayout _Grades . addView ( txtViewLegal )
2020-08-14 21:41:43 +02:00
refreshLayout _Grades . isRefreshing = false
}
}
}