2019-09-01 11:54:12 +02:00
|
|
|
package com.denizd.substitutionplan.fragments
|
2019-05-26 11:35:36 +02:00
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import android.content.res.Configuration
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.os.Handler
|
2019-10-12 21:03:34 +02:00
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
|
import android.view.LayoutInflater
|
2019-05-26 11:35:36 +02:00
|
|
|
import android.view.View
|
2019-10-12 21:03:34 +02:00
|
|
|
import android.view.ViewGroup
|
2019-05-26 11:35:36 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-09-24 09:32:25 +02:00
|
|
|
import androidx.lifecycle.LiveData
|
2019-05-26 11:35:36 +02:00
|
|
|
import androidx.lifecycle.ViewModelProviders
|
|
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2019-10-12 21:03:34 +02:00
|
|
|
import com.denizd.substitutionplan.R
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.adapters.SubstitutionAdapter
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.database.SubstViewModel
|
2019-10-12 21:03:34 +02:00
|
|
|
import com.denizd.substitutionplan.databinding.PlanBinding
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.models.Substitution
|
2019-05-26 11:35:36 +02:00
|
|
|
import kotlin.collections.ArrayList
|
|
|
|
|
|
2019-10-12 21:03:34 +02:00
|
|
|
internal open class PlanFragment : Fragment() {
|
2019-09-24 09:32:25 +02:00
|
|
|
internal lateinit var mAdapter: SubstitutionAdapter
|
|
|
|
|
internal var planCardList = ArrayList<Substitution>()
|
|
|
|
|
private lateinit var substViewModel: SubstViewModel
|
2019-08-31 16:56:00 +02:00
|
|
|
private lateinit var mContext: Context
|
2019-09-24 09:32:25 +02:00
|
|
|
internal lateinit var prefs: SharedPreferences
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-09-24 09:32:25 +02:00
|
|
|
internal var isPersonalPlanEmpty: Boolean = true
|
|
|
|
|
internal val handler = Handler()
|
|
|
|
|
internal var substitutionPlan: LiveData<List<Substitution>>? = null
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-10-12 21:03:34 +02:00
|
|
|
internal lateinit var binding: PlanBinding
|
|
|
|
|
|
2019-05-26 11:35:36 +02:00
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
|
super.onAttach(context)
|
|
|
|
|
mContext = context
|
|
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 21:03:34 +02:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
|
binding = PlanBinding.inflate(inflater, container, false)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-26 11:35:36 +02:00
|
|
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
|
|
|
|
super.onConfigurationChanged(newConfig)
|
2019-10-12 21:03:34 +02:00
|
|
|
binding.recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-09-18 11:12:10 +02:00
|
|
|
substViewModel = ViewModelProviders.of(this).get(SubstViewModel::class.java)
|
2019-09-24 09:32:25 +02:00
|
|
|
substitutionPlan = if (prefs.getBoolean("app_specific_sorting", true)) {
|
|
|
|
|
substViewModel.allSubstitutionsSorted
|
|
|
|
|
} else {
|
|
|
|
|
substViewModel.allSubstitutionsOriginal
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-09-23 17:23:59 +02:00
|
|
|
mAdapter = SubstitutionAdapter(planCardList, prefs)
|
2019-10-12 21:03:34 +02:00
|
|
|
binding.recyclerView.apply {
|
|
|
|
|
hasFixedSize()
|
|
|
|
|
layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
|
|
|
|
|
adapter = mAdapter
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-08-31 16:12:36 +02:00
|
|
|
if (prefs.getInt("firstTimeOpening", 0) == 1) {
|
2019-10-12 21:03:34 +02:00
|
|
|
substViewModel.refresh(swipeRefreshLayout = binding.swipeRefreshLayout, rootView = view.rootView, refreshMenu = true)
|
2019-09-01 11:54:12 +02:00
|
|
|
prefs.edit().putInt("firstTimeOpening", 2).apply()
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prefs.getBoolean("autoRefresh", false)) {
|
2019-10-12 21:03:34 +02:00
|
|
|
substViewModel.refresh(swipeRefreshLayout = binding.swipeRefreshLayout, rootView = view.rootView, refreshMenu = false)
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-12 21:03:34 +02:00
|
|
|
binding.swipeRefreshLayout.setOnRefreshListener {
|
|
|
|
|
substViewModel.refresh(swipeRefreshLayout = binding.swipeRefreshLayout, rootView = view.rootView, refreshMenu = false)
|
2019-08-31 16:56:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getGridColumnCount(config: Configuration): Int {
|
|
|
|
|
val tabletSize = resources.getBoolean(R.bool.isTablet)
|
|
|
|
|
return if (tabletSize) {
|
|
|
|
|
when (config.orientation) {
|
|
|
|
|
Configuration.ORIENTATION_PORTRAIT -> 2
|
|
|
|
|
else -> 3
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
when (config.orientation) {
|
|
|
|
|
Configuration.ORIENTATION_PORTRAIT -> 1
|
|
|
|
|
else -> 2
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|