Adds functional search using sqlite full text search syntax.
This commit is contained in:
@@ -14,9 +14,10 @@ import com.majinnaibu.monstercards.data.converters.SetOfStringConverter;
|
||||
import com.majinnaibu.monstercards.data.converters.SetOfTraitConverter;
|
||||
import com.majinnaibu.monstercards.data.converters.UUIDConverter;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.models.MonsterFTS;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Database(entities = {Monster.class}, version = 1)
|
||||
@Database(entities = {Monster.class, MonsterFTS.class}, version = 2)
|
||||
@TypeConverters({
|
||||
ArmorTypeConverter.class,
|
||||
ChallengeRatingConverter.class,
|
||||
|
||||
@@ -4,7 +4,10 @@ import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.migration.Migration;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
import com.facebook.flipper.android.AndroidFlipperClient;
|
||||
import com.facebook.flipper.android.utils.FlipperUtils;
|
||||
@@ -50,6 +53,7 @@ public class MonsterCardsApplication extends Application {
|
||||
}
|
||||
|
||||
m_db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "monsters")
|
||||
.addMigrations(MIGRATION_1_2)
|
||||
.fallbackToDestructiveMigrationOnDowngrade()
|
||||
.build();
|
||||
m_monsterLibraryRepository = new MonsterRepository(m_db);
|
||||
@@ -69,4 +73,17 @@ public class MonsterCardsApplication extends Application {
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
}
|
||||
|
||||
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
|
||||
@Override
|
||||
public void migrate(@NonNull SupportSQLiteDatabase database) {
|
||||
// rename table monster to monsters
|
||||
database.execSQL("ALTER TABLE monster RENAME TO monsters");
|
||||
// create the fts view
|
||||
database.execSQL("CREATE VIRTUAL TABLE IF NOT EXISTS `monsters_fts` USING FTS4(`name` TEXT, `size` TEXT, `type` TEXT, `subtype` TEXT, `alignment` TEXT, content=`monsters`)");
|
||||
// build the initial full text search index
|
||||
database.execSQL("INSERT INTO monsters_fts(monsters_fts) VALUES('rebuild')");
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,15 +15,18 @@ import io.reactivex.rxjava3.core.Flowable;
|
||||
|
||||
@Dao
|
||||
public interface MonsterDAO {
|
||||
@Query("SELECT * FROM monster")
|
||||
@Query("SELECT * FROM monsters")
|
||||
Flowable<List<Monster>> getAll();
|
||||
|
||||
@Query("SELECT * FROM monster WHERE id IN (:monsterIds)")
|
||||
@Query("SELECT * FROM monsters WHERE id IN (:monsterIds)")
|
||||
Flowable<List<Monster>> loadAllByIds(String[] monsterIds);
|
||||
|
||||
@Query("SELECT * FROM monster WHERE name LIKE :name LIMIT 1")
|
||||
@Query("SELECT * FROM monsters WHERE name LIKE :name LIMIT 1")
|
||||
Flowable<Monster> findByName(String name);
|
||||
|
||||
@Query("SELECT monsters.* FROM monsters JOIN monsters_fts ON monsters.oid = monsters_fts.docid WHERE monsters_fts MATCH :searchText")
|
||||
Flowable<List<Monster>> search(String searchText);
|
||||
|
||||
@Insert
|
||||
Completable insertAll(Monster... monsters);
|
||||
|
||||
|
||||
@@ -29,6 +29,13 @@ public class MonsterRepository {
|
||||
.observeOn(AndroidSchedulers.mainThread());
|
||||
}
|
||||
|
||||
public Flowable<List<Monster>> searchMonsters(String searchText) {
|
||||
return m_db.monsterDAO()
|
||||
.search(searchText)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread());
|
||||
}
|
||||
|
||||
public Flowable<Monster> getMonster(UUID monsterId) {
|
||||
return m_db.monsterDAO()
|
||||
.loadAllByIds(new String[]{monsterId.toString()})
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Entity(tableName = "monsters")
|
||||
@SuppressLint("DefaultLocale")
|
||||
@SuppressWarnings("unused")
|
||||
public class Monster {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.majinnaibu.monstercards.models;
|
||||
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.Fts4;
|
||||
|
||||
@Entity(tableName = "monsters_fts")
|
||||
@Fts4(contentEntity = Monster.class)
|
||||
public class MonsterFTS {
|
||||
public String name;
|
||||
public String size;
|
||||
public String type;
|
||||
public String subtype;
|
||||
public String alignment;
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
package com.majinnaibu.monstercards.ui.search;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.data.MonsterRepository;
|
||||
import com.majinnaibu.monstercards.ui.MCFragment;
|
||||
|
||||
public class SearchFragment extends MCFragment {
|
||||
@@ -21,22 +22,34 @@ public class SearchFragment extends MCFragment {
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
|
||||
View root = inflater.inflate(R.layout.fragment_search, container, false);
|
||||
MonsterRepository repository = this.getMonsterRepository();
|
||||
SearchResultsRecyclerViewAdapter adapter = new SearchResultsRecyclerViewAdapter(repository, null);
|
||||
final RecyclerView recyclerView = root.findViewById(R.id.monster_list);
|
||||
assert recyclerView != null;
|
||||
setupRecyclerView(recyclerView, adapter);
|
||||
|
||||
final TextView textView = root.findViewById(R.id.search_query);
|
||||
searchViewModel.getSearchQuery().observe(getViewLifecycleOwner(), new Observer<String>() {
|
||||
textView.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void onChanged(@Nullable String s) {
|
||||
textView.setText(s);
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable) {
|
||||
adapter.doSearch(textView.getText().toString());
|
||||
}
|
||||
});
|
||||
|
||||
final Button btnSearch = root.findViewById(R.id.button_search);
|
||||
btnSearch.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
}
|
||||
});
|
||||
return root;
|
||||
}
|
||||
|
||||
private void setupRecyclerView(@NonNull RecyclerView recyclerView, @NonNull SearchResultsRecyclerViewAdapter adapter) {
|
||||
recyclerView.setAdapter(adapter);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.majinnaibu.monstercards.ui.search;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.data.MonsterRepository;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.utils.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.rxjava3.core.Flowable;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
|
||||
public class SearchResultsRecyclerViewAdapter extends RecyclerView.Adapter<SearchResultsRecyclerViewAdapter.ViewHolder> {
|
||||
public interface ItemCallback {
|
||||
void onItem(Monster monster);
|
||||
}
|
||||
|
||||
private final MonsterRepository mRepository;
|
||||
private String mSearchText;
|
||||
private List<Monster> mValues;
|
||||
private Disposable mSubscriptionHandler;
|
||||
private final ItemCallback mOnClickHandler;
|
||||
|
||||
public SearchResultsRecyclerViewAdapter(MonsterRepository repository,
|
||||
ItemCallback onClick) {
|
||||
mRepository = repository;
|
||||
mSearchText = "";
|
||||
mValues = new ArrayList<>();
|
||||
mOnClickHandler = onClick;
|
||||
mSubscriptionHandler = null;
|
||||
|
||||
doSearch(mSearchText);
|
||||
}
|
||||
|
||||
public void doSearch(String searchText) {
|
||||
if (mSubscriptionHandler != null && !mSubscriptionHandler.isDisposed()) {
|
||||
mSubscriptionHandler.dispose();
|
||||
}
|
||||
mSearchText = searchText;
|
||||
Flowable<List<Monster>> foundMonsters = mRepository.searchMonsters(mSearchText);
|
||||
mSubscriptionHandler = foundMonsters.subscribe(monsters -> {
|
||||
mValues = monsters;
|
||||
notifyDataSetChanged();
|
||||
},
|
||||
throwable -> Logger.logError("Error performing search", throwable));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.monster_list_content, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||
Monster monster = mValues.get(position);
|
||||
holder.mIdView.setText(monster.id.toString().substring(0, 6));
|
||||
holder.mContentView.setText(monster.name);
|
||||
holder.itemView.setTag(monster);
|
||||
holder.itemView.setOnClickListener(view -> {
|
||||
if (mOnClickHandler != null) {
|
||||
mOnClickHandler.onItem(monster);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mValues.size();
|
||||
}
|
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder {
|
||||
final TextView mIdView;
|
||||
final TextView mContentView;
|
||||
|
||||
ViewHolder(View view) {
|
||||
super(view);
|
||||
mIdView = view.findViewById(R.id.id_text);
|
||||
mContentView = view.findViewById(R.id.content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,6 @@
|
||||
tools:context=".ui.search.SearchFragment"
|
||||
tools:targetApi="o">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_search"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:text="@string/action_search"
|
||||
app:layout_constraintBaseline_toBaselineOf="@+id/search_query"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/search_query"
|
||||
android:layout_width="0dp"
|
||||
@@ -27,7 +18,23 @@
|
||||
android:hint="@string/label_search_query"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="textPersonName"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button_search"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/monster_list"
|
||||
android:layout_height="0dp"
|
||||
android:layout_width="0dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:layout_constraintTop_toBottomOf="@+id/search_query"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layoutManager="LinearLayoutManager"
|
||||
tools:context=".SearchResultsFragment"
|
||||
tools:listitem="@layout/monster_list_content" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user