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
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import kotlinx.android.synthetic.main.fragment_grades.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.QISPOSParser
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.GradeLinearLayout
/ * *
* A simple [ Fragment ] subclass .
* /
class GradesFragment : Fragment ( ) {
private lateinit var refreshLayoutGrades : SwipeRefreshLayout
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
GlobalScope . launch ( Dispatchers . Default ) {
addGrades ( )
}
return view
}
private fun addGrades ( ) = GlobalScope . launch ( Dispatchers . Default ) {
withContext ( Dispatchers . Main ) {
refreshLayout _Grades . isRefreshing = true
}
val parser = QISPOSParser ( )
val grades = parser . parseGrades ( context !! )
withContext ( Dispatchers . Main ) {
linLayout _Grades . removeAllViews ( ) // clear layout
}
// for each semester add a new card
grades . forEach { semester ->
val semesterCard = DayCardView ( context !! )
semesterCard . setDayHeading ( semester . key )
// for each subject add a new linLayout
semester . value . forEachIndexed { index , subject ->
val subjectLayout = GradeLinearLayout ( context ) . set {
subjectName = subject . name
grade = subject . grade
}
if ( index == semester . value . lastIndex )
subjectLayout . disableDivider ( )
semesterCard . getLinLayoutDay ( ) . addView ( subjectLayout )
}
// without context we can't access the view
withContext ( Dispatchers . Main ) {
linLayout _Grades . addView ( semesterCard )
}
}
withContext ( Dispatchers . Main ) {
refreshLayout _Grades . isRefreshing = false
}
//refreshLayoutGrades.isRefreshing = false
}
}