Browse Source

complete & move AddLessonDialog to separate class

pull/46/head
Jannik 11 months ago
parent
commit
bea1b47396
Signed by: Seil0 GPG Key ID: E8459F3723C52C24
  1. 2
      app/build.gradle
  2. 6
      app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt
  3. 81
      app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt
  4. 142
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt

2
app/build.gradle

@ -47,7 +47,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
implementation 'androidx.core:core:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

6
app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt

@ -152,13 +152,11 @@ class TCoRAPIController {
/**
* TODO
*/
fun getLessonSubjectList(courseName: String, week: Int): Deferred<Array<String>> {
fun getLessonSubjectListAsync(courseName: String, week: Int): Deferred<ArrayList<String>> {
val url = URL("$tcorBaseURL/lessonSubjectList?courseName=$courseName&week=$week")
return GlobalScope.async {
println(courseName)
println(url.readText())
Gson().fromJson(url.readText(), Array<String>::class.java)
Gson().fromJson(url.readText(), ArrayList<String>()::class.java)
}
}
}

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

@ -22,36 +22,24 @@
package org.mosad.seil0.projectlaogai.fragments
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.ScrollView
import android.widget.Spinner
import androidx.fragment.app.Fragment
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 com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_timetable.*
import kotlinx.coroutines.*
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.coursesList
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController.Companion.getLessonSubjectList
import org.mosad.seil0.projectlaogai.hsoparser.Course
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.AddLessonDialog
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import java.util.stream.Collectors
/**
* The timetable controller class
@ -94,7 +82,8 @@ class TimeTableFragment : Fragment() {
}
faBtnAddLesson.setOnClickListener {
initAddLessonDialog().show()
//initAddLessonDialog().show()
AddLessonDialog(context!!).initAddLessonDialog().show()
}
// hide the btnCardValue if the user is scrolling down
@ -169,68 +158,4 @@ class TimeTableFragment : Fragment() {
}
}
// TODO move to separate class
private fun initAddLessonDialog(): 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) {
println("add course")
}
.negativeButton(R.string.cancel) { }
val spinnerCourses: Spinner = dialog.getCustomView().findViewById(R.id.spinner_Courses)
val spinnerLessons: Spinner = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
val field1List: List<String>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
field1List = coursesList.stream().map(Course::courseName).collect(Collectors.toList())
} else {
field1List = ArrayList()
coursesList.forEach { course ->
field1List.add(course.courseName)
}
}
ArrayAdapter(
context!!,
android.R.layout.simple_spinner_item,
field1List
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerCourses.adapter = adapter
}
// FIXME onItemSelectedListener is called on dialog.show
spinnerCourses.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
println("onItemSelected: ${parent.getItemAtPosition(pos)}")
// TODO get Lessons
val lessonSubjects = getLessonSubjectList(parent.getItemAtPosition(pos).toString(), 0)
println(lessonSubjects)
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
spinnerLessons.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
println(parent.getItemAtPosition(pos))
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
return dialog
}
}

142
app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt

@ -0,0 +1,142 @@
/**
* 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.hsoparser.Course
import java.util.stream.Collectors
/**
* This class can create a new AddLessonDialog.
*/
class AddLessonDialog(_context: Context) {
private val context = _context
private lateinit var spinnerCourses: Spinner
private lateinit var spinnerLessons: Spinner
private val lessonSubjectsList = ArrayList<String>()
private val courseNamesList = getCourseNames()
var selectedCourse = ""
/**
* create a new AddLessonDialog (BottomSheet)
*/
fun initAddLessonDialog(): 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) {
println("add course \"$selectedCourse\"")
// TODO call add function
}
.negativeButton(R.string.cancel) { }
// initialize the spinners
spinnerCourses = dialog.getCustomView().findViewById(R.id.spinner_Courses)
spinnerLessons = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
setArrayAdapter(spinnerCourses, courseNamesList)
val lessonsAdapter = setArrayAdapter(spinnerLessons, lessonSubjectsList)
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) {
val lessonSubjects = runBlocking {
TCoRAPIController.getLessonSubjectListAsync(parent.getItemAtPosition(pos).toString(), 0).await()
}
lessonsAdapter.clear()
lessonsAdapter.addAll(lessonSubjects)
lessonsAdapter.notifyDataSetChanged()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
spinnerLessons.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerLessons.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
selectedCourse = 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
}
}
Loading…
Cancel
Save