diff --git a/app/src/main/java/com/denizd/substitutionplan/data/DataFetcher.kt b/app/src/main/java/com/denizd/substitutionplan/data/DataFetcher.kt index a323338..c7f1fac 100644 --- a/app/src/main/java/com/denizd/substitutionplan/data/DataFetcher.kt +++ b/app/src/main/java/com/denizd/substitutionplan/data/DataFetcher.kt @@ -50,7 +50,6 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole private var mContext = WeakReference(context) private var mApplication = application private var mView = WeakReference(parentView) - private var priority = 200 private var notificationText = "" private var informational = "" private val prefs = PreferenceManager.getDefaultSharedPreferences(mContext.get()) @@ -91,6 +90,7 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole } } } catch (e: Exception) { + e.printStackTrace() mView.get()?.let { v: View -> val snackBarView = v.findViewById(R.id.coordination) Snackbar.make(snackBarView, mContext.get()?.getString(R.string.no_internet_connection) ?: "", Snackbar.LENGTH_LONG).setBackgroundTint(ContextCompat.getColor(mContext.get()!!, @@ -169,20 +169,22 @@ internal class DataFetcher(isPlan: Boolean, isMenu: Boolean, isJobService: Boole val row = rows[i] val cols = row.select("th") + val group = cols[0].text() + val date = cols[1].text() val subst = Substitution( - group = cols[0].text(), - date = cols[1].text(), + group = group, + date = date, time = cols[2].text(), course = cols[3].text(), room = cols[4].text(), additional = cols[5].text(), teacher = cols[6].text(), type = cols[7].text(), - priority = priority + priority = HelperFunctions.assignRanking(group, date.substring(0, 3) == "psa"), + date_priority = HelperFunctions.assignDatePriority(date) ) substArray.add(subst) substRepo.insert(subst) - priority-- } var countOfNotificationItems = 0 var countOfMoreNotificationItems = 0 diff --git a/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt b/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt index f626ae4..fb477ce 100644 --- a/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt +++ b/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt @@ -25,6 +25,7 @@ import java.util.* import java.io.File import java.io.FileOutputStream import java.io.OutputStreamWriter +import java.lang.NumberFormatException import javax.xml.parsers.DocumentBuilderFactory /** @@ -309,6 +310,52 @@ internal object HelperFunctions { return false } + /** + * Assigns an integer to a given group used to sort the substitution plan in SQL. + * The ranking is stored as 'priority' in the database + * + * @param group the group string that should be assigned a ranking + * @param isPSA used to determine a PSA, assigns lowest value if true, therefore putting + * the PSA at the top + * + * @return the ranking as an integer + */ + fun assignRanking(group: String, isPSA: Boolean): Int { + val juniors = arrayOf("5", "6", "7", "8", "9") + if (isPSA) return -31 + return try { + when { + checkStringForArray(group.substring(0, 1), juniors) -> -30 + group.substring(0, 1) == "1" || group.substring(0, 1) == "2" -> { + val a = group.substring(1, 2).toInt() + a - (a * 2) + } + else -> -29 + } + } catch (e: StringIndexOutOfBoundsException) { + 0 + } + } + + /** + * Assigns a date priority to circumvent the event of the 1.12. being shown before the + * 31.11., essentially + * + * @param date the date to be analysed + * + * @return an integer value to determine the ranking in the database + */ + fun assignDatePriority(date: String): Int { + val periodIndex = date.indexOf('.', 0, true) + return try { + date.substring(periodIndex + 1, date.length - 1).toInt() + } catch (e: StringIndexOutOfBoundsException) { + 0 + } catch (n: NumberFormatException) { + -1 + } + } + /** * The code below is used for saving and restoring user settings. However, it is currently * inaccessible to the end user without entering a code in the hidden settings menu, diff --git a/app/src/main/java/com/denizd/substitutionplan/database/SubstDao.kt b/app/src/main/java/com/denizd/substitutionplan/database/SubstDao.kt index d651461..054d36c 100644 --- a/app/src/main/java/com/denizd/substitutionplan/database/SubstDao.kt +++ b/app/src/main/java/com/denizd/substitutionplan/database/SubstDao.kt @@ -8,8 +8,7 @@ import com.denizd.substitutionplan.models.Substitution @Dao internal interface SubstDao { -// @get:Query("SELECT * FROM subst_table ORDER BY date ASC, `group` ASC, time ASC, priority DESC") - @get:Query("SELECT * FROM subst_table ORDER BY priority DESC") + @get:Query("SELECT * FROM subst_table ORDER BY date_priority ASC, priority ASC, `group` ASC, time ASC") val allSubstitutions: LiveData> @Insert diff --git a/app/src/main/java/com/denizd/substitutionplan/database/SubstDatabase.kt b/app/src/main/java/com/denizd/substitutionplan/database/SubstDatabase.kt index f67e359..0387bec 100644 --- a/app/src/main/java/com/denizd/substitutionplan/database/SubstDatabase.kt +++ b/app/src/main/java/com/denizd/substitutionplan/database/SubstDatabase.kt @@ -9,7 +9,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase import com.denizd.substitutionplan.models.Food import com.denizd.substitutionplan.models.Substitution -@Database(entities = [Substitution::class, Food::class], version = 7, exportSchema = false) +@Database(entities = [Substitution::class, Food::class], version = 8, exportSchema = false) internal abstract class SubstDatabase : RoomDatabase() { abstract fun substDao(): SubstDao @@ -27,6 +27,11 @@ internal abstract class SubstDatabase : RoomDatabase() { database.execSQL("ALTER TABLE subst_table ADD COLUMN type TEXT NOT NULL DEFAULT ''") } } + private val addDatePriorityColumn = object : Migration(7, 8) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE subst_table ADD COLUMN date_priority INTEGER NOT NULL DEFAULT 0") + } + } fun getInstance(context: Context): SubstDatabase? { if (instance == null) { @@ -34,7 +39,7 @@ internal abstract class SubstDatabase : RoomDatabase() { instance = Room.databaseBuilder(context.applicationContext, SubstDatabase::class.java, "subst_database") - .addMigrations(addTeacherColumn, addTypeColumn) + .addMigrations(addTeacherColumn, addTypeColumn, addDatePriorityColumn) .fallbackToDestructiveMigration() .build() } diff --git a/app/src/main/java/com/denizd/substitutionplan/models/Substitution.kt b/app/src/main/java/com/denizd/substitutionplan/models/Substitution.kt index 4778157..530cf27 100644 --- a/app/src/main/java/com/denizd/substitutionplan/models/Substitution.kt +++ b/app/src/main/java/com/denizd/substitutionplan/models/Substitution.kt @@ -13,7 +13,8 @@ internal data class Substitution( val additional: String, val teacher: String, val type: String, - val priority: Int + val priority: Int, + val date_priority: Int ) { @PrimaryKey(autoGenerate = true)