2019-04-29 19:27:51 +02:00
|
|
|
package com.denizd.substitutionplan
|
|
|
|
|
|
2019-05-26 11:35:36 +02:00
|
|
|
import android.content.Context
|
2019-04-30 19:24:13 +02:00
|
|
|
import android.content.SharedPreferences
|
2019-05-26 14:59:00 +02:00
|
|
|
import android.os.AsyncTask
|
2019-04-30 19:24:13 +02:00
|
|
|
import android.os.Bundle
|
2019-05-26 14:59:00 +02:00
|
|
|
import android.os.Handler
|
|
|
|
|
import android.os.Looper
|
2019-04-30 19:24:13 +02:00
|
|
|
import android.preference.PreferenceManager
|
|
|
|
|
import android.view.View
|
2019-05-26 14:59:00 +02:00
|
|
|
import android.view.animation.Animation
|
|
|
|
|
import android.view.animation.AnimationUtils
|
|
|
|
|
import android.widget.ProgressBar
|
2019-04-30 19:24:13 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-05-26 18:40:02 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2019-04-30 19:24:13 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
2019-06-02 17:57:57 +02:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2019-04-30 19:24:13 +02:00
|
|
|
import com.madapps.prefrences.EasyPrefrences
|
2019-05-26 14:59:00 +02:00
|
|
|
import org.jsoup.Jsoup
|
|
|
|
|
import org.jsoup.nodes.Document
|
|
|
|
|
import org.jsoup.select.Elements
|
|
|
|
|
import java.lang.IndexOutOfBoundsException
|
2019-04-30 19:24:13 +02:00
|
|
|
|
|
|
|
|
class FoodFragment : Fragment(R.layout.food_layout) {
|
|
|
|
|
|
2019-05-26 14:59:00 +02:00
|
|
|
private val foodArrayList = ArrayList<Food>()
|
2019-04-30 19:24:13 +02:00
|
|
|
private val mAdapter = FoodAdapter(foodArrayList)
|
2019-05-26 11:35:36 +02:00
|
|
|
private lateinit var mContext: Context
|
|
|
|
|
private lateinit var prefs: SharedPreferences
|
2019-05-26 14:59:00 +02:00
|
|
|
private lateinit var edit: SharedPreferences.Editor
|
2019-05-26 11:35:36 +02:00
|
|
|
private lateinit var easyPrefs: EasyPrefrences
|
2019-05-26 14:59:00 +02:00
|
|
|
lateinit var foodListPopulation: ArrayList<String>
|
|
|
|
|
lateinit var recyclerView: RecyclerView
|
2019-04-30 19:24:13 +02:00
|
|
|
|
2019-05-26 11:35:36 +02:00
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
|
super.onAttach(context)
|
|
|
|
|
mContext = context
|
|
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
2019-05-26 14:59:00 +02:00
|
|
|
edit = prefs.edit()
|
2019-05-26 11:35:36 +02:00
|
|
|
easyPrefs = EasyPrefrences(mContext)
|
|
|
|
|
}
|
2019-04-30 19:24:13 +02:00
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-05-26 11:35:36 +02:00
|
|
|
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
|
|
|
|
|
foodListPopulation = ArrayList(easyPrefs.getListString("foodListPrefs"))
|
2019-04-30 19:24:13 +02:00
|
|
|
|
|
|
|
|
try {
|
2019-05-26 11:35:36 +02:00
|
|
|
recyclerView = view.findViewById(R.id.linear_food)
|
2019-04-30 19:24:13 +02:00
|
|
|
recyclerView.hasFixedSize()
|
2019-05-26 18:40:02 +02:00
|
|
|
recyclerView.layoutManager = GridLayoutManager(mContext, 1) // , RecyclerView.VERTICAL,false
|
2019-04-30 19:24:13 +02:00
|
|
|
recyclerView.adapter = mAdapter
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
recyclerView.removeAllViews()
|
2019-05-26 11:35:36 +02:00
|
|
|
} catch (ignored: NullPointerException) {}
|
2019-04-30 19:24:13 +02:00
|
|
|
|
2019-05-26 11:35:36 +02:00
|
|
|
for (i in 0 until foodListPopulation.size) {
|
2019-04-30 19:24:13 +02:00
|
|
|
foodArrayList.add(Food(foodListPopulation[i]))
|
|
|
|
|
mAdapter.setFood(foodArrayList)
|
|
|
|
|
recyclerView.scheduleLayoutAnimation()
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
} catch (ignored: NullPointerException) {}
|
2019-04-30 19:24:13 +02:00
|
|
|
|
|
|
|
|
if (prefs.getBoolean("autoRefresh", false)) {
|
|
|
|
|
pullToRefresh.isRefreshing = true
|
2019-05-26 14:59:00 +02:00
|
|
|
RepopulateFood(mContext, recyclerView, easyPrefs, mAdapter, foodArrayList, pullToRefresh, view.rootView).execute()
|
2019-04-30 19:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pullToRefresh.setOnRefreshListener {
|
|
|
|
|
pullToRefresh.isRefreshing = true
|
2019-05-26 14:59:00 +02:00
|
|
|
RepopulateFood(mContext, recyclerView, easyPrefs, mAdapter, foodArrayList, pullToRefresh, view.rootView).execute()
|
|
|
|
|
}
|
|
|
|
|
/* bad approach - I should be using the DataFetcher.kt class, but that requires figuring out how to access the RecyclerView and its adapter from
|
|
|
|
|
* another class - the best approach would be to store the data in the database instead of using EasyPreferences, however, I'm not doing that now */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RepopulateFood(context: Context, recyclerView: RecyclerView, easyPrefs: EasyPrefrences,
|
|
|
|
|
adapter: FoodAdapter, foodArrayList: ArrayList<Food>,
|
|
|
|
|
swipeRefreshLayout: SwipeRefreshLayout, rootView: View) : AsyncTask<Void, Void, Void>() {
|
|
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
private val mContext = context
|
2019-05-26 14:59:00 +02:00
|
|
|
private val mRecyclerView = recyclerView
|
|
|
|
|
private val mEasyPrefs = easyPrefs
|
|
|
|
|
private val mAdapter = adapter
|
|
|
|
|
private val mFoodArrayList = foodArrayList
|
|
|
|
|
private var foodList = ArrayList<String>()
|
|
|
|
|
private lateinit var foodElements: Elements
|
|
|
|
|
private lateinit var docFood: Document
|
|
|
|
|
private val pullToRefresh = swipeRefreshLayout
|
|
|
|
|
private lateinit var progressBar: ProgressBar
|
|
|
|
|
val mRootView = rootView
|
|
|
|
|
private val fadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out)
|
|
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
2019-06-09 18:35:36 +02:00
|
|
|
private var exceptionOccurred = false
|
2019-05-26 14:59:00 +02:00
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void?): Void? {
|
2019-06-02 17:57:57 +02:00
|
|
|
try {
|
|
|
|
|
docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get()
|
|
|
|
|
foodElements = docFood.select("th")
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
progressBar = mRootView.findViewById(R.id.progressBar)
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
progressBar.max = foodElements.size
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
var foodInt = 0
|
|
|
|
|
while (foodInt in 0 until foodElements.size) {
|
|
|
|
|
try { // what a mess this is!
|
|
|
|
|
if (foodElements[foodInt].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Freitag")) {
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
if (foodElements[foodInt + 3].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Freitag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("von")) {
|
|
|
|
|
foodList.add(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 2].text())
|
|
|
|
|
foodInt += 2
|
|
|
|
|
} else if (foodElements[foodInt + 2].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Freitag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("von")) {
|
|
|
|
|
foodList.add(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text())
|
|
|
|
|
foodInt += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
foodList.add(foodElements[foodInt].text())
|
|
|
|
|
}
|
|
|
|
|
} catch (e: IndexOutOfBoundsException) {
|
|
|
|
|
try {
|
2019-05-26 14:59:00 +02:00
|
|
|
foodList.add(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 2].text())
|
2019-06-02 17:57:57 +02:00
|
|
|
break
|
|
|
|
|
} catch (e1: IndexOutOfBoundsException) {
|
2019-05-26 14:59:00 +02:00
|
|
|
foodList.add(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text())
|
2019-06-02 17:57:57 +02:00
|
|
|
break
|
2019-05-26 14:59:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-02 17:57:57 +02:00
|
|
|
foodInt++
|
|
|
|
|
progressBar.progress = foodInt
|
2019-05-26 14:59:00 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
progressBar.progress = 100
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
mEasyPrefs.putListString("foodListPrefs", foodList)
|
2019-06-09 18:35:36 +02:00
|
|
|
} catch (e: Exception) { exceptionOccurred = true }
|
2019-05-26 14:59:00 +02:00
|
|
|
return null
|
2019-04-30 19:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-26 14:59:00 +02:00
|
|
|
override fun onPostExecute(result: Void?) {
|
|
|
|
|
super.onPostExecute(result)
|
|
|
|
|
|
2019-06-09 18:35:36 +02:00
|
|
|
if (!exceptionOccurred) {
|
2019-06-02 17:57:57 +02:00
|
|
|
try {
|
|
|
|
|
mRecyclerView.removeAllViews()
|
|
|
|
|
mFoodArrayList.removeAll(mFoodArrayList)
|
|
|
|
|
} catch (ignored: NullPointerException) {}
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
val foodListPopulation = ArrayList(mEasyPrefs.getListString("foodListPrefs"))
|
2019-05-26 14:59:00 +02:00
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
for (i in 0 until foodListPopulation.size) {
|
|
|
|
|
mFoodArrayList.add(Food(foodListPopulation[i]))
|
|
|
|
|
mAdapter.setFood(mFoodArrayList)
|
2019-05-26 14:59:00 +02:00
|
|
|
}
|
2019-06-02 17:57:57 +02:00
|
|
|
|
|
|
|
|
mRecyclerView.scheduleLayoutAnimation()
|
|
|
|
|
pullToRefresh.isRefreshing = false
|
|
|
|
|
|
|
|
|
|
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
|
|
|
|
fadeOut.setAnimationListener(object: Animation.AnimationListener {
|
|
|
|
|
override fun onAnimationStart(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationRepeat(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationEnd(arg0: Animation) {
|
|
|
|
|
progressBar.progress = 0
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
pullToRefresh.isRefreshing = false
|
|
|
|
|
val snackbarview = mRootView.findViewById<View>(R.id.coordination)
|
|
|
|
|
Snackbar.make(snackbarview, mContext.getText(R.string.nointernet), Snackbar.LENGTH_LONG)
|
|
|
|
|
.setAction("Action", null).show()
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-26 14:59:00 +02:00
|
|
|
}
|
2019-04-30 19:24:13 +02:00
|
|
|
}
|
2019-04-29 19:27:51 +02:00
|
|
|
}
|