Fixed a bug that would clear the database upon background refresh

This commit is contained in:
Deniz Düzgören
2019-08-07 07:25:06 +02:00
parent 52db4b20bb
commit 78e836b737
5 changed files with 27 additions and 31 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ android {
applicationId "com.denizd.substitutionplan" applicationId "com.denizd.substitutionplan"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 28 targetSdkVersion 28
versionCode 17 versionCode 18
versionName "2.1.0" versionName "2.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
@@ -15,7 +15,6 @@ import androidx.core.content.ContextCompat
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.select.Elements
import java.lang.IndexOutOfBoundsException import java.lang.IndexOutOfBoundsException
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@@ -46,14 +45,13 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
private val foodUrl = "https://djd4rkn355.github.io/food.html" private val foodUrl = "https://djd4rkn355.github.io/food.html"
override fun doInBackground(vararg params: Void?): Void? { override fun doInBackground(vararg params: Void?): Void? {
try { // try {
if (menu) { if (menu) {
val foodViewModel = FoodViewModel(mApplication)
val docFood = Jsoup.connect(foodUrl).get() val docFood = Jsoup.connect(foodUrl).get()
val foodElements = docFood.select("th")
currentFoodTime = docFood.select("h1")[0].text() currentFoodTime = docFood.select("h1")[0].text()
if (currentFoodTime != prefs.getString("timeFood", "")) { if (currentFoodTime != prefs.getString("timeFood", "")) {
val foodViewModel = FoodViewModel(mApplication)
val foodElements = docFood.select("th")
var foodInt = 0 var foodInt = 0
var priority = 0 var priority = 0
foodViewModel.deleteAll() foodViewModel.deleteAll()
@@ -101,10 +99,10 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
} }
if (plan) { if (plan) {
val substViewModel = SubstViewModel(mApplication)
val doc = Jsoup.connect(substUrl).get() val doc = Jsoup.connect(substUrl).get()
currentTime = doc.select("h1")[0].text() currentTime = doc.select("h1")[0].text()
if (currentTime != prefs.getString("time", "")) { if (currentTime != prefs.getString("time", "")) {
val substViewModel = SubstViewModel(mApplication)
val rows = doc.select("tr") val rows = doc.select("tr")
val paragraphs = doc.select("p") val paragraphs = doc.select("p")
@@ -128,7 +126,7 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
for (i in 0 until rows.size) { for (i in 0 until rows.size) {
val row = rows[i] val row = rows[i]
val cols = row.select("th") as Elements val cols = row.select("th")
groupS.add(cols[0].text()) groupS.add(cols[0].text())
dateS.add(cols[1].text()) dateS.add(cols[1].text())
@@ -174,8 +172,8 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
} }
} }
} }
edit.putString("time", currentTime).apply()
} }
if (jobservice && prefs.getBoolean("notif", true)) { if (jobservice && prefs.getBoolean("notif", true)) {
val openApp = Intent(mContext, Main::class.java) val openApp = Intent(mContext, Main::class.java)
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
@@ -209,6 +207,7 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
manager.notify(1, notification) manager.notify(1, notification)
} }
} }
edit.putString("time", currentTime).apply()
} }
} }
@@ -222,12 +221,12 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
Snackbar.make(snackBarView, snackText, Snackbar.LENGTH_LONG).show() Snackbar.make(snackBarView, snackText, Snackbar.LENGTH_LONG).show()
} }
} }
} catch (e: Exception) { // } catch (e: Exception) {
mView?.let { v: View -> // mView?.let { v: View ->
val snackBarView = v.findViewById<View>(R.id.coordination) // val snackBarView = v.findViewById<View>(R.id.coordination)
Snackbar.make(snackBarView, mContext.getString(R.string.noInternet), Snackbar.LENGTH_LONG).setBackgroundTint(ContextCompat.getColor(mContext, R.color.colorError)).show() // Snackbar.make(snackBarView, mContext.getString(R.string.noInternet), Snackbar.LENGTH_LONG).setBackgroundTint(ContextCompat.getColor(mContext, R.color.colorError)).show()
} // }
} // }
return null return null
} }
@@ -48,10 +48,6 @@ class Main : AppCompatActivity(R.layout.app_bar_main) {
edit.putInt("launchDev", prefs.getInt("launchDev", 0) + 1) edit.putInt("launchDev", prefs.getInt("launchDev", 0) + 1)
edit.apply() edit.apply()
if (prefs.getBoolean("notif", true)) {
notificationJob()
}
val appbarlayout = findViewById<AppBarLayout>(R.id.appbarlayout) val appbarlayout = findViewById<AppBarLayout>(R.id.appbarlayout)
val toolbarTxt = findViewById<TextView>(R.id.toolbarTxt) val toolbarTxt = findViewById<TextView>(R.id.toolbarTxt)
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav) val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
@@ -209,6 +205,10 @@ class Main : AppCompatActivity(R.layout.app_bar_main) {
} }
} }
} }
if (prefs.getBoolean("notif", true)) {
notificationJob()
}
} }
} }
@@ -30,18 +30,15 @@ class NotificationService : JobService() {
val networkStatus = connectivityManager.activeNetworkInfo val networkStatus = connectivityManager.activeNetworkInfo
Thread(Runnable { Thread(Runnable {
if (!prefs.getBoolean("notif", false)) { if (!prefs.getBoolean("notif", false) || jobCancelled || networkStatus == null) {
jobFinished(params, false)
return@Runnable return@Runnable
} else { } else {
if (jobCancelled || networkStatus == null) { edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
return@Runnable try {
} DataFetcher(true, true, true, context, application, null).execute()
if (prefs.getBoolean("notif", false)) { } catch (ignored: Exception) {}
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
try {
DataFetcher(true, true, true, context, application, null).execute()
} catch (ignored: Exception) {}
}
jobFinished(params, false) jobFinished(params, false)
} }
}).start() }).start()
@@ -33,7 +33,7 @@ public class SubstRepository(application: Application) {
} }
class InsertSubstAsync(substDao: SubstDao?) : AsyncTask<Subst, Void, Void>() { class InsertSubstAsync(substDao: SubstDao?) : AsyncTask<Subst, Void, Void>() {
val mSubstDao = substDao private val mSubstDao = substDao
override fun doInBackground(vararg substs: Subst): Void? { override fun doInBackground(vararg substs: Subst): Void? {
mSubstDao?.insertSubst(substs[0]) mSubstDao?.insertSubst(substs[0])
return null return null