This is the home of the new hsoApp!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

151 lines
5.8 KiB

/**
* 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.uicomponents
import android.content.Context
import android.os.Build
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
import com.afollestad.materialdialogs.bottomsheets.setPeekHeight
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import kotlinx.coroutines.runBlocking
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.controller.TimetableController
import org.mosad.seil0.projectlaogai.util.Course
import java.util.stream.Collectors
/**
* This class can create a new AddLessonDialog.
*/
class AddSubjectDialog(_context: Context) {
private val context = _context
private lateinit var spinnerCourses: Spinner
private lateinit var spinnerSubjects: Spinner
private val subjectsList = ArrayList<String>()
private val courseNamesList = getCourseNames()
var selectedCourse = ""
var selectedSubject = ""
/**
* create a new AddLessonDialog (BottomSheet)
*/
fun initialize(): MaterialDialog {
val dialog = MaterialDialog(context, BottomSheet())
.title(R.string.add_lesson)
.message(R.string.add_lesson_desc)
.customView(R.layout.dialog_add_lesson)
.setPeekHeight(900)
.positiveButton(R.string.add) {
val lessons = TCoRAPIController.getLessons(selectedCourse, selectedSubject, 0)
println("add lesson \"$selectedCourse: $selectedSubject\"")
println(lessons.toString())
TimetableController.addASubject(selectedCourse, selectedSubject)
// TODO refresh timetable (add a function to show additional lessons)
// TODO save
}
.negativeButton(R.string.cancel)
// initialize the spinners
spinnerCourses = dialog.getCustomView().findViewById(R.id.spinner_Courses)
spinnerSubjects = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
setArrayAdapter(spinnerCourses, courseNamesList)
val lessonsAdapter = setArrayAdapter(spinnerSubjects, subjectsList)
spinnerCourses.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerCourses.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
selectedCourse = parent.getItemAtPosition(pos).toString()
// TODO show loading dialog while loading
val lessonSubjects = runBlocking {
TCoRAPIController.getSubjectListAsync(parent.getItemAtPosition(pos).toString(), 0).await()
}
lessonsAdapter.clear()
lessonsAdapter.addAll(lessonSubjects)
lessonsAdapter.notifyDataSetChanged()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
spinnerSubjects.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerSubjects.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
selectedSubject = parent.getItemAtPosition(pos).toString()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
return dialog
}
/**
* set a new ArrayAdapter for a spinner with a list
* @param spinner the spinner you wish to set the adapter for
* @param list the list to set the adapter to
*/
private fun setArrayAdapter(spinner: Spinner, list: List<String>): ArrayAdapter<String> {
return ArrayAdapter(context, android.R.layout.simple_spinner_item, list)
.also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
/**
* get all course names from coursesList
* @return a list, containing all course names
*/
private fun getCourseNames(): List<String> {
val coursesNameList: List<String>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
coursesNameList = CacheController.coursesList.stream().map(Course::courseName).collect(
Collectors.toList())
} else {
coursesNameList = ArrayList()
CacheController.coursesList.forEach { course ->
coursesNameList.add(course.courseName)
}
}
return coursesNameList
}
}