Archived
Plan and personal plan now use the same Kotlin class, food fragment doesnt work properly (yet)
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
package com.denizd.substitutionplan
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.content.res.Configuration
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.preference.PreferenceManager
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class PlanFragment(isPersonal: Boolean) : Fragment(R.layout.plan) {
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
private lateinit var mAdapter: CardAdapter
|
||||
private lateinit var layoutManager: GridLayoutManager
|
||||
private val planCardList = ArrayList<Subst>()
|
||||
private lateinit var bottomSheetText: TextView
|
||||
private lateinit var substViewModel: SubstViewModel
|
||||
private lateinit var mContext: Context
|
||||
private lateinit var prefs: SharedPreferences
|
||||
private lateinit var edit: SharedPreferences.Editor
|
||||
private val personal = isPersonal
|
||||
|
||||
private lateinit var smileydown: TextView
|
||||
private lateinit var smileydowntext: TextView
|
||||
private lateinit var linearsmiley: LinearLayout
|
||||
private var persPlanEmpty: Boolean = true
|
||||
private val handler = Handler()
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
mContext = context
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
||||
edit = prefs.edit()
|
||||
}
|
||||
|
||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
val grid = when (newConfig.orientation) {
|
||||
Configuration.ORIENTATION_PORTRAIT -> 1
|
||||
else -> 2
|
||||
}
|
||||
layoutManager = GridLayoutManager(mContext, grid)
|
||||
recyclerView.layoutManager = layoutManager
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
|
||||
bottomSheetText = view.rootView.findViewById(R.id.bottom_sheet_text)
|
||||
|
||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||
|
||||
recyclerView = view.findViewById(R.id.linearRecycler)
|
||||
recyclerView.hasFixedSize()
|
||||
|
||||
val grid = when (resources.configuration.orientation) {
|
||||
Configuration.ORIENTATION_PORTRAIT -> 1
|
||||
else -> 2
|
||||
}
|
||||
layoutManager = GridLayoutManager(mContext, grid)
|
||||
recyclerView.layoutManager = layoutManager
|
||||
|
||||
mAdapter = CardAdapter(planCardList)
|
||||
recyclerView.adapter = mAdapter
|
||||
|
||||
if (personal) {
|
||||
smileydown = view.findViewById(R.id.smileydown)
|
||||
smileydowntext = view.findViewById(R.id.smileydowntext)
|
||||
linearsmiley = view.findViewById(R.id.linearsmiley)
|
||||
}
|
||||
|
||||
if (prefs.getInt("firstTimeOpening", 0) == 2) {
|
||||
if (!prefs.getBoolean("notif", false)) {
|
||||
pullToRefresh.isRefreshing = true
|
||||
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
|
||||
edit.putInt("firstTimeOpening", 3).apply()
|
||||
}
|
||||
}
|
||||
|
||||
if (prefs.getBoolean("autoRefresh", false)) {
|
||||
pullToRefresh.isRefreshing = true
|
||||
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
|
||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||
}
|
||||
substViewModel = ViewModelProviders.of(activity!!).get(SubstViewModel::class.java)
|
||||
substViewModel.allSubst.observe(this, Observer<List<Subst>> {
|
||||
if (personal) {
|
||||
planCardList.clear()
|
||||
smileydown.visibility = View.GONE
|
||||
smileydowntext.visibility = View.GONE
|
||||
linearsmiley.visibility = View.GONE
|
||||
persPlanEmpty = true
|
||||
recyclerView.visibility = View.VISIBLE
|
||||
for (i in 0 until it.size) {
|
||||
if (prefs.getString("courses", "").isEmpty() && prefs.getString("classes", "").isNotEmpty()) {
|
||||
if (it[i].group.toString().isNotEmpty() && !it[i].group.equals("")) {
|
||||
if (prefs.getString("classes", "").contains(it[i].group.toString()) || it[i].group.toString().contains(prefs.getString("classes", "").toString())) {
|
||||
planCardList.add(it[i])
|
||||
persPlanEmpty = false
|
||||
}
|
||||
}
|
||||
} else if (prefs.getString("classes", "").isNotEmpty() && prefs.getString("courses", "").isNotEmpty()) {
|
||||
if (!it[i].group.equals("") && !it[i].course.equals("")) {
|
||||
if (prefs.getString("courses", "").contains(it[i].course.toString())) {
|
||||
if (prefs.getString("classes", "").contains(it[i].group.toString()) || it[i].group.toString().contains(prefs.getString("classes", "").toString())) {
|
||||
planCardList.add(it[i])
|
||||
persPlanEmpty = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// recyclerView.adapter!!.notifyDataSetChanged() // TODO check whether this is necessary
|
||||
planCardList.sortWith(Comparator { lhs, rhs -> Integer.compare(rhs.priority, lhs.priority) })
|
||||
recyclerView.scheduleLayoutAnimation()
|
||||
mAdapter.setSubst(planCardList)
|
||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||
|
||||
handler.postDelayed({
|
||||
if (persPlanEmpty) {
|
||||
recyclerView.visibility = View.GONE
|
||||
smileydown.visibility = View.VISIBLE
|
||||
smileydowntext.visibility = View.VISIBLE
|
||||
linearsmiley.visibility = View.VISIBLE
|
||||
linearsmiley.scheduleLayoutAnimation()
|
||||
}
|
||||
}, 64)
|
||||
}
|
||||
} else {
|
||||
mAdapter.setSubst(it)
|
||||
recyclerView.scheduleLayoutAnimation()
|
||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||
}
|
||||
})
|
||||
|
||||
pullToRefresh.setOnRefreshListener {
|
||||
pullToRefresh.isRefreshing = true
|
||||
DataFetcher(true, false, false, mContext, activity!!.application, view.rootView).execute()
|
||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user