Skip to content

Commit

Permalink
Merge pull request #160 from ahctang/add-tools-to-search
Browse files Browse the repository at this point in the history
Add tools to universal search
  • Loading branch information
jaysondc authored Aug 16, 2020
2 parents 4ab9229 + 7d60595 commit 972649c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ abstract class SearchDao {
loadAllKinsectsSync(AppSettings.dataLocale)
}

private val toolsDataCache = CachedValue(timeout) {
loadAllToolsSync(AppSettings.dataLocale)
}

fun searchLocations(searchFilter: String): List<Location> {
val filter = SearchFilter(searchFilter)
return locationDataCache.get().filter { filter.matches(it.name) }
Expand Down Expand Up @@ -103,6 +107,11 @@ abstract class SearchDao {
return kinsectDataCache.get().filter { filter.matches(it.name) }
}

fun searchTools(searchFilter: String): List<ToolBase> {
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
Expand Down Expand Up @@ -196,4 +205,14 @@ abstract class SearchDao {
ORDER BY kt.name ASC
""")
abstract fun loadAllKinsectsSync(langId: String): List<Kinsect>

@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<ToolBase>
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ class SearchResultAdapter: BasicListDelegationAdapter<Any>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -44,20 +44,20 @@ 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
activityViewModel.searchActive.value = true
(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)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SearchResults(
val items: List<ItemBase> = emptyList(),
val weapons: List<WeaponBase> = emptyList(),
val quests: List<QuestBase> = emptyList(),
val kinsects: List<Kinsect> = emptyList()
val kinsects: List<Kinsect> = emptyList(),
val tools: List<ToolBase> = emptyList()
)

class UniversalSearchViewModel(app: Application) : AndroidViewModel(app) {
Expand Down Expand Up @@ -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)
)
}
}

0 comments on commit 972649c

Please sign in to comment.