Archived
AsyncTask kotlin class "DataFetcher" is done
This commit is contained in:
@@ -8,10 +8,17 @@ import android.graphics.Color
|
|||||||
import android.media.RingtoneManager
|
import android.media.RingtoneManager
|
||||||
import android.os.AsyncTask
|
import android.os.AsyncTask
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
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 android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import com.madapps.prefrences.EasyPrefrences
|
import com.madapps.prefrences.EasyPrefrences
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
import org.jsoup.nodes.Document
|
import org.jsoup.nodes.Document
|
||||||
@@ -20,8 +27,11 @@ import java.lang.IndexOutOfBoundsException
|
|||||||
import java.lang.NullPointerException
|
import java.lang.NullPointerException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLConnection
|
import java.net.URLConnection
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, context: Context, application: Application, view: View) : AsyncTask<Void, Void, Void>() {
|
class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, context: Context, application: Application, parentview: View?) : AsyncTask<Void, Void, Void>() {
|
||||||
|
|
||||||
var jobservice = isjobservice
|
var jobservice = isjobservice
|
||||||
var plan = isplan
|
var plan = isplan
|
||||||
@@ -36,19 +46,17 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
|
|
||||||
var mContext = context
|
var mContext = context
|
||||||
var mApplication = application
|
var mApplication = application
|
||||||
var mView = view
|
var mView = parentview
|
||||||
var priority = 200
|
var priority = 200
|
||||||
var notifText = ""
|
var notifText = ""
|
||||||
var informational = ""
|
var informational = ""
|
||||||
var foodList = ArrayList<String>()
|
var foodList = ArrayList<String>()
|
||||||
var successfulAttempt = false
|
|
||||||
lateinit var connection: URLConnection
|
lateinit var connection: URLConnection
|
||||||
lateinit var rows: Elements
|
lateinit var rows: Elements
|
||||||
lateinit var paragraphs: Elements
|
lateinit var paragraphs: Elements
|
||||||
lateinit var foodElements: Elements
|
lateinit var foodElements: Elements
|
||||||
lateinit var doc: Document
|
lateinit var doc: Document
|
||||||
lateinit var docFood: Document
|
lateinit var docFood: Document
|
||||||
var modified = ""
|
|
||||||
var manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
var manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
val channelId = "general"
|
val channelId = "general"
|
||||||
var channelName = mContext.getString(R.string.general)
|
var channelName = mContext.getString(R.string.general)
|
||||||
@@ -57,41 +65,31 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
lateinit var notification: Notification
|
lateinit var notification: Notification
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(mContext) as SharedPreferences
|
val prefs = PreferenceManager.getDefaultSharedPreferences(mContext) as SharedPreferences
|
||||||
val edit = prefs.edit() as SharedPreferences.Editor
|
val edit = prefs.edit() as SharedPreferences.Editor
|
||||||
|
lateinit var pullToRefresh: SwipeRefreshLayout
|
||||||
|
lateinit var progressBar: ProgressBar
|
||||||
val easyPrefs = EasyPrefrences(mContext)
|
val easyPrefs = EasyPrefrences(mContext)
|
||||||
val substViewModel = SubstViewModel(mApplication)
|
val substViewModel = SubstViewModel(mApplication)
|
||||||
|
val oldDateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
|
||||||
|
val newDateFormat = "yyyy-MM-dd, HH:mm:ss"
|
||||||
|
var sdf = SimpleDateFormat(oldDateFormat)
|
||||||
|
lateinit var d: Date
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void?): Void? {
|
override fun doInBackground(vararg params: Void?): Void? {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
doc = Jsoup.connect("https://djd4rkn355.github.io/subst").get()
|
mView?.let { v: View ->
|
||||||
connection = URL("https://djd4rkn355.github.io/subst").openConnection()
|
pullToRefresh = v.findViewById(R.id.pullToRefresh)
|
||||||
modified = connection.getHeaderField("Last-Modified")
|
progressBar = v.findViewById(R.id.progressBar)
|
||||||
rows = doc.select("tr")
|
progressBar.progress = 0
|
||||||
paragraphs = doc.select("p")
|
}
|
||||||
|
if (menu) {
|
||||||
var groupS = arrayOfNulls<String>(rows.size)
|
|
||||||
var dateS = arrayOfNulls<String>(rows.size)
|
|
||||||
var timeS = arrayOfNulls<String>(rows.size)
|
|
||||||
var courseS = arrayOfNulls<String>(rows.size)
|
|
||||||
var roomS = arrayOfNulls<String>(rows.size)
|
|
||||||
var additionalS = arrayOfNulls<String>(rows.size)
|
|
||||||
|
|
||||||
docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get()
|
docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get()
|
||||||
foodElements = docFood.select("th")
|
foodElements = docFood.select("th")
|
||||||
|
|
||||||
successfulAttempt = true
|
mView?.let {
|
||||||
|
progressBar.max = foodElements.size
|
||||||
for (i in 0 until paragraphs.size) {
|
|
||||||
if (i == 0) {
|
|
||||||
informational = paragraphs[i].text()
|
|
||||||
} else {
|
|
||||||
informational += "\n\n" + paragraphs[i].text()
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
edit.putString("informational", informational).apply()
|
|
||||||
|
|
||||||
if (successfulAttempt) {
|
|
||||||
if (menu) {
|
|
||||||
var foodInt = 0
|
var foodInt = 0
|
||||||
while (foodInt in 0 until foodElements.size) {
|
while (foodInt in 0 until foodElements.size) {
|
||||||
try { // what a mess this is!
|
try { // what a mess this is!
|
||||||
@@ -138,12 +136,42 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
foodInt++
|
foodInt++
|
||||||
|
mView?.let {
|
||||||
|
progressBar.incrementProgressBy(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
easyPrefs.putListString("foodListPrefs", foodList)
|
easyPrefs.putListString("foodListPrefs", foodList)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (plan) {
|
if (plan) {
|
||||||
|
doc = Jsoup.connect("https://djd4rkn355.github.io/subst").get()
|
||||||
|
connection = URL("https://djd4rkn355.github.io/subst").openConnection()
|
||||||
|
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
||||||
|
d = Date(connection.getHeaderField("Last-Modified"))
|
||||||
|
rows = doc.select("tr")
|
||||||
|
paragraphs = doc.select("p")
|
||||||
|
|
||||||
|
val groupS = arrayOfNulls<String>(rows.size)
|
||||||
|
val dateS = arrayOfNulls<String>(rows.size)
|
||||||
|
val timeS = arrayOfNulls<String>(rows.size)
|
||||||
|
val courseS = arrayOfNulls<String>(rows.size)
|
||||||
|
val roomS = arrayOfNulls<String>(rows.size)
|
||||||
|
val additionalS = arrayOfNulls<String>(rows.size)
|
||||||
|
|
||||||
|
mView?.let {
|
||||||
|
progressBar.max = rows.size
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i in 0 until paragraphs.size) {
|
||||||
|
if (i == 0) {
|
||||||
|
informational = paragraphs[i].text()
|
||||||
|
} else {
|
||||||
|
informational += "\n\n" + paragraphs[i].text()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
edit.putString("informational", informational).apply()
|
||||||
|
|
||||||
substViewModel.deleteAllSubst()
|
substViewModel.deleteAllSubst()
|
||||||
|
|
||||||
for (i in 0 until rows.size) {
|
for (i in 0 until rows.size) {
|
||||||
@@ -156,6 +184,11 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
courseS[i] = cols[3].text()
|
courseS[i] = cols[3].text()
|
||||||
roomS[i] = cols[4].text()
|
roomS[i] = cols[4].text()
|
||||||
additionalS[i] = cols[5].text()
|
additionalS[i] = cols[5].text()
|
||||||
|
if (!jobservice) {
|
||||||
|
mView?.let {
|
||||||
|
progressBar.incrementProgressBy(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val md = MiscData()
|
val md = MiscData()
|
||||||
val drawable = md.getIcon(courseS[i].toString())
|
val drawable = md.getIcon(courseS[i].toString())
|
||||||
@@ -228,9 +261,41 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
manager.notify(1, notification) // TODO notification plays multiple times
|
manager.notify(1, notification) // TODO notification plays multiple times
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mView?.let { v: View ->
|
||||||
|
val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
||||||
|
|
||||||
|
val handler = Handler(Looper.getMainLooper())
|
||||||
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
pullToRefresh.isRefreshing = false
|
||||||
|
val snackbarview = v.findViewById<View>(R.id.coordination)
|
||||||
|
sdf.applyPattern(newDateFormat)
|
||||||
|
val sb = StringBuilder()
|
||||||
|
val lastupdated = sb.append(mContext.getText(R.string.lastupdatedK)).append(sdf.format(d)).toString()
|
||||||
|
Snackbar.make(snackbarview, lastupdated, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e: NullPointerException) {
|
||||||
|
mView?.let { v: View ->
|
||||||
|
pullToRefresh.isRefreshing = false
|
||||||
|
val snackbarview = v.findViewById<View>(R.id.coordination)
|
||||||
|
Snackbar.make(snackbarview, mContext.getText(R.string.nointernet), Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: NullPointerException) {}
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user