Skip to content

Commit

Permalink
Add optional parameter to texture querying methods
Browse files Browse the repository at this point in the history
And remove the parameterised flavors to avoid code duplication.

Sure, it's a bit more code now, but having just one SQL query for
each case is less error-prone.
  • Loading branch information
markusfisch committed Jul 10, 2023
1 parent 1d48447 commit 9a7b73c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public Cursor getShaders() {
null);
}

public Cursor getTextures() {
public Cursor getTextures(String substring) {
boolean useSubstring = substring != null;
return db.rawQuery(
"SELECT " +
TEXTURES_ID + "," +
Expand All @@ -162,25 +163,17 @@ public Cursor getTextures() {
TEXTURES_THUMB +
" FROM " + TEXTURES +
" WHERE " + TEXTURES_RATIO + " = 1" +
(useSubstring
? " AND " + TEXTURES_NAME + " LIKE ?"
: "") +
" ORDER BY " + TEXTURES_ID,
null);
}

public Cursor getTexturesWith(String substring) {
return db.rawQuery(
"SELECT " +
TEXTURES_ID + "," +
TEXTURES_NAME + "," +
TEXTURES_WIDTH + "," +
TEXTURES_HEIGHT + "," +
TEXTURES_THUMB +
" FROM " + TEXTURES +
" WHERE " + TEXTURES_RATIO + " = 1 AND " + TEXTURES_NAME + " LIKE ?" +
" ORDER BY " + TEXTURES_ID,
new String[]{"%" + substring + "%"});
useSubstring
? new String[]{"%" + substring + "%"}
: null);
}

public Cursor getSamplerCubeTextures() {
public Cursor getSamplerCubeTextures(String substring) {
boolean useSubstring = substring != null;
return db.rawQuery(
"SELECT " +
TEXTURES_ID + "," +
Expand All @@ -190,22 +183,13 @@ public Cursor getSamplerCubeTextures() {
TEXTURES_THUMB +
" FROM " + TEXTURES +
" WHERE " + TEXTURES_RATIO + " = 1.5" +
(useSubstring
? " AND " + TEXTURES_NAME + " LIKE ?"
: "") +
" ORDER BY " + TEXTURES_ID,
null);
}

public Cursor getSamplerCubeTexturesWith(String substring) {
return db.rawQuery(
"SELECT " +
TEXTURES_ID + "," +
TEXTURES_NAME + "," +
TEXTURES_WIDTH + "," +
TEXTURES_HEIGHT + "," +
TEXTURES_THUMB +
" FROM " + TEXTURES +
" WHERE " + TEXTURES_RATIO + " = 1.5 AND " + TEXTURES_NAME + " LIKE ?" +
" ORDER BY " + TEXTURES_ID,
new String[]{"%" + substring + "%"});
useSubstring
? new String[]{"%" + substring + "%"}
: null);
}

public boolean isShaderAvailable(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ protected void addTexture() {
}

protected Cursor loadTextures() {
if (searchQuery == null) {
return ShaderEditorApp.db.getTextures();
}

return ShaderEditorApp.db.getTexturesWith(searchQuery);
return ShaderEditorApp.db.getTextures(searchQuery);
}

private void showTexture(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ protected void addTexture() {

@Override
protected Cursor loadTextures() {
if (searchQuery == null) {
return ShaderEditorApp.db.getSamplerCubeTextures();
}

return ShaderEditorApp.db.getSamplerCubeTexturesWith(searchQuery);
return ShaderEditorApp.db.getSamplerCubeTextures(searchQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void onAttachedToWindow() {

MergeCursor mergeCursor = new MergeCursor(new Cursor[]{
matrixCursor,
ShaderEditorApp.db.getTextures()
ShaderEditorApp.db.getTextures(null)
});

adapter = new TextureSpinnerAdapter(context, mergeCursor);
Expand Down

0 comments on commit 9a7b73c

Please sign in to comment.