Skip to content

Commit

Permalink
update timetable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Mar 5, 2022
1 parent 99bac6e commit 24b4872
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions app/src/main/java/com/ethosa/ktc/ui/fragments/TimetableFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ethosa.ktc.ui.fragments

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -27,6 +29,7 @@ import okhttp3.Response
class TimetableFragment : Fragment() {
private var _binding: FragmentTimetableBinding? = null
private lateinit var itemDecoration: RecyclerView.ItemDecoration
private lateinit var preferences: SharedPreferences

private val college = CollegeApi()
private val binding get() = _binding!!
Expand All @@ -46,6 +49,13 @@ class TimetableFragment : Fragment() {
itemDecoration = SpacingItemDecoration(0, 32)
binding.timetable.addItemDecoration(itemDecoration)

// Load state
preferences = requireActivity().getPreferences(Context.MODE_PRIVATE)
state = preferences.getInt("state", 0)
branch = Branch(id = preferences.getInt("branch", 0), "")
group = Group(preferences.getInt("group", 0), preferences.getString("group_title", "")!!)
loadState()

binding.back.setOnClickListener {
when (state) {
1 -> fetchBranches()
Expand All @@ -55,8 +65,6 @@ class TimetableFragment : Fragment() {
binding.back.isEnabled = false
}

fetchBranches()

return binding.root
}

Expand All @@ -68,12 +76,21 @@ class TimetableFragment : Fragment() {
_binding = null
}

private fun loadState() {
when (state) {
0 -> fetchBranches()
1 -> fetchCourses(branch!!.id)
2 -> fetchTimetable(group!!.id)
}
}

/**
* Fetches branches and shows it.
*/
@Suppress("MemberVisibilityCanBePrivate")
fun fetchBranches() {
state = 0
preferences.edit().putInt("state", state).apply()
college.fetchBranches(object : CollegeCallback {
override fun onResponse(call: Call, response: Response) {
if (_binding == null) return
Expand Down Expand Up @@ -109,6 +126,8 @@ class TimetableFragment : Fragment() {
binding.timetableToolbar.visibility = View.VISIBLE
binding.timetableTitle.text = "Курсы"
binding.timetable.adapter = CourseAdapter(this@TimetableFragment, courses)
preferences.edit().putInt("state", state).apply()
preferences.edit().putInt("branch", branch!!.id).apply()
}
}
})
Expand All @@ -131,9 +150,12 @@ class TimetableFragment : Fragment() {

requireActivity().runOnUiThread {
binding.back.isEnabled = true
binding.timetableTitle.text = "${timetable.week_number} неделя"
binding.timetableTitle.text = "${group!!.title}\n${timetable.week_number} неделя"
binding.timetableToolbar.visibility = View.VISIBLE
binding.timetable.adapter = TimetableAdapter(this@TimetableFragment, timetable)
preferences.edit().putInt("state", state).apply()
preferences.edit().putInt("group", group!!.id).apply()
preferences.edit().putString("group_title", group!!.title).apply()
}
}
}, week)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_timetable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 24b4872

Please sign in to comment.