From 7d605958f4ac417a0533927107d3f043b2bb87d0 Mon Sep 17 00:00:00 2001 From: ahctang Date: Sun, 16 Aug 2020 14:53:54 +0900 Subject: [PATCH] Add tools to universal search --- .../mhworlddatabase/data/dao/SearchDao.kt | 19 +++++++++++++++++++ .../features/search/SearchResultAdapter.kt | 11 +++++++++++ .../search/UniversalSearchFragment.kt | 8 ++++---- .../search/UniversalSearchViewModel.kt | 6 ++++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/data/dao/SearchDao.kt b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/data/dao/SearchDao.kt index 5a0aa369..9f0abd9f 100644 --- a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/data/dao/SearchDao.kt +++ b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/data/dao/SearchDao.kt @@ -53,6 +53,10 @@ abstract class SearchDao { loadAllKinsectsSync(AppSettings.dataLocale) } + private val toolsDataCache = CachedValue(timeout) { + loadAllToolsSync(AppSettings.dataLocale) + } + fun searchLocations(searchFilter: String): List { val filter = SearchFilter(searchFilter) return locationDataCache.get().filter { filter.matches(it.name) } @@ -103,6 +107,11 @@ abstract class SearchDao { return kinsectDataCache.get().filter { filter.matches(it.name) } } + fun searchTools(searchFilter: String): List { + val filter = SearchFilter(searchFilter) + return toolsDataCache.get().filter { filter.matches(it.name) } + } + // All queries for search are below. Currently, it loads the entire table. // These are basically sync duplicates of existing queries, but the slower ones // could be replaced for an "in-db" search, once the db supports normalized_names @@ -196,4 +205,14 @@ abstract class SearchDao { ORDER BY kt.name ASC """) abstract fun loadAllKinsectsSync(langId: String): List + + @Query(""" + SELECT t.id, order_id, icon_color, name, name_base, description, tool_type + FROM tool t + JOIN tool_text tt + ON tt.id = t.id + WHERE lang_id = :langId + ORDER BY t.order_id ASC + """) + abstract fun loadAllToolsSync(langId: String): List } \ No newline at end of file diff --git a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/SearchResultAdapter.kt b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/SearchResultAdapter.kt index 83c204c7..e32e796f 100644 --- a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/SearchResultAdapter.kt +++ b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/SearchResultAdapter.kt @@ -30,12 +30,23 @@ class SearchResultAdapter: BasicListDelegationAdapter(SimpleUniversalBinder items.addAll(results.weapons.map(::createWeaponBinder)) items.addAll(results.quests.map(::createQuestBinder)) items.addAll(results.kinsects.map(::createKinsectBinder)) + items.addAll(results.tools.map(::createToolBinder)) this.items = items notifyDataSetChanged() } } +fun createToolBinder(tool: ToolBase) = createSimpleUniversalBinder { ctx -> + val icon = AssetLoader.loadIconFor(tool) + val name = tool.name + val typeString = ctx.getString(R.string.type_location) + + SimpleUniversalBinding(name, typeString, IconType.EMBELLISHED, icon) { + it.getRouter().navigateToolDetail(tool.id) + } +} + fun createLocationBinder(location: Location) = createSimpleUniversalBinder { ctx -> val icon = AssetLoader.loadIconFor(location) val name = location.name diff --git a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchFragment.kt b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchFragment.kt index bfe2677e..2be70e67 100644 --- a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchFragment.kt +++ b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchFragment.kt @@ -20,7 +20,7 @@ private const val SEARCH_FILTER = "SEARCH_FILTER" class UniversalSearchFragment : RecyclerViewFragment() { private val activityViewModel by lazy { - ViewModelProviders.of(activity!!).get(MainActivityViewModel::class.java) + ViewModelProviders.of(requireActivity()).get(MainActivityViewModel::class.java) } private val searchViewModel by lazy { @@ -44,7 +44,7 @@ class UniversalSearchFragment : RecyclerViewFragment() { } // add decorator - recyclerView.addItemDecoration(StandardDivider(DashedDividerDrawable(context!!))) + recyclerView.addItemDecoration(StandardDivider(DashedDividerDrawable(requireContext()))) // open up the search menu (if not open) if we're on this page // If the user hit back and returned to this page, we need to open it again @@ -52,12 +52,12 @@ class UniversalSearchFragment : RecyclerViewFragment() { (activity as MainActivity).updateSearchView(searchViewModel.searchFilter) // If the activity filter changes, update the fragment viewmodel - activityViewModel.filter.observe(this, Observer { + activityViewModel.filter.observe(viewLifecycleOwner, Observer { searchViewModel.searchData(it) }) // If the search results have a value or changed, show them. - searchViewModel.searchResults.observe(this, Observer { + searchViewModel.searchResults.observe(viewLifecycleOwner, Observer { if (it != null) adapter.bindSearchResults(it) }) } diff --git a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchViewModel.kt b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchViewModel.kt index 75760b84..e4020266 100644 --- a/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchViewModel.kt +++ b/app/src/main/java/com/gatheringhallstudios/mhworlddatabase/features/search/UniversalSearchViewModel.kt @@ -19,7 +19,8 @@ class SearchResults( val items: List = emptyList(), val weapons: List = emptyList(), val quests: List = emptyList(), - val kinsects: List = emptyList() + val kinsects: List = emptyList(), + val tools: List = emptyList() ) class UniversalSearchViewModel(app: Application) : AndroidViewModel(app) { @@ -90,7 +91,8 @@ class UniversalSearchViewModel(app: Application) : AndroidViewModel(app) { items = dao.searchItems(filterStr), weapons = dao.searchWeapons(filterStr), quests = dao.searchQuests(filterStr), - kinsects = dao.searchKinsects(filterStr) + kinsects = dao.searchKinsects(filterStr), + tools = dao.searchTools(filterStr) ) } } \ No newline at end of file