Skip to content

Commit

Permalink
Store receipt dates as timestamps instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
therealsujitk committed Oct 12, 2023
1 parent 94f8723 commit 2d92685
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 50 deletions.
56 changes: 28 additions & 28 deletions app/schemas/tk.therealsuji.vtopchennai.helpers.AppDatabase/2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "49cb6695574b9a1a0cbc27bc5171f77b",
"identityHash": "7b22558aa636907447831dc30868b3a9",
"entities": [
{
"tableName": "assignments",
Expand Down Expand Up @@ -46,10 +46,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -96,10 +96,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": [
Expand Down Expand Up @@ -152,10 +152,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": [
Expand Down Expand Up @@ -220,10 +220,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -300,10 +300,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -362,10 +362,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": [
Expand Down Expand Up @@ -454,10 +454,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": [
Expand All @@ -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",
Expand All @@ -493,15 +493,15 @@
{
"fieldPath": "date",
"columnName": "date",
"affinity": "TEXT",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"number"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -530,10 +530,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": [
Expand Down Expand Up @@ -592,10 +592,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -630,10 +630,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -704,10 +704,10 @@
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
],
"autoGenerate": true
]
},
"indices": [],
"foreignKeys": [
Expand Down Expand Up @@ -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')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -65,18 +61,6 @@ public void onSuccess(@NonNull List<Receipt> receipts) {
return;
}

Collections.sort(receipts, new Comparator<Receipt>() {
@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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +47,7 @@
Staff.class,
Timetable.class
},
version = 2,
version = 3,
autoMigrations = {
@AutoMigration(from = 1, to = 2),
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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");
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Receipt>> getReceipts();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class Receipt {
public Double amount;

@ColumnInfo(name = "date")
public String date;
public Long date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2d92685

Please sign in to comment.