From 9a7b73c738ee861c5a5ed8a9c9627f955f96b02d Mon Sep 17 00:00:00 2001 From: Markus Fisch Date: Mon, 10 Jul 2023 22:51:46 +0200 Subject: [PATCH] Add optional parameter to texture querying methods 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. --- .../shadereditor/database/Database.java | 48 +++++++------------ .../UniformSampler2dPageFragment.java | 6 +-- .../UniformSamplerCubePageFragment.java | 6 +-- .../widget/BackBufferParametersView.java | 2 +- 4 files changed, 19 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/database/Database.java b/app/src/main/java/de/markusfisch/android/shadereditor/database/Database.java index 58d71f9c..ab6d28b1 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/database/Database.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/database/Database.java @@ -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 + "," + @@ -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 + "," + @@ -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) { diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSampler2dPageFragment.java b/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSampler2dPageFragment.java index 3ca59128..5439fb77 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSampler2dPageFragment.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSampler2dPageFragment.java @@ -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) { diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSamplerCubePageFragment.java b/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSamplerCubePageFragment.java index 7a3d8396..1b889078 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSamplerCubePageFragment.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/fragment/UniformSamplerCubePageFragment.java @@ -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); } } diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/widget/BackBufferParametersView.java b/app/src/main/java/de/markusfisch/android/shadereditor/widget/BackBufferParametersView.java index 6e3fe3bf..1c5f86ad 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/widget/BackBufferParametersView.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/widget/BackBufferParametersView.java @@ -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);