Skip to content

Commit

Permalink
Add search to equipment
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Nov 22, 2024
1 parent 39bb41f commit e8f35cd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 20 deletions.
12 changes: 12 additions & 0 deletions HabitRPG/Extensions/Down-Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ extension Down {
string.replaceCharacters(in: range, with: "\n")
range = string.mutableString.range(of: "<br>")
}

range = string.mutableString.range(of: "<strong>")
let scaledBaseSize = UIFontMetrics.default.scaledSystemFont(ofSize: baseSize).pointSize
while range.length > 0 {
string.replaceCharacters(in: range, with: "")
let endRange = string.mutableString.range(of: "</strong>")
string.replaceCharacters(in: endRange, with: "")
let boldStart = range.location
string.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: scaledBaseSize), range: NSRange(location: boldStart, length: endRange.location - boldStart))

range = string.mutableString.range(of: "<strong>")
}
}

private func applyMentions(_ string: NSMutableAttributedString, mentions: [String]) {
Expand Down
34 changes: 30 additions & 4 deletions HabitRPG/TableViewDataSources/EquipmentViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import Habitica_Models
import Habitica_Database
import ReactiveSwift

class EquipmentViewDataSource: BaseReactiveTableViewDataSource<GearProtocol> {
Expand All @@ -16,7 +17,28 @@ class EquipmentViewDataSource: BaseReactiveTableViewDataSource<GearProtocol> {
private let inventoryRepository = InventoryRepository()

private var equippedKey: String?
var searchProperty = MutableProperty<String?>(nil)
var searchString: String? {
get {
return searchProperty.value
}
set(value) {
searchProperty.value = value
}
}

private func buildGearSignalProducer(keys: [String], gearType: String, search: String?) -> SignalProducer<ReactiveResults<[GearProtocol]>, Never> {
let predicate: NSPredicate
if let search = search {
predicate = NSPredicate(format: "key IN %@ && type == %@ && (text CONTAINS[cd] %@ || notes CONTAINS[cd] %@)", keys, gearType, search, search)
} else {
predicate = NSPredicate(format: "key IN %@ && type == %@", keys, gearType)
}
return inventoryRepository.getGear(predicate: predicate).flatMapError({ (_) -> SignalProducer<ReactiveResults<[GearProtocol]>, Never> in
return SignalProducer.empty
})
}

init(useCostume: Bool, gearType: String) {
super.init()
sections.append(ItemSection<GearProtocol>())
Expand All @@ -28,15 +50,19 @@ class EquipmentViewDataSource: BaseReactiveTableViewDataSource<GearProtocol> {
}).filter({ (key) -> Bool in
return !key.isEmpty
})
}).flatMapError({ (_) -> SignalProducer<[String], Never> in
return SignalProducer.empty
})
.flatMap(.latest, {[weak self] (keys) in
return self?.inventoryRepository.getGear(predicate: NSPredicate(format: "key IN %@ && type == %@", keys, gearType)) ?? SignalProducer.empty
.combineLatest(with: searchProperty.producer)
.flatMap(.latest, {[weak self] (keys, search) in
return self?.buildGearSignalProducer(keys: keys, gearType: gearType, search: search) ?? SignalProducer.empty
})
.on(value: {[weak self](gear, changes) in
.on(value: {[weak self](gear: [GearProtocol], changes: ReactiveChangeset?) in
self?.sections[0].items = gear
self?.notify(changes: changes)
})
.start())
.start()
)

disposable.add(userRepository.getUser().on(value: {[weak self]user in
if useCostume {
Expand Down
10 changes: 5 additions & 5 deletions HabitRPG/UI/General/MainMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,15 @@ class MainMenuViewController: BaseTableViewController {
case "fall":
seasonText = L10n.fall
case "nye":
seasonText = L10n.nye
seasonText = L10n.winter
case "birthday":
seasonText = L10n.birthday
seasonText = L10n.winter
case "valentines":
seasonText = L10n.valentines
seasonText = L10n.winter
case "habitoween":
seasonText = "Habitoween"
seasonText = L10n.fall
case "thanksgiving":
seasonText = L10n.turkeyDay
seasonText = L10n.fall
default:
seasonText = L10n.isOpen
}
Expand Down
40 changes: 39 additions & 1 deletion HabitRPG/UI/Inventory/EquipmentDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import UIKit
import Habitica_Models

class EquipmentDetailViewController: BaseTableViewController {
class EquipmentDetailViewController: BaseTableViewController, UISearchResultsUpdating {

var selectedType: String?
var selectedCostume = false

var datasource: EquipmentViewDataSource?
private let inventoryRepository = InventoryRepository()

private var searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
super.viewDidLoad()
if let gearType = selectedType {
Expand All @@ -26,6 +28,28 @@ class EquipmentDetailViewController: BaseTableViewController {

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100

self.navigationItem.searchController = searchController
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.backButtonDisplayMode = .minimal
navigationItem.backButtonTitle = nil
if #available(iOS 16.0, *) {
self.navigationItem.preferredSearchBarPlacement = .inline
searchController.scopeBarActivation = .onTextEntry
searchController.searchSuggestions = [
UISearchSuggestionItem(localizedSuggestion: "Spring Gear"),
UISearchSuggestionItem(localizedSuggestion: "Summer Gear"),
UISearchSuggestionItem(localizedSuggestion: "Autumn Gear"),
UISearchSuggestionItem(localizedSuggestion: "Winter Gear"),
UISearchSuggestionItem(localizedSuggestion: "Subscriber Item")
]
}
searchController.searchResultsUpdater = self
}

override func applyTheme(theme: any Theme) {
super.applyTheme(theme: theme)
searchController.searchBar.backgroundColor = theme.contentBackgroundColor
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand All @@ -36,4 +60,18 @@ class EquipmentDetailViewController: BaseTableViewController {
}
}
}

func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text, !searchText.isEmpty {
datasource?.searchString = searchText
} else {
datasource?.searchString = nil
}
self.tableView.reloadData()
}

@available(iOS 16.0, *)
func updateSearchResults(for searchController: UISearchController, selecting searchSuggestion: any UISearchSuggestion) {
searchController.searchBar.text = searchSuggestion.localizedSuggestion
}
}
14 changes: 4 additions & 10 deletions HabitRPG/UI/Purchases/SubscriptionPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SubscriptionViewModel: BaseSubscriptionViewModel {
if self?.scrollToTop == nil {
self?.scrollToTop = Date()
}
if presentationPoint != nil {
if self?.presentationPoint != nil {
self?.dismiss()
}
}
Expand Down Expand Up @@ -553,7 +553,7 @@ class SubscriptionModalViewController: HostingPanModal<SubscriptionPage> {
super.init(nibName: nil, bundle: nil)
viewModel.dimissVC = {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.dismiss()
self.dismiss(animated: true)
})
}

Expand All @@ -576,7 +576,7 @@ class SubscriptionModalViewController: HostingPanModal<SubscriptionPage> {
super.init(coder: aDecoder)
viewModel.dimissVC = {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.dismiss()
self.dismiss(animated: true)
})
}
}
Expand All @@ -588,13 +588,7 @@ class SubscriptionModalViewController: HostingPanModal<SubscriptionPage> {
view.insertSubview(upperBackground, at: 0)
upperBackground.backgroundColor = .purple300
scrollView.bounces = false

userRepository.getUser().on(value: {[weak self] user in
self?.viewModel.isSubscribed = user.isSubscribed
self?.viewModel.subscriptionPlan = user.purchased?.subscriptionPlan
self?.viewModel.showHourglassPromo = user.purchased?.subscriptionPlan?.isEligableForHourglassPromo == true
}).start()


viewModel.onGiftButtonTapped = {[weak self] in
self?.giftSubscriptionButtonTapped()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public extension QuestDropItemProtocol {
return "Pet_Food_\(key ?? "")"
case "hatchingPotions":
return "Pet_HatchingPotion_\(key ?? "")"
case "pets":
return "stable_Pet-\(key ?? "")"
case "mounts":
return "Mount_Head_\(key ?? "")"
default:
return "shop_\(key ?? "")"
}
Expand Down

0 comments on commit e8f35cd

Please sign in to comment.