From 2d926858b87a1c32c8a84a8a79579dda00086df9 Mon Sep 17 00:00:00 2001 From: Sujit Kumar <60378235+therealsujitk@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:43:30 +0530 Subject: [PATCH] Store receipt dates as timestamps instead of strings --- .../2.json | 56 +++++++++---------- .../adapters/ExamsItemAdapter.java | 2 +- .../adapters/ReceiptsItemAdapter.java | 5 +- .../fragments/RecyclerViewFragment.java | 16 ------ .../vtopchennai/helpers/AppDatabase.java | 18 +++++- .../vtopchennai/interfaces/ReceiptsDao.java | 2 +- .../vtopchennai/models/Receipt.java | 2 +- .../vtopchennai/services/VTOPService.java | 6 +- 8 files changed, 57 insertions(+), 50 deletions(-) diff --git a/app/schemas/tk.therealsuji.vtopchennai.helpers.AppDatabase/2.json b/app/schemas/tk.therealsuji.vtopchennai.helpers.AppDatabase/2.json index 953af6fb..ef2ffa77 100644 --- a/app/schemas/tk.therealsuji.vtopchennai.helpers.AppDatabase/2.json +++ b/app/schemas/tk.therealsuji.vtopchennai.helpers.AppDatabase/2.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 2, - "identityHash": "49cb6695574b9a1a0cbc27bc5171f77b", + "identityHash": "7b22558aa636907447831dc30868b3a9", "entities": [ { "tableName": "assignments", @@ -46,10 +46,10 @@ } ], "primaryKey": { + "autoGenerate": false, "columnNames": [ "id" - ], - "autoGenerate": false + ] }, "indices": [], "foreignKeys": [] @@ -96,10 +96,10 @@ } ], "primaryKey": { + "autoGenerate": false, "columnNames": [ "id" - ], - "autoGenerate": false + ] }, "indices": [], "foreignKeys": [ @@ -152,10 +152,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [ @@ -220,10 +220,10 @@ } ], "primaryKey": { + "autoGenerate": false, "columnNames": [ "id" - ], - "autoGenerate": false + ] }, "indices": [], "foreignKeys": [] @@ -300,10 +300,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [] @@ -362,10 +362,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [ @@ -454,10 +454,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [ @@ -476,7 +476,7 @@ }, { "tableName": "receipts", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`number` INTEGER NOT NULL, `amount` REAL, `date` TEXT, PRIMARY KEY(`number`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`number` INTEGER NOT NULL, `amount` REAL, `date` INTEGER, PRIMARY KEY(`number`))", "fields": [ { "fieldPath": "number", @@ -493,15 +493,15 @@ { "fieldPath": "date", "columnName": "date", - "affinity": "TEXT", + "affinity": "INTEGER", "notNull": false } ], "primaryKey": { + "autoGenerate": false, "columnNames": [ "number" - ], - "autoGenerate": false + ] }, "indices": [], "foreignKeys": [] @@ -530,10 +530,10 @@ } ], "primaryKey": { + "autoGenerate": false, "columnNames": [ "id" - ], - "autoGenerate": false + ] }, "indices": [], "foreignKeys": [ @@ -592,10 +592,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [] @@ -630,10 +630,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [] @@ -704,10 +704,10 @@ } ], "primaryKey": { + "autoGenerate": true, "columnNames": [ "id" - ], - "autoGenerate": true + ] }, "indices": [], "foreignKeys": [ @@ -794,7 +794,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '49cb6695574b9a1a0cbc27bc5171f77b')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7b22558aa636907447831dc30868b3a9')" ] } } \ No newline at end of file diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ExamsItemAdapter.java b/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ExamsItemAdapter.java index 3c04e4e3..a445e22b 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ExamsItemAdapter.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ExamsItemAdapter.java @@ -96,7 +96,7 @@ public void setExamItem(Exam.AllData examItem) { courseCode.setText(examItem.courseCode); if (examItem.startTime != null) { - date.setText(Html.fromHtml(this.examItem.getContext().getString(R.string.date, dateFormat.format(examItem.startTime).toUpperCase(Locale.ROOT)), Html.FROM_HTML_MODE_LEGACY)); + date.setText(Html.fromHtml(this.examItem.getContext().getString(R.string.date, dateFormat.format(examItem.startTime).toUpperCase(Locale.ENGLISH)), Html.FROM_HTML_MODE_LEGACY)); date.setVisibility(View.VISIBLE); } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ReceiptsItemAdapter.java b/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ReceiptsItemAdapter.java index 524fd3c8..8003549c 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ReceiptsItemAdapter.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/adapters/ReceiptsItemAdapter.java @@ -10,7 +10,9 @@ import androidx.recyclerview.widget.RecyclerView; import java.text.DecimalFormat; +import java.text.SimpleDateFormat; import java.util.List; +import java.util.Locale; import tk.therealsuji.vtopchennai.R; import tk.therealsuji.vtopchennai.models.Receipt; @@ -59,10 +61,11 @@ public void setReceipt(Receipt receipt) { TextView number = this.receipt.findViewById(R.id.text_view_receipt_number); TextView amount = this.receipt.findViewById(R.id.text_view_amount); TextView date = this.receipt.findViewById(R.id.text_view_date); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); number.setText(String.valueOf(receipt.number)); amount.setText(new DecimalFormat("₹ #.00/-").format(receipt.amount)); - date.setText(receipt.date); + date.setText(dateFormat.format(receipt.date).toUpperCase(Locale.ENGLISH)); } } } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/fragments/RecyclerViewFragment.java b/app/src/main/java/tk/therealsuji/vtopchennai/fragments/RecyclerViewFragment.java index 06501c91..0e3c5046 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/fragments/RecyclerViewFragment.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/fragments/RecyclerViewFragment.java @@ -1,7 +1,6 @@ package tk.therealsuji.vtopchennai.fragments; import android.annotation.SuppressLint; -import android.icu.text.SimpleDateFormat; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -13,10 +12,7 @@ import com.google.firebase.analytics.FirebaseAnalytics; -import java.util.Collections; -import java.util.Comparator; import java.util.List; -import java.util.Locale; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.annotations.NonNull; @@ -65,18 +61,6 @@ public void onSuccess(@NonNull List receipts) { return; } - Collections.sort(receipts, new Comparator() { - @Override - public int compare(Receipt r1, Receipt r2) { - SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); - try { - return format.parse(r2.date).compareTo(format.parse(r1.date)); - } catch (Exception ignored) { - } - return 0; - } - }); - recyclerView.setAdapter(new ReceiptsItemAdapter(receipts)); } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/helpers/AppDatabase.java b/app/src/main/java/tk/therealsuji/vtopchennai/helpers/AppDatabase.java index 021a62a4..b8d4e96d 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/helpers/AppDatabase.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/helpers/AppDatabase.java @@ -2,10 +2,13 @@ import android.content.Context; +import androidx.annotation.NonNull; import androidx.room.AutoMigration; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; +import androidx.room.migration.Migration; +import androidx.sqlite.db.SupportSQLiteDatabase; import tk.therealsuji.vtopchennai.interfaces.AssignmentsDao; import tk.therealsuji.vtopchennai.interfaces.AttendanceDao; @@ -44,7 +47,7 @@ Staff.class, Timetable.class }, - version = 2, + version = 3, autoMigrations = { @AutoMigration(from = 1, to = 2), } @@ -56,6 +59,7 @@ public static synchronized AppDatabase getInstance(Context context) { if (instance == null) { instance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "vit_student") + .addMigrations(MIGRATION_2_3) .fallbackToDestructiveMigration() .build(); } @@ -90,4 +94,16 @@ public static synchronized void deleteDatabase(Context context) { public abstract StaffDao staffDao(); public abstract TimetableDao timetableDao(); + + // Manual Migrations + static final Migration MIGRATION_2_3 = new Migration(2, 3) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("ALTER TABLE receipts RENAME TO receipts_old"); + database.execSQL("CREATE TABLE receipts (number INTEGER NOT NULL PRIMARY KEY, amount REAL, date INTEGER)"); + database.execSQL("INSERT INTO receipts (number, amount) SELECT number, amount FROM receipts_old"); + database.execSQL("UPDATE receipts SET date = 0"); + database.execSQL("DROP TABLE receipts_old"); + } + }; } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/interfaces/ReceiptsDao.java b/app/src/main/java/tk/therealsuji/vtopchennai/interfaces/ReceiptsDao.java index 8bf25410..3459b1ec 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/interfaces/ReceiptsDao.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/interfaces/ReceiptsDao.java @@ -18,6 +18,6 @@ public interface ReceiptsDao { @Query("DELETE FROM receipts") Completable deleteAll(); - @Query("SELECT * FROM receipts") + @Query("SELECT * FROM receipts ORDER BY date DESC, number DESC") Single> getReceipts(); } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/models/Receipt.java b/app/src/main/java/tk/therealsuji/vtopchennai/models/Receipt.java index 756958c2..73548709 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/models/Receipt.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/models/Receipt.java @@ -13,5 +13,5 @@ public class Receipt { public Double amount; @ColumnInfo(name = "date") - public String date; + public Long date; } diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/services/VTOPService.java b/app/src/main/java/tk/therealsuji/vtopchennai/services/VTOPService.java index f8cf4bce..2c6f7d39 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/services/VTOPService.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/services/VTOPService.java @@ -2209,15 +2209,19 @@ private void downloadReceipts() { for (int i = 0; i < receiptsArray.length(); ++i) { JSONObject receiptsObject = receiptsArray.getJSONObject(i); Receipt receipt = new Receipt(); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); // If this is true, there's a web scrapping issue if (receiptsObject.isNull("number")) { continue; } + String receiptDateString = this.getStringValue(receiptsObject, "date"); + Date receiptDate = receiptDateString != null ? dateFormat.parse(receiptDateString) : null; + receipt.number = receiptsObject.getInt("number"); receipt.amount = this.getDoubleValue(receiptsObject, "amount"); - receipt.date = this.getStringValue(receiptsObject, "date"); + receipt.date = receiptDate != null ? receiptDate.getTime() : 0; receipts.add(receipt); }