Made a working code

This commit is contained in:
Lokaprathap 2025-11-23 23:31:46 +05:30
parent 7ea845062a
commit 3e0712e28c
10 changed files with 84 additions and 19 deletions

View File

@ -124,6 +124,11 @@ dependencies {
implementation(libs.com.google.android.material.material)
coreLibraryDesugaring(libs.com.android.tools.desugar.jdk.libs)
// Room (local database for history)
// Use explicit coordinates to ensure dependency resolution
implementation("androidx.room:room-runtime:2.6.1")
annotationProcessor("androidx.room:room-compiler:2.6.1")
// Third-party
implementation(libs.com.journeyapps.zxing.android.embedded)
implementation(libs.com.github.yalantis.ucrop)

View File

@ -1,6 +1,7 @@
package protect.card_locker;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
@ -15,9 +16,7 @@ import java.util.Calendar;
import java.util.List;
public class HistoryActivity extends CatimaAppCompatActivity {
private AppDatabase appDatabase;
private HistoryDao historyDao;
private static final String TAG = "HistoryActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -31,9 +30,6 @@ public class HistoryActivity extends CatimaAppCompatActivity {
actionBar.setDisplayHomeAsUpEnabled(true);
}
appDatabase = AppDatabase.getInstance(this);
historyDao = appDatabase.historyDao();
RecyclerView rvHistory = findViewById(R.id.rvHistory);
TextView tvEmpty = findViewById(R.id.tvEmpty);
@ -42,18 +38,33 @@ public class HistoryActivity extends CatimaAppCompatActivity {
long sevenDaysAgo = calendar.getTimeInMillis();
new Thread(() -> {
List<HistoryEntity> historyItems = historyDao.getLast7Days(sevenDaysAgo);
runOnUiThread(() -> {
if (historyItems.isEmpty()) {
try {
AppDatabase db = AppDatabase.getInstance(this);
// Remove any previously seeded debug-sample rows (cardId = -1)
db.historyDao().deleteByCardId(-1);
Log.d(TAG, "Removed any debug-sample history rows (cardId=-1)");
List<HistoryEntity> historyItems = db.historyDao().getLast7Days(sevenDaysAgo);
Log.d(TAG, "Loaded history items: " + (historyItems == null ? "null" : historyItems.size()));
final List<HistoryEntity> finalItems = historyItems;
runOnUiThread(() -> {
if (finalItems == null || finalItems.isEmpty()) {
rvHistory.setVisibility(View.GONE);
tvEmpty.setVisibility(View.VISIBLE);
} else {
rvHistory.setVisibility(View.VISIBLE);
tvEmpty.setVisibility(View.GONE);
rvHistory.setLayoutManager(new LinearLayoutManager(this));
rvHistory.setAdapter(new HistoryAdapter(finalItems));
}
});
} catch (Exception e) {
Log.e(TAG, "Failed to load history", e);
runOnUiThread(() -> {
rvHistory.setVisibility(View.GONE);
tvEmpty.setVisibility(View.VISIBLE);
} else {
rvHistory.setVisibility(View.VISIBLE);
tvEmpty.setVisibility(View.GONE);
rvHistory.setLayoutManager(new LinearLayoutManager(this));
rvHistory.setAdapter(new HistoryAdapter(historyItems));
}
});
});
}
}).start();
}

View File

@ -16,4 +16,8 @@ public interface HistoryDao {
@Query("DELETE FROM history WHERE timestamp < :sevenDaysAgo")
void deleteOlderThan(long sevenDaysAgo);
// Remove debug-sample rows inserted previously (cardId = -1)
@Query("DELETE FROM history WHERE cardId = :debugCardId")
void deleteByCardId(int debugCardId);
}

View File

@ -676,6 +676,32 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
return;
}
// Log history entry (record that this card was viewed). Use Room database in background.
new Thread(() -> {
try {
HistoryEntity entity = new HistoryEntity();
entity.cardId = loyaltyCardId;
entity.cardName = loyaltyCard != null ? loyaltyCard.store : "";
entity.timestamp = System.currentTimeMillis();
Log.d(TAG, "Inserting history entry: cardId=" + entity.cardId + " cardName=" + entity.cardName + " ts=" + entity.timestamp);
AppDatabase db = AppDatabase.getInstance(LoyaltyCardViewActivity.this);
db.historyDao().insert(entity);
// Cleanup old history entries older than 7 days
long sevenDaysAgo = System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000;
db.historyDao().deleteOlderThan(sevenDaysAgo);
// Log current count for debugging
List<HistoryEntity> items = db.historyDao().getLast7Days(sevenDaysAgo);
Log.d(TAG, "History count after insert: " + (items == null ? "null" : items.size()));
} catch (Exception e) {
// Keep logging non-fatal; don't crash the view activity
Log.e(TAG, "Failed to insert history entry", e);
}
}).start();
setTitle(loyaltyCard.store);
loyaltyCardGroups = DBHelper.getLoyaltyCardGroups(database, loyaltyCardId);

View File

@ -726,6 +726,13 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
return true;
}
if (id == R.id.action_history) {
// Open history activity showing last 7 days of card views
Intent i = new Intent(getApplicationContext(), HistoryActivity.class);
startActivity(i);
return true;
}
if (id == R.id.action_settings) {
Intent i = new Intent(getApplicationContext(), SettingsActivity.class);
mSettingsLauncher.launch(i);

View File

@ -15,7 +15,7 @@
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android.layout.height="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>

View File

@ -24,7 +24,7 @@
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history_white_24dp"
android:title="@string/History"
android:title="@string/history_title"
app:showAsAction="never"/>
<item
android:id="@+id/action_manage_groups"

View File

@ -22,7 +22,7 @@
<string name="edit">Edit</string>
<string name="delete">Delete</string>
<string name="confirm">Confirm</string>
<string name="History">History_title</string>
<string name="history_title">History</string>
<!-- START NOTE: i18n oddness -->
<!-- The following may seem weird, but it is necessary to give translators enough flexibility.
For example, in Russian, Android's plural quantity "one" actually refers to "any number ending on 1 but not ending in 11".

View File

@ -44,6 +44,14 @@
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.Material3.ActionBar">
<!-- Lightweight overlay used by AppBarLayout/Toolbar -->
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.Material3">
<!-- Lightweight popup overlay used for toolbar popups -->
</style>
<style name="AppTheme.TextView.NoData" parent="AppTheme">
<item name="android:layout_centerHorizontal">true</item>
<item name="android:layout_centerVertical">true</item>

View File

@ -1,6 +1,7 @@
[versions]
# Third-party
acra = "5.13.1"
room = "2.6.1"
# Testing
androidXTest = "1.7.0"
@ -18,6 +19,9 @@ androidx-preference-preference = { group = "androidx.preference", name = "prefer
com-google-android-material-material = { group = "com.google.android.material", name = "material", version = "1.13.0" }
com-android-tools-desugar_jdk_libs = { group = "com.android.tools", name = "desugar_jdk_libs", version = "2.1.5" }
androidx-room-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
androidx-room-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
# Third-party
com-journeyapps-zxing_android_embedded = { group = "com.journeyapps", name = "zxing-android-embedded", version = "4.3.0" }
com-github-yalantis-ucrop = { group = "com.github.yalantis", name = "ucrop", version = "2.2.11" }