Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TermHistory, a class for tracking what terms the user views #42

Merged
merged 1 commit into from Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/src/androidTest/java/net/mdln/englisc/TermHistoryTest.java
@@ -0,0 +1,31 @@
package net.mdln.englisc;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;

public class TermHistoryTest {

@Test
public void history() {
Context ctx = InstrumentationRegistry.getInstrumentation().getTargetContext();
try (TermHistory history = new TermHistory(ctx, TermHistory.Location.IN_MEMORY, 0)) {
history.recordId(100, 1000);
history.recordId(101, 1001);
Set<Integer> expectedIds = new HashSet();
assertEquals(expectedIds, history.getIds(0));
expectedIds.add(101);
assertEquals(expectedIds, history.getIds(1));
expectedIds.add(100);
assertEquals(expectedIds, history.getIds(2));
assertEquals(expectedIds, history.getIds(3));
}
}
}
83 changes: 83 additions & 0 deletions app/src/main/java/net/mdln/englisc/TermHistory.java
@@ -0,0 +1,83 @@
package net.mdln.englisc;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
* Keeps track of term ids viewed and their timestamps so we can show recently viewed items
* in {@link MainActivity}.
*/
public class TermHistory implements AutoCloseable {

private static final int SCHEMA_VERSION = 1;

private final SQLiteOpenHelper databaseHelper;

TermHistory(Context ctx, Location loc, long deleteHistoryBeforeMillis) {
this.databaseHelper = new OpenHelper(ctx, loc, deleteHistoryBeforeMillis / 1000.0);
}

void recordId(int nid, long timeMillis) {
String sql = "INSERT INTO history (nid, timestamp_secs) VALUES (?, ?)";
databaseHelper.getWritableDatabase().execSQL(sql, new Object[]{nid, timeMillis / 1000.0});
}

Set<Integer> getIds(int limit) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Set<Integer> ids = new HashSet<>();
String sql = "SELECT nid FROM history ORDER BY timestamp_secs DESC LIMIT " + limit;
try (Cursor cursor = db.rawQuery(sql, new String[]{})) {
while (cursor.moveToNext()) {
ids.add(cursor.getInt(0));
}
}
return ids;
}

@Override
public void close() {
databaseHelper.close();
}

enum Location {ON_DISK, IN_MEMORY}

private static final class OpenHelper extends SQLiteOpenHelper {

private final double deleteHistoryBeforeSecs;

public OpenHelper(Context context, Location loc, double deleteHistoryBeforeSecs) {
super(context, getPath(context, loc), null, SCHEMA_VERSION);
this.deleteHistoryBeforeSecs = deleteHistoryBeforeSecs;
}

@Nullable
private static String getPath(Context context, Location loc) {
return loc == Location.IN_MEMORY ? null :
new File(context.getNoBackupFilesDir(), "history.db").toString();
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE history (nid INTEGER, timestamp_secs REAL)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

@Override
public void onOpen(SQLiteDatabase db) {
if (deleteHistoryBeforeSecs > 0) {
db.execSQL("DELETE FROM history WHERE timestamp_secs < ?", new String[]{String.valueOf(deleteHistoryBeforeSecs)});
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/net/mdln/englisc/WebViewStyle.java
Expand Up @@ -30,7 +30,7 @@ public static void apply(Activity activity, WebView view, String html) {
private static String styledHtml(Activity activity, String html, boolean night) {
String headBlock = "<head><style type=\"text/css\">" + Streams.readUtf8Resource(activity, R.raw.defn) + "</style></head>";
String styleClass = night ? "dark" : "light";
String bodyBlock = "<body class=\"" + styleClass + "\">" + html + "</body>";
String bodyBlock = "<body class=\"" + styleClass + "\">" + html + "</body>";
return "<html>" + headBlock + bodyBlock + "</html>";
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Expand Up @@ -5,5 +5,5 @@
<string name="close_button">Close</string>
<string name="feedback_subject">Feedback on %s</string>
<string name="type_feedback_here">TYPE FEEDBACK HERE</string>
<string name="main_help_text">Type \'th\' for \'þ\' or \'ð\', and \'ae\' for \'æ\'. You can search for Anglo-Saxon words, Modern English words, and abbreviations. Got ideas for how to make the app better? Please let me know by selecting Feedback in the ⋮ menu.</string>
<string name="main_help_text">Type \'th\' for \'þ\' or \'ð\', and \'ae\' for \'æ\'. You can search for Anglo-Saxon and Modern English words.</string>
</resources>