Skip to content

Commit

Permalink
db updated; fixed navigation on first/last chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoponciano committed Jun 5, 2015
1 parent 7909c81 commit e432d5d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String args[]) throws Exception {
verses.addStringProperty("text");

Entity books = schema.addEntity("Book");
books.addLongProperty("ordering").primaryKey().unique();
books.addLongProperty("indice").primaryKey().unique();
books.setTableName("books");
books.addStringProperty("name");
books.addStringProperty("abbreviation");
Expand Down
Binary file modified app/src/main/assets/databases/biblia_arc.db.zip
Binary file not shown.
18 changes: 9 additions & 9 deletions app/src/main/java-gen/greendao/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
*/
public class Book {

private Long ordering;
private Long indice;
private String name;
private String abbreviation;
private String testament;

public Book() {
}

public Book(Long ordering) {
this.ordering = ordering;
public Book(Long indice) {
this.indice = indice;
}

public Book(Long ordering, String name, String abbreviation, String testament) {
this.ordering = ordering;
public Book(Long indice, String name, String abbreviation, String testament) {
this.indice = indice;
this.name = name;
this.abbreviation = abbreviation;
this.testament = testament;
}

public Long getOrdering() {
return ordering;
public Long getIndice() {
return indice;
}

public void setOrdering(Long ordering) {
this.ordering = ordering;
public void setIndice(Long indice) {
this.indice = indice;
}

public String getName() {
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java-gen/greendao/BookDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BookDao extends AbstractDao<Book, Long> {
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Ordering = new Property(0, Long.class, "ordering", true, "ORDERING");
public final static Property Indice = new Property(0, Long.class, "indice", true, "INDICE");
public final static Property Name = new Property(1, String.class, "name", false, "NAME");
public final static Property Abbreviation = new Property(2, String.class, "abbreviation", false, "ABBREVIATION");
public final static Property Testament = new Property(3, String.class, "testament", false, "TESTAMENT");
Expand All @@ -42,7 +42,7 @@ public BookDao(DaoConfig config, DaoSession daoSession) {
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "'books' (" + //
"'ORDERING' INTEGER PRIMARY KEY UNIQUE ," + // 0: ordering
"'INDICE' INTEGER PRIMARY KEY UNIQUE ," + // 0: indice
"'NAME' TEXT," + // 1: name
"'ABBREVIATION' TEXT," + // 2: abbreviation
"'TESTAMENT' TEXT);"); // 3: testament
Expand All @@ -59,9 +59,9 @@ public static void dropTable(SQLiteDatabase db, boolean ifExists) {
protected void bindValues(SQLiteStatement stmt, Book entity) {
stmt.clearBindings();

Long ordering = entity.getOrdering();
if (ordering != null) {
stmt.bindLong(1, ordering);
Long indice = entity.getIndice();
if (indice != null) {
stmt.bindLong(1, indice);
}

String name = entity.getName();
Expand Down Expand Up @@ -90,7 +90,7 @@ public Long readKey(Cursor cursor, int offset) {
@Override
public Book readEntity(Cursor cursor, int offset) {
Book entity = new Book( //
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // ordering
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // indice
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // abbreviation
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // testament
Expand All @@ -101,7 +101,7 @@ public Book readEntity(Cursor cursor, int offset) {
/** @inheritdoc */
@Override
public void readEntity(Cursor cursor, Book entity, int offset) {
entity.setOrdering(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setIndice(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
entity.setAbbreviation(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
entity.setTestament(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
Expand All @@ -110,15 +110,15 @@ public void readEntity(Cursor cursor, Book entity, int offset) {
/** @inheritdoc */
@Override
protected Long updateKeyAfterInsert(Book entity, long rowId) {
entity.setOrdering(rowId);
entity.setIndice(rowId);
return rowId;
}

/** @inheritdoc */
@Override
public Long getKey(Book entity) {
if(entity != null) {
return entity.getOrdering();
return entity.getIndice();
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class BibliaDatabase extends SQLiteAssetHelper {
//private static final String TAG = SQLiteAssetHelper.class.getSimpleName();
private static final String DATABASE_NAME = "biblia_arc.db";
private static final int DATABASE_VERSION = 10;
private static final int DATABASE_VERSION = 11;

private static DaoSession daoSession;
private static DaoMaster daoMaster;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,40 @@ public ArrayList<String> Busca(String s) {
return listaDeVersos;
}

public Verse UltimoCapitulo(String livroSigla) {
QueryBuilder<Verse> pqb = verseDao.queryBuilder();
return pqb
.where(VerseDao.Properties.Book.eq(livroSigla))
.orderDesc(VerseDao.Properties.Chapter).limit(1).unique();
}

public Verse ProximoCapitulo(String nomeDoLivro, int capitulo) {
Verse proximoCapitulo;
Verse proximoCapitulo = null;

Book book = bookDao.queryBuilder()
.where(BookDao.Properties.Name.eq(nomeDoLivro)).unique();

QueryBuilder<Verse> pqb = verseDao.queryBuilder();
Verse ultimo = UltimoCapitulo(book.getAbbreviation());

proximoCapitulo = pqb.where(pqb.and(
VerseDao.Properties.Book.eq(book.getAbbreviation()),
VerseDao.Properties.Chapter.eq(capitulo+1)),
VerseDao.Properties.Verse.eq(1))
.unique();
if (proximoCapitulo == null) {
QueryBuilder<Verse> pqb = verseDao.queryBuilder();
if (capitulo < ultimo.getChapter()) {
proximoCapitulo = pqb.where(pqb.and(
VerseDao.Properties.Book.eq(book.getAbbreviation()),
VerseDao.Properties.Chapter.eq(capitulo+1)),
VerseDao.Properties.Verse.eq(1))
.unique();
}
else {
Book nextBook = bookDao.queryBuilder()
.where(BookDao.Properties.Ordering.eq(capitulo-1)).unique();
.where(BookDao.Properties.Indice.eq(book.getIndice()+1)).unique();
pqb = verseDao.queryBuilder();
proximoCapitulo = pqb.where(pqb.and(
VerseDao.Properties.Book.eq(nextBook.getAbbreviation()),
VerseDao.Properties.Chapter.eq(1),
VerseDao.Properties.Verse.eq(1)))
.unique();
if (nextBook != null) {
proximoCapitulo = pqb.where(pqb.and(
VerseDao.Properties.Book.eq(nextBook.getAbbreviation()),
VerseDao.Properties.Chapter.eq(1),
VerseDao.Properties.Verse.eq(1)))
.unique();
}
}

return proximoCapitulo;
Expand All @@ -112,13 +124,17 @@ public Verse CapituloAnterior(String nomeDoLivro, int capitulo) {
VerseDao.Properties.Chapter.eq(capitulo-1)),
VerseDao.Properties.Verse.eq(1))
.unique();
if (capituloAnterior == null) {

if (capituloAnterior == null && book.getIndice() > 1) {
Book previousBook = bookDao.queryBuilder()
.where(BookDao.Properties.Ordering.eq(capitulo-1)).unique();
.where(BookDao.Properties.Indice.eq(book.getIndice()-1)).unique();

pqb = verseDao.queryBuilder();
Verse ultimo = UltimoCapitulo(previousBook.getAbbreviation());

capituloAnterior = pqb.where(pqb.and(
VerseDao.Properties.Book.eq(previousBook.getAbbreviation()),
VerseDao.Properties.Chapter.eq(1),
VerseDao.Properties.Chapter.eq(ultimo.getChapter()),
VerseDao.Properties.Verse.eq(1)))
.unique();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
//Toast.makeText(getActivity(), "Left Swipe", Toast.LENGTH_SHORT).show();
Verse proximo = listaDeVersiculos.ProximoCapitulo(livro, capitulo);
if (proximo != null) {

Book book = listaDeLivros.ByAbbreviation(proximo.getBook());
Fragment newFragment = VersiculosFragment.newInstance(
book.getName(),
Expand All @@ -61,8 +60,8 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
} else if (e2.getX() - e1.getX() > VersiculosFragment.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > VersiculosFragment.SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(getActivity(), "Right Swipe", Toast.LENGTH_SHORT).show();
Verse anterior = listaDeVersiculos.CapituloAnterior(livro, capitulo);
Book book = listaDeLivros.ByAbbreviation(anterior.getBook());
if (anterior != null) {
Book book = listaDeLivros.ByAbbreviation(anterior.getBook());
Fragment newFragment = VersiculosFragment.newInstance(
book.getName(),
anterior.getChapter()
Expand All @@ -81,7 +80,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
}
}
} catch (Exception e) {
Log.d("MyGestureDetector.onFling", e.getMessage());
Log.d("GestureDetector.onFling", e.getLocalizedMessage());
// nothing
}
return false;
Expand Down

0 comments on commit e432d5d

Please sign in to comment.