Archived
Food fragment is now using Kotlin too, but doesn't use DataFetcher.kt
This commit is contained in:
@@ -73,6 +73,8 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
private val newDateFormat = "yyyy-MM-dd, HH:mm:ss"
|
private val newDateFormat = "yyyy-MM-dd, HH:mm:ss"
|
||||||
private var sdf = SimpleDateFormat(oldDateFormat)
|
private var sdf = SimpleDateFormat(oldDateFormat)
|
||||||
private lateinit var d: Date
|
private lateinit var d: Date
|
||||||
|
private val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
||||||
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void?): Void? {
|
override fun doInBackground(vararg params: Void?): Void? {
|
||||||
|
|
||||||
@@ -137,10 +139,22 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
}
|
}
|
||||||
foodInt++
|
foodInt++
|
||||||
mView?.let {
|
mView?.let {
|
||||||
progressBar.incrementProgressBy(1)
|
progressBar.progress = foodInt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mView?.let {
|
||||||
|
progressBar.progress = 100
|
||||||
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
easyPrefs.putListString("foodListPrefs", foodList)
|
easyPrefs.putListString("foodListPrefs", foodList)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,21 +272,15 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
||||||
manager.notify(1, notification) // TODO notification plays multiple times
|
manager.notify(1, notification)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mView?.let { v: View ->
|
mView?.let { v: View ->
|
||||||
val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
|
||||||
|
|
||||||
val handler = Handler(Looper.getMainLooper())
|
|
||||||
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
||||||
|
|
||||||
fadeOut.setAnimationListener(object: Animation.AnimationListener {
|
fadeOut.setAnimationListener(object: Animation.AnimationListener {
|
||||||
override fun onAnimationStart(arg0: Animation) {}
|
override fun onAnimationStart(arg0: Animation) {}
|
||||||
|
|
||||||
override fun onAnimationRepeat(arg0: Animation) {}
|
override fun onAnimationRepeat(arg0: Animation) {}
|
||||||
|
|
||||||
override fun onAnimationEnd(arg0: Animation) {
|
override fun onAnimationEnd(arg0: Animation) {
|
||||||
progressBar.progress = 0
|
progressBar.progress = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,31 +2,42 @@ package com.denizd.substitutionplan
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import android.os.AsyncTask
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.view.animation.Animation
|
||||||
|
import android.view.animation.AnimationUtils
|
||||||
|
import android.widget.ProgressBar
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
import com.madapps.prefrences.EasyPrefrences
|
import com.madapps.prefrences.EasyPrefrences
|
||||||
|
import kotlinx.android.synthetic.main.plan.*
|
||||||
|
import org.jsoup.Jsoup
|
||||||
|
import org.jsoup.nodes.Document
|
||||||
|
import org.jsoup.select.Elements
|
||||||
|
import java.lang.IndexOutOfBoundsException
|
||||||
|
|
||||||
class FoodFragment : Fragment(R.layout.food_layout) {
|
class FoodFragment : Fragment(R.layout.food_layout) {
|
||||||
|
|
||||||
public val foodArrayList = ArrayList<Food>()
|
private val foodArrayList = ArrayList<Food>()
|
||||||
private val mAdapter = FoodAdapter(foodArrayList)
|
private val mAdapter = FoodAdapter(foodArrayList)
|
||||||
private lateinit var mContext: Context
|
private lateinit var mContext: Context
|
||||||
private lateinit var prefs: SharedPreferences
|
private lateinit var prefs: SharedPreferences
|
||||||
// private lateinit var edit: SharedPreferences.Editor
|
private lateinit var edit: SharedPreferences.Editor
|
||||||
private lateinit var easyPrefs: EasyPrefrences
|
private lateinit var easyPrefs: EasyPrefrences
|
||||||
private lateinit var foodListPopulation: ArrayList<String>
|
lateinit var foodListPopulation: ArrayList<String>
|
||||||
private lateinit var recyclerView: RecyclerView
|
lateinit var recyclerView: RecyclerView
|
||||||
|
|
||||||
override fun onAttach(context: Context) {
|
override fun onAttach(context: Context) {
|
||||||
super.onAttach(context)
|
super.onAttach(context)
|
||||||
mContext = context
|
mContext = context
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
||||||
// edit = prefs.edit()
|
edit = prefs.edit()
|
||||||
easyPrefs = EasyPrefrences(mContext)
|
easyPrefs = EasyPrefrences(mContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,13 +65,132 @@ class FoodFragment : Fragment(R.layout.food_layout) {
|
|||||||
|
|
||||||
if (prefs.getBoolean("autoRefresh", false)) {
|
if (prefs.getBoolean("autoRefresh", false)) {
|
||||||
pullToRefresh.isRefreshing = true
|
pullToRefresh.isRefreshing = true
|
||||||
DataFetcher(false, true, false, mContext, activity!!.application, view.rootView).execute()
|
RepopulateFood(mContext, recyclerView, easyPrefs, mAdapter, foodArrayList, pullToRefresh, view.rootView).execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
pullToRefresh.setOnRefreshListener {
|
pullToRefresh.setOnRefreshListener {
|
||||||
pullToRefresh.isRefreshing = true
|
pullToRefresh.isRefreshing = true
|
||||||
DataFetcher(false, true, false, mContext, activity!!.application, view.rootView).execute()
|
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>() {
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
|
override fun doInBackground(vararg params: Void?): Void? {
|
||||||
|
docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get()
|
||||||
|
foodElements = docFood.select("th")
|
||||||
|
|
||||||
|
try {
|
||||||
|
mFoodArrayList.removeAll(mFoodArrayList)
|
||||||
|
} catch (ignored: NullPointerException) {}
|
||||||
|
|
||||||
|
progressBar = mRootView.findViewById(R.id.progressBar)
|
||||||
|
|
||||||
|
progressBar.max = foodElements.size
|
||||||
|
|
||||||
|
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")) {
|
||||||
|
|
||||||
|
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 {
|
||||||
|
foodList.add(foodElements[foodInt].text() + "\n"
|
||||||
|
+ foodElements[foodInt + 1].text() + "\n"
|
||||||
|
+ foodElements[foodInt + 2].text())
|
||||||
|
break
|
||||||
|
} catch (e1: IndexOutOfBoundsException) {
|
||||||
|
foodList.add(foodElements[foodInt].text() + "\n"
|
||||||
|
+ foodElements[foodInt + 1].text())
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foodInt++
|
||||||
|
progressBar.progress = foodInt
|
||||||
|
}
|
||||||
|
|
||||||
|
progressBar.progress = 100
|
||||||
|
|
||||||
|
mEasyPrefs.putListString("foodListPrefs", foodList)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPostExecute(result: Void?) {
|
||||||
|
super.onPostExecute(result)
|
||||||
|
|
||||||
|
try {
|
||||||
|
mRecyclerView.removeAllViews()
|
||||||
|
} catch (ignored: NullPointerException) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
mRecyclerView.removeAllViews()
|
||||||
|
} catch (ignored: NullPointerException) {}
|
||||||
|
|
||||||
|
val foodListPopulation = ArrayList(mEasyPrefs.getListString("foodListPrefs"))
|
||||||
|
|
||||||
|
for (i in 0 until foodListPopulation.size) {
|
||||||
|
mFoodArrayList.add(Food(foodListPopulation[i]))
|
||||||
|
mAdapter.setFood(mFoodArrayList)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -119,11 +119,9 @@ class PlanFragment(isPersonal: Boolean) : Fragment(R.layout.plan) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// recyclerView.adapter!!.notifyDataSetChanged() // TODO check whether this is necessary
|
|
||||||
planCardList.sortWith(Comparator { lhs, rhs -> Integer.compare(rhs.priority, lhs.priority) })
|
planCardList.sortWith(Comparator { lhs, rhs -> Integer.compare(rhs.priority, lhs.priority) })
|
||||||
recyclerView.scheduleLayoutAnimation()
|
recyclerView.scheduleLayoutAnimation()
|
||||||
mAdapter.setSubst(planCardList)
|
mAdapter.setSubst(planCardList)
|
||||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
|
||||||
|
|
||||||
handler.postDelayed({
|
handler.postDelayed({
|
||||||
if (persPlanEmpty) {
|
if (persPlanEmpty) {
|
||||||
@@ -138,8 +136,8 @@ class PlanFragment(isPersonal: Boolean) : Fragment(R.layout.plan) {
|
|||||||
} else {
|
} else {
|
||||||
mAdapter.setSubst(it)
|
mAdapter.setSubst(it)
|
||||||
recyclerView.scheduleLayoutAnimation()
|
recyclerView.scheduleLayoutAnimation()
|
||||||
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
|
||||||
}
|
}
|
||||||
|
bottomSheetText.text = prefs.getString("informational", getString(R.string.noinfo))
|
||||||
})
|
})
|
||||||
|
|
||||||
pullToRefresh.setOnRefreshListener {
|
pullToRefresh.setOnRefreshListener {
|
||||||
|
|||||||
Reference in New Issue
Block a user