From 8e3281db9579f97f84fa69cad5775398cdc2391b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20D=C3=BCzg=C3=B6ren?= Date: Thu, 23 May 2019 16:26:12 +0200 Subject: [PATCH] AsyncTask kotlin class "DataFetcher" is done --- .../denizd/substitutionplan/DataFetcher.kt | 351 +++++++++++------- 1 file changed, 208 insertions(+), 143 deletions(-) diff --git a/app/src/main/java/com/denizd/substitutionplan/DataFetcher.kt b/app/src/main/java/com/denizd/substitutionplan/DataFetcher.kt index 8a8ed2a..20cdc31 100644 --- a/app/src/main/java/com/denizd/substitutionplan/DataFetcher.kt +++ b/app/src/main/java/com/denizd/substitutionplan/DataFetcher.kt @@ -8,10 +8,17 @@ import android.graphics.Color import android.media.RingtoneManager import android.os.AsyncTask import android.os.Build +import android.os.Handler +import android.os.Looper import android.preference.PreferenceManager import android.view.View +import android.view.animation.Animation +import android.view.animation.AnimationUtils +import android.widget.ProgressBar import android.widget.RemoteViews import androidx.core.app.NotificationCompat +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import com.google.android.material.snackbar.Snackbar import com.madapps.prefrences.EasyPrefrences import org.jsoup.Jsoup import org.jsoup.nodes.Document @@ -20,8 +27,11 @@ import java.lang.IndexOutOfBoundsException import java.lang.NullPointerException import java.net.URL 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() { +class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, context: Context, application: Application, parentview: View?) : AsyncTask() { var jobservice = isjobservice var plan = isplan @@ -36,19 +46,17 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte var mContext = context var mApplication = application - var mView = view + var mView = parentview var priority = 200 var notifText = "" var informational = "" var foodList = ArrayList() - var successfulAttempt = false lateinit var connection: URLConnection lateinit var rows: Elements lateinit var paragraphs: Elements lateinit var foodElements: Elements lateinit var doc: Document lateinit var docFood: Document - var modified = "" var manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channelId = "general" var channelName = mContext.getString(R.string.general) @@ -57,116 +65,151 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte lateinit var notification: Notification val prefs = PreferenceManager.getDefaultSharedPreferences(mContext) as SharedPreferences val edit = prefs.edit() as SharedPreferences.Editor + lateinit var pullToRefresh: SwipeRefreshLayout + lateinit var progressBar: ProgressBar val easyPrefs = EasyPrefrences(mContext) 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? { try { - doc = Jsoup.connect("https://djd4rkn355.github.io/subst").get() - connection = URL("https://djd4rkn355.github.io/subst").openConnection() - modified = connection.getHeaderField("Last-Modified") - rows = doc.select("tr") - paragraphs = doc.select("p") - - var groupS = arrayOfNulls(rows.size) - var dateS = arrayOfNulls(rows.size) - var timeS = arrayOfNulls(rows.size) - var courseS = arrayOfNulls(rows.size) - var roomS = arrayOfNulls(rows.size) - var additionalS = arrayOfNulls(rows.size) - - docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get() - foodElements = docFood.select("th") - - successfulAttempt = true - - for (i in 0 until paragraphs.size) { - if (i == 0) { - informational = paragraphs[i].text() - } else { - informational += "\n\n" + paragraphs[i].text() - } + mView?.let { v: View -> + pullToRefresh = v.findViewById(R.id.pullToRefresh) + progressBar = v.findViewById(R.id.progressBar) + progressBar.progress = 0 } - edit.putString("informational", informational).apply() + if (menu) { + docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get() + foodElements = docFood.select("th") - if (successfulAttempt) { - if (menu) { - 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")) { + mView?.let { + progressBar.max = foodElements.size + } - 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 - } + 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")) { - } else { - foodList.add(foodElements[foodInt].text()) - } - } catch (e: IndexOutOfBoundsException) { - try { + 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()) - break - } catch (e1: IndexOutOfBoundsException) { + 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()) - break + foodInt += 1 } - } - foodInt++ - } - easyPrefs.putListString("foodListPrefs", foodList) + } 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++ + mView?.let { + progressBar.incrementProgressBy(1) + } } - if (plan) { - substViewModel.deleteAllSubst() + easyPrefs.putListString("foodListPrefs", foodList) + } - for (i in 0 until rows.size) { - val row = rows[i] - val cols = row.select("th") as Elements + 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") - groupS[i] = cols[0].text() - dateS[i] = cols[1].text() - timeS[i] = cols[2].text() - courseS[i] = cols[3].text() - roomS[i] = cols[4].text() - additionalS[i] = cols[5].text() + val groupS = arrayOfNulls(rows.size) + val dateS = arrayOfNulls(rows.size) + val timeS = arrayOfNulls(rows.size) + val courseS = arrayOfNulls(rows.size) + val roomS = arrayOfNulls(rows.size) + val additionalS = arrayOfNulls(rows.size) - val md = MiscData() - val drawable = md.getIcon(courseS[i].toString()) - val subst = Subst(drawable, groupS[i].toString(), dateS[i].toString(), timeS[i].toString(), courseS[i].toString(), - roomS[i].toString(), additionalS[i].toString(), priority) - priority-- - substViewModel.insert(subst) + mView?.let { + progressBar.max = rows.size + } - if (jobservice) { - if (prefs.getString("courses", "").isEmpty() && prefs.getString("classes", "").isNotEmpty()) { - if (groupS[i].toString().isNotEmpty() && !groupS[i].equals("")) { + 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() + + for (i in 0 until rows.size) { + val row = rows[i] + val cols = row.select("th") as Elements + + groupS[i] = cols[0].text() + dateS[i] = cols[1].text() + timeS[i] = cols[2].text() + courseS[i] = cols[3].text() + roomS[i] = cols[4].text() + additionalS[i] = cols[5].text() + if (!jobservice) { + mView?.let { + progressBar.incrementProgressBy(1) + } + } + + val md = MiscData() + val drawable = md.getIcon(courseS[i].toString()) + val subst = Subst(drawable, groupS[i].toString(), dateS[i].toString(), timeS[i].toString(), courseS[i].toString(), + roomS[i].toString(), additionalS[i].toString(), priority) + priority-- + substViewModel.insert(subst) + + if (jobservice) { + if (prefs.getString("courses", "").isEmpty() && prefs.getString("classes", "").isNotEmpty()) { + if (groupS[i].toString().isNotEmpty() && !groupS[i].equals("")) { + if (prefs.getString("classes", "").contains(groupS[i].toString()) || groupS[i].toString().contains(prefs.getString("classes", "").toString())) { + if (notifText.isNotEmpty()) { + notifText += ", " + } + notifText += courseS[i] + ": " + additionalS[i] + } + } + } else if (prefs.getString("classes", "").isNotEmpty() && prefs.getString("courses", "").isNotEmpty()) { + if (!groupS[i].equals("") && !courseS[i].equals("")) { + if (prefs.getString("courses", "").contains(courseS[i].toString())) { if (prefs.getString("classes", "").contains(groupS[i].toString()) || groupS[i].toString().contains(prefs.getString("classes", "").toString())) { if (notifText.isNotEmpty()) { notifText += ", " @@ -174,63 +217,85 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte notifText += courseS[i] + ": " + additionalS[i] } } - } else if (prefs.getString("classes", "").isNotEmpty() && prefs.getString("courses", "").isNotEmpty()) { - if (!groupS[i].equals("") && !courseS[i].equals("")) { - if (prefs.getString("courses", "").contains(courseS[i].toString())) { - if (prefs.getString("classes", "").contains(groupS[i].toString()) || groupS[i].toString().contains(prefs.getString("classes", "").toString())) { - if (notifText.isNotEmpty()) { - notifText += ", " - } - notifText += courseS[i] + ": " + additionalS[i] - } - } - } } } } - if (jobservice) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - channel = NotificationChannel(channelId, channelName, importance) - channel.enableLights(true) - channel.lightColor = Color.BLUE - manager.createNotificationChannel(channel) - } - - val openApp = Intent(mContext, Main::class.java) - openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK - openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK - val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0) // PendingIntent.FLAG_ONE_SHOT - - val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification) - notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.subst)) - notificationLayout.setTextViewText(R.id.notification_textview, notifText) - - if (notifText.isNotEmpty()) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notification = NotificationCompat.Builder(mContext) // TODO switch out the deprecated notification delivery method - .setStyle(NotificationCompat.DecoratedCustomViewStyle()) - .setCustomContentView(notificationLayout) - .setSmallIcon(R.drawable.ic_avh) - .setChannelId(channelId) - .setContentIntent(openAppPending) - .setAutoCancel(true) - .build() - } else { - notification = NotificationCompat.Builder(mContext) - .setStyle(NotificationCompat.DecoratedCustomViewStyle()) - .setCustomContentView(notificationLayout) - .setSmallIcon(R.drawable.ic_avh) - .setContentIntent(openAppPending) - .setAutoCancel(true) - .build() - } - notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) - manager.notify(1, notification) // TODO notification plays multiple times - } - } } + if (jobservice) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + channel = NotificationChannel(channelId, channelName, importance) + channel.enableLights(true) + channel.lightColor = Color.BLUE + manager.createNotificationChannel(channel) + } + + val openApp = Intent(mContext, Main::class.java) + openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK + openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK + val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0) // PendingIntent.FLAG_ONE_SHOT + + val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification) + notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.subst)) + notificationLayout.setTextViewText(R.id.notification_textview, notifText) + + if (notifText.isNotEmpty()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + notification = NotificationCompat.Builder(mContext) // TODO switch out the deprecated notification delivery method + .setStyle(NotificationCompat.DecoratedCustomViewStyle()) + .setCustomContentView(notificationLayout) + .setSmallIcon(R.drawable.ic_avh) + .setChannelId(channelId) + .setContentIntent(openAppPending) + .setAutoCancel(true) + .build() + } else { + notification = NotificationCompat.Builder(mContext) + .setStyle(NotificationCompat.DecoratedCustomViewStyle()) + .setCustomContentView(notificationLayout) + .setSmallIcon(R.drawable.ic_avh) + .setContentIntent(openAppPending) + .setAutoCancel(true) + .build() + } + notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) + 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(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) {} + + } catch (e: NullPointerException) { + mView?.let { v: View -> + pullToRefresh.isRefreshing = false + val snackbarview = v.findViewById(R.id.coordination) + Snackbar.make(snackbarview, mContext.getText(R.string.nointernet), Snackbar.LENGTH_LONG) + .setAction("Action", null).show() + } + } return null } } \ No newline at end of file