Skip to content

Commit

Permalink
Merge branch 'devel' into stable
Browse files Browse the repository at this point in the history
Fixes:
- Fixed orientation change not preserving data
- Added cache for spell list to speed up load times
  • Loading branch information
dseguin committed Sep 4, 2020
2 parents bfac117 + d537c1a commit 4dc5c30
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ D&DB

Android app and database for searching and displaying data for Dungeons & Dragons (5e).

Pre-built binaries available in the [releases tab](https://github.com/dseguin/dndb/releases/latest), or download the [latest release directly](https://github.com/dseguin/dndb/releases/download/v0.1.1/dndb-androidapi19-0.1.1.apk).
Pre-built binaries available in the [releases tab](https://github.com/dseguin/dndb/releases/latest), or download the [latest release directly](https://github.com/dseguin/dndb/releases/download/v0.1.2/dndb-androidapi19-0.1.2.apk).

Spells
------
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/ca/printf/dndb/data/DndbSQLManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import ca.printf.dndb.R;
import ca.printf.dndb.view.ErrorFragment;

public class DndbSQLManager extends SQLiteOpenHelper {
private static final String DB_NAME = "dndb.sqlite";
Expand All @@ -33,18 +35,21 @@ public class DndbSQLManager extends SQLiteOpenHelper {
public static final String TABLE_SPELL_COMPONENT = "spell_component";
public static final String TABLE_COMPONENT = "component";
private Context ctx;
private FragmentActivity act;

public DndbSQLManager(Context c) {
public DndbSQLManager(Context c, FragmentActivity a) {
super(c, DB_NAME, null, DB_VER);
this.ctx = c;
this.act = a;
}

public void onCreate(SQLiteDatabase db) {
try {
CommonIO.execSQLFromFile(ctx, R.raw.spells_ddl, db);
CommonIO.execSQLFromFile(ctx, R.raw.spells_init_dml, db);
} catch (IOException e) {
} catch (Exception e) {
Log.e(this.getClass().getName(), "Error creating database ", e);
ErrorFragment.errorScreen(act.getSupportFragmentManager(), "Error creating database", e);
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ca/printf/dndb/entity/Spell.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class Spell implements Serializable {
public static final String QUERY_CONDITION_OPTIONS = QUERY_ATTR_OPTIONS(COL_CONDITION, DndbSQLManager.TABLE_CONDITION);
public static final String QUERY_SOURCE_OPTIONS = QUERY_ATTR_OPTIONS(COL_SOURCE_SHORTNAME, DndbSQLManager.TABLE_SOURCE);
public static final String QUERY_CLASS_OPTIONS = QUERY_ATTR_OPTIONS(COL_CLASS, DndbSQLManager.TABLE_CLASS_LIST);
// Get spell count
public static final String QUERY_SPELL_COUNT = "SELECT COUNT(" + COL_ID + ") FROM " + DndbSQLManager.TABLE_SPELL + ";";
// Instance vars
private long id;
private String name;
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/ca/printf/dndb/view/RootActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ protected void onCreate(Bundle savedInstanceState) {
ActionBarDrawerToggle drw_tog =
new ActionBarDrawerToggle(this, drw, tb, R.string.app_name, R.string.app_name);
drw.addDrawerListener(drw_tog);
drw.openDrawer(GravityCompat.START);
if(savedInstanceState == null)
drw.openDrawer(GravityCompat.START);
drw_tog.syncState();

((NavigationView)findViewById(R.id.nav_sidebar)).setNavigationItemSelectedListener(this);

content_frag = new DefaultFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame, content_frag, FRAG_DEFAULT)
.commit();
if(savedInstanceState == null) {
content_frag = new DefaultFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame, content_frag, FRAG_DEFAULT)
.commit();
}
}

public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/ca/printf/dndb/view/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != FILEPICKER_RESULT || resultCode != Activity.RESULT_OK)
return;
try {
DndbSQLManager dbman = new DndbSQLManager(getContext());
DndbSQLManager dbman = new DndbSQLManager(getContext(), getActivity());
SQLiteDatabase db = dbman.getWritableDatabase();
dbman.execZipPackage(db, getActivity().getContentResolver().openInputStream(data.getData()));
db.close();
} catch (FileNotFoundException | NullPointerException e) {
Log.e("onActivityResult", "Error loading file from file picker", e);
} catch (IOException e) {
Log.e("onActivityResult", "Error processing SQL in " + data.getData().toString(), e);
} catch (Exception e) {
Log.e("onActivityResult", "Error processing source package", e);
ErrorFragment.errorScreen(getActivity().getSupportFragmentManager(),
"Error processing source package", e);
}
}

private void resetDB() {
DndbSQLManager dbman = new DndbSQLManager(getContext());
DndbSQLManager dbman = new DndbSQLManager(getContext(), getActivity());
SQLiteDatabase db = dbman.getWritableDatabase();
Log.d("resetDB", "Clearing database with onCreate()");
dbman.onCreate(db);
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/ca/printf/dndb/view/SpellDetailsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ca.printf.dndb.entity.Spell;

public class SpellDetailsFragment extends Fragment {
private static final String PREV_SPELL = "previous_spell";
private Spell spell;

public SpellDetailsFragment(Spell spell) {
Expand All @@ -24,6 +25,18 @@ public SpellDetailsFragment() {}

public void onCreate(Bundle b) {
super.onCreate(b);
if(b == null)
return;
spell = (Spell)b.getSerializable(PREV_SPELL);
}

public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(saveCurrentSpell(outState));
}

private Bundle saveCurrentSpell(Bundle b) {
b.putSerializable(PREV_SPELL, this.spell);
return b;
}

public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b) {
Expand Down
87 changes: 83 additions & 4 deletions app/src/main/java/ca/printf/dndb/view/SpellsListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import ca.printf.dndb.R;
Expand All @@ -29,6 +34,7 @@
import ca.printf.dndb.list.SpellSortSpinner;

public class SpellsListFragment extends Fragment {
private static final String SPELL_CACHE = "spell_cache";
private ArrayList<Spell> spells;
private DndbSQLManager dbman;
private SpellListManager adapter;
Expand All @@ -41,14 +47,28 @@ public SpellsListFragment() {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbman = new DndbSQLManager(getContext());
dbman = new DndbSQLManager(getContext(), getActivity());
initSpells();
try {
if(loadSpellCache()) {
Log.d("loadSpellCache", "Spell cache successfully loaded");
return;
}
} catch (Exception e) {
Log.e("loadSpellCache", "Error while reading spell cache", e);
}
try {
readSpellsFromDB();
} catch (Exception e) {
ErrorFragment.errorScreen(getActivity().getSupportFragmentManager(),
"readSpellsFromDB: Error reading spells from database", e);
}
try {
if(saveSpellCache())
Log.d("saveSpellCache", "Spell cache was written successfully");
} catch (Exception e) {
Log.e("saveSpellCache", "Error while writing to spell cache", e);
}
}

public View onCreateView(LayoutInflater li, ViewGroup v, Bundle b) {
Expand All @@ -57,8 +77,8 @@ public View onCreateView(LayoutInflater li, ViewGroup v, Bundle b) {
ListView list = root.findViewById(R.id.spells_listview);
list.setAdapter(adapter);
list.setOnItemClickListener(spellSelection);
((Button)root.findViewById(R.id.spells_btn_filter_spells)).setOnClickListener(filterButton);
((Button)root.findViewById(R.id.spells_btn_sort_spells)).setOnClickListener(sortButton);
root.findViewById(R.id.spells_btn_filter_spells).setOnClickListener(filterButton);
root.findViewById(R.id.spells_btn_sort_spells).setOnClickListener(sortButton);
return root;
}

Expand Down Expand Up @@ -441,4 +461,63 @@ private void setSpellMultivalues(Spell s, SQLiteDatabase db) {
private String stripTableFromCol(String col) {
return col.substring(col.lastIndexOf('.') + 1);
}

private File getSpellCache() throws IOException {
File dir = getActivity().getCacheDir();
if(dir == null)
throw new IOException("getCacheDir(): Could not get file handler to cache directory");
File f = new File(dir.getPath() + File.separator + SPELL_CACHE);
if(f.createNewFile())
Log.d("getSpellCache", "Cache file \"" + SPELL_CACHE + "\" does not exist. Creating...");
return f;
}

private boolean saveSpellCache() throws IOException {
File f = getSpellCache();
if(f.delete() && f.createNewFile()) {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
for(Spell s : spells)
oos.writeObject(s);
oos.close();
} else {
return false;
}
return true;
}

private boolean loadSpellCache() throws IOException {
ArrayList<Spell> tmp = new ArrayList<>();
File f = getSpellCache();
if(f.length() == 0)
return false;
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
while(ois.available() != -1) {
try {
tmp.add((Spell)ois.readObject());
} catch (ClassNotFoundException e) {
ois.close();
return false;
} catch (EOFException e) {
break;
}
}
ois.close();
if(tmp.size() != getDBSpellCount())
return false;
spells = tmp;
return true;
}

private int getDBSpellCount() {
int ret = 0;
SQLiteDatabase db = dbman.getReadableDatabase();
Cursor res = db.rawQuery(Spell.QUERY_SPELL_COUNT, null);
if(res.getCount() != 0) {
res.moveToFirst();
ret = res.getInt(0);
}
res.close();
db.close();
return ret;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/about_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
android:id="@+id/about_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColorLink="@color/colorAccent"
android:text="@string/about_text"/>
</LinearLayout>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/spells_listview_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -49,6 +50,7 @@
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="10dp"
android:columnCount="2">
<LinearLayout
android:layout_width="200dp"
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [<!ENTITY appversion "0.1.0"> <!ENTITY appname "D&amp;DB">]>
<!DOCTYPE resources [<!ENTITY appversion "0.1.2"> <!ENTITY appname "D&amp;DB">]>

<resources>
<string name="app_name">&appname;</string>
Expand Down Expand Up @@ -72,10 +72,18 @@
<string name="label_spell_sort">Sort by :</string>
<string name="general_button_sort">Sort</string>
<string name="label_spell_sort_header">Sort Spells</string>
<string name="about_text">&appname;, created by <a href="https://github.com/dseguin">David Seguin</a>\n\n
<string name="about_text">&appname; is an application for browsing data from
the 5th edition ruleset of Dungeons &amp; Dragons.\n\n
Author : <a href="https://github.com/dseguin">David Seguin</a>\n
App Version : &appversion;\n
Source Code : <a href="https://github.com/dseguin/dndb">GitHub</a>\n
License : <a href="https://opensource.org/licenses/MIT">MIT</a>
License : <a href="https://github.com/dseguin/dndb/blob/stable/LICENSE">MIT</a>\n\n
Material included comes from the
<a href="https://dnd.wizards.com/articles/features/systems-reference-document-srd">Wizards of the Coast SRD</a>
and other sources released under the
<a href="https://media.wizards.com/2016/downloads/DND/SRD-OGL_V5.1.pdf">Open Game License</a>.\n\n
The \"dragon ampersand\" is a <a href="https://dnd.wizards.com/pressassets">press asset</a>
provided by Wizards of the Coast.
</string>
<string name="label_spell_sort_descending">Sort in descending order</string>
<string name="label_spell_filter_exclude">Exclude selected components</string>
Expand All @@ -87,4 +95,4 @@
<string name="general_confirm_dialog_title">Are you sure?</string>
<string name="general_button_confirm">Confirm</string>
<string name="label_settings_reset_db_confim_msg">Are you sure you want to reset your database to initial defaults?</string>
</resources>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0-beta05"
classpath 'com.android.tools.build:gradle:4.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 4dc5c30

Please sign in to comment.