Archived
"Last updated" time is now compared to a string embedded in the HTML instead of the "Last-Updated" header field to prevent duplicate notifications when the food menu refreshes
This commit is contained in:
@@ -33,7 +33,6 @@ class ColourAdapter(private var mColours: List<Colour>, onClickListener: OnClick
|
||||
|
||||
override fun onBindViewHolder(holder: ColourViewHolder, position: Int) {
|
||||
val currentItem = mColours[position]
|
||||
val prefs = PreferenceManager.getDefaultSharedPreferences(holder.image.context)
|
||||
|
||||
holder.title.text = currentItem.title
|
||||
holder.titleNoLang.text = currentItem.titleNoLang
|
||||
@@ -54,6 +53,6 @@ class ColourAdapter(private var mColours: List<Colour>, onClickListener: OnClick
|
||||
}
|
||||
|
||||
public interface OnClickListener {
|
||||
fun onClick(position: Int, title: String, titleNolang: String)
|
||||
fun onClick(position: Int, title: String, titleNoLang: String)
|
||||
}
|
||||
}
|
||||
@@ -49,12 +49,9 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
private val edit = prefs.edit()
|
||||
private lateinit var pullToRefresh: SwipeRefreshLayout
|
||||
private lateinit var progressBar: ProgressBar
|
||||
private val oldDateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
|
||||
private val newDateFormat = "yyyy-MM-dd, HH:mm:ss"
|
||||
private var sdf = SimpleDateFormat(oldDateFormat)
|
||||
private lateinit var d: Date
|
||||
private val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
var currentTime = ""
|
||||
|
||||
override fun doInBackground(vararg params: Void?): Void? {
|
||||
try {
|
||||
@@ -142,9 +139,8 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
if (plan) {
|
||||
val substViewModel = SubstViewModel(mApplication)
|
||||
val doc = Jsoup.connect("https://djd4rkn355.github.io/subst_test").get()
|
||||
val connection = URL("https://djd4rkn355.github.io/subst").openConnection()
|
||||
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
||||
d = sdf.parse(connection.getHeaderField("Last-Modified"))
|
||||
currentTime = doc.select("h1").text()
|
||||
if (currentTime != prefs.getString("time", "")) {
|
||||
val rows = doc.select("tr")
|
||||
val paragraphs = doc.select("p")
|
||||
|
||||
@@ -192,20 +188,28 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
priority--
|
||||
substViewModel.insertSubst(subst)
|
||||
|
||||
if (jobservice) {
|
||||
if ((prefs.getString("courses", "") ?: "").isEmpty() && (prefs.getString("classes", "") ?: "").isNotEmpty()) {
|
||||
if (jobservice && prefs.getBoolean("notif", true)) {
|
||||
if ((prefs.getString("courses", "")
|
||||
?: "").isEmpty() && (prefs.getString("classes", "")
|
||||
?: "").isNotEmpty()) {
|
||||
if (groupS[i].isNotEmpty() && groupS[i] != "") {
|
||||
if ((prefs.getString("classes", "") ?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "") ?: "").toString())) {
|
||||
if ((prefs.getString("classes", "")
|
||||
?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "")
|
||||
?: "").toString())) {
|
||||
if (notifText.isNotEmpty()) {
|
||||
notifText += ", "
|
||||
}
|
||||
notifText += courseS[i] + ": " + additionalS[i]
|
||||
}
|
||||
}
|
||||
} else if ((prefs.getString("classes", "") ?: "").isNotEmpty() && (prefs.getString("courses", "") ?: "").isNotEmpty()) {
|
||||
} else if ((prefs.getString("classes", "")
|
||||
?: "").isNotEmpty() && (prefs.getString("courses", "")
|
||||
?: "").isNotEmpty()) {
|
||||
if (groupS[i] != "" && courseS[i] != "") {
|
||||
if ((prefs.getString("courses", "") ?: "").contains(courseS[i])) {
|
||||
if ((prefs.getString("classes", "") ?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "") ?: "").toString())) {
|
||||
if ((prefs.getString("classes", "")
|
||||
?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "")
|
||||
?: "").toString())) {
|
||||
if (notifText.isNotEmpty()) {
|
||||
notifText += ", "
|
||||
}
|
||||
@@ -215,8 +219,9 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
}
|
||||
}
|
||||
}
|
||||
edit.putString("time", currentTime).apply()
|
||||
}
|
||||
if (jobservice) {
|
||||
if (jobservice && prefs.getBoolean("notif", true)) {
|
||||
val openApp = Intent(mContext, Main::class.java)
|
||||
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
@@ -250,6 +255,7 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (plan || menu) {
|
||||
mView?.let { v: View ->
|
||||
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
||||
@@ -263,9 +269,8 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
||||
pullToRefresh.isRefreshing = false
|
||||
if (plan) {
|
||||
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()
|
||||
val lastUpdated = sb.append(mContext.getText(R.string.lastupdatedK)).append(currentTime)
|
||||
Snackbar.make(snackBarView, lastUpdated, Snackbar.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.app.job.JobService
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.preference.PreferenceManager
|
||||
import org.jsoup.Jsoup
|
||||
import java.net.URL
|
||||
|
||||
class NotificationService : JobService() {
|
||||
@@ -29,21 +30,20 @@ class NotificationService : JobService() {
|
||||
val networkStatus = connectivityManager.activeNetworkInfo
|
||||
|
||||
Thread(Runnable {
|
||||
if (!prefs.getBoolean("notif", false)) {
|
||||
return@Runnable
|
||||
} else {
|
||||
if (jobCancelled || networkStatus == null) {
|
||||
return@Runnable
|
||||
}
|
||||
if (prefs.getBoolean("notif", false)) {
|
||||
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
|
||||
try {
|
||||
val url = URL("https://djd4rkn355.github.io/subst.html")
|
||||
val connection = url.openConnection()
|
||||
if (connection.getHeaderField("Last-Modified") != prefs.getString("time", "")) {
|
||||
DataFetcher(true, true, true, context, application, null).execute()
|
||||
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
||||
}
|
||||
} catch (ignored: Exception) {}
|
||||
}
|
||||
jobFinished(params, false)
|
||||
}
|
||||
}).start()
|
||||
}
|
||||
}
|
||||
@@ -350,7 +350,7 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
|
||||
dialogView.findViewById<TextView>(R.id.dialogtext).text = getString(R.string.fortest)
|
||||
dialogButton.setOnClickListener {
|
||||
when (dialogEditText.text.toString()) {
|
||||
"@DIAGNOSTICS" -> {
|
||||
"_DIAGNOSTICS" -> {
|
||||
val alertDialogDev = AlertDialog.Builder(mContext, R.style.AlertDialog)
|
||||
val devDialogView = LayoutInflater.from(mContext).inflate(R.layout.diagnostics_dialog, null)
|
||||
val devDialogText = devDialogView.findViewById<TextView>(R.id.dialogtext)
|
||||
@@ -382,11 +382,11 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
|
||||
Toast.makeText(mContext, R.string.noyoutube, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
"@NOTIFICATION" -> {
|
||||
"_NOTIFICATION" -> {
|
||||
edit.putString("time", "").apply()
|
||||
Toast.makeText(mContext, "Notification time cleared", Toast.LENGTH_LONG).show()
|
||||
} // TODO add option to clear database
|
||||
"@FIRSTTIME" -> {
|
||||
"_FIRSTTIME" -> {
|
||||
edit.putBoolean("firstTime", true).apply()
|
||||
Toast.makeText(mContext, "First time flag cleared", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user