Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS-3282 Global search | Save last search state for each space #1895

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Anytype.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@
2E88257C2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E88257B2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift */; };
2E88257E2C5BDF0D00FD59D3 /* DocumentMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E88257D2C5BDF0D00FD59D3 /* DocumentMode.swift */; };
2E8825802C6140BB00FD59D3 /* ObjectVersionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E88257F2C6140BB00FD59D3 /* ObjectVersionData.swift */; };
2E8825832C650FCE00FD59D3 /* GlobalSearchSavedStatesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E8825822C650FCE00FD59D3 /* GlobalSearchSavedStatesService.swift */; };
2E92219D294B8B5000968750 /* DataViewBlockViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E92219C294B8B5000968750 /* DataViewBlockViewModel.swift */; };
2E92219F294B8CDE00968750 /* DataViewBlockConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E92219E294B8CDE00968750 /* DataViewBlockConfiguration.swift */; };
2E9221A1294B8D4900968750 /* DataViewBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E9221A0294B8D4900968750 /* DataViewBlockView.swift */; };
Expand Down Expand Up @@ -2919,6 +2920,7 @@
2E88257B2C5A61AC00FD59D3 /* ObjectVersionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectVersionViewModel.swift; sourceTree = "<group>"; };
2E88257D2C5BDF0D00FD59D3 /* DocumentMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentMode.swift; sourceTree = "<group>"; };
2E88257F2C6140BB00FD59D3 /* ObjectVersionData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectVersionData.swift; sourceTree = "<group>"; };
2E8825822C650FCE00FD59D3 /* GlobalSearchSavedStatesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalSearchSavedStatesService.swift; sourceTree = "<group>"; };
2E92219C294B8B5000968750 /* DataViewBlockViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockViewModel.swift; sourceTree = "<group>"; };
2E92219E294B8CDE00968750 /* DataViewBlockConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockConfiguration.swift; sourceTree = "<group>"; };
2E9221A0294B8D4900968750 /* DataViewBlockView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataViewBlockView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -7926,6 +7928,14 @@
path = Object;
sourceTree = "<group>";
};
2E8825812C650F7F00FD59D3 /* GlobalSearchSavedStates */ = {
isa = PBXGroup;
children = (
2E8825822C650FCE00FD59D3 /* GlobalSearchSavedStatesService.swift */,
);
path = GlobalSearchSavedStates;
sourceTree = "<group>";
};
2E92219B294B8B0100968750 /* DataView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -8627,6 +8637,7 @@
3D2F9C14271EADC50043C326 /* Object */ = {
isa = PBXGroup;
children = (
2E8825812C650F7F00FD59D3 /* GlobalSearchSavedStates */,
2EAFEE5A2BEA653900DB190C /* SearchWithMeta */,
3D8624402B738D6400A5FCF4 /* TypeProvider */,
3D8624452B738D6400A5FCF4 /* TypesService */,
Expand Down Expand Up @@ -12072,6 +12083,7 @@
12DE4063264AE4C8007B5074 /* UIViewExtensions.swift in Sources */,
C905B29A2828CA2F00F1106B /* ToastPresenter.swift in Sources */,
12A7B6C226C3E47200544037 /* CoverColor.swift in Sources */,
2E8825832C650FCE00FD59D3 /* GlobalSearchSavedStatesService.swift in Sources */,
2A5025212BC02BBE00A141B8 /* BasicIconView.swift in Sources */,
1215E8C126A86CAC0089AF7E /* ImageSource.swift in Sources */,
3D30426A2BD123A60098F80F /* MembershipTierSelectionCarousel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
struct GlobalSearchState: Equatable, Hashable {
struct GlobalSearchState: Equatable, Hashable, Codable {
var searchText = ""
var mode: Mode = .default

enum Mode: Equatable, Hashable {
enum Mode: Equatable, Hashable, Codable {
case `default`
case filtered(name: String, limitObjectIds: [String])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ final class GlobalSearchViewModel: ObservableObject {
private var globalSearchDataBuilder: any GlobalSearchDataBuilderProtocol
@Injected(\.defaultObjectCreationService)
private var defaultObjectCreationService: any DefaultObjectCreationServiceProtocol
@Injected(\.globalSearchSavedStatesService)
private var globalSearchSavedStatesService: any GlobalSearchSavedStatesServiceProtocol

private let moduleData: GlobalSearchModuleData

Expand All @@ -22,6 +24,7 @@ final class GlobalSearchViewModel: ObservableObject {

init(data: GlobalSearchModuleData) {
self.moduleData = data
self.restoreState()
}

func onAppear() {
Expand Down Expand Up @@ -68,6 +71,7 @@ final class GlobalSearchViewModel: ObservableObject {
}

func onSearchTextChanged() {
storeState()
AnytypeAnalytics.instance().logSearchInput(spaceId: moduleData.spaceId)
}

Expand All @@ -88,6 +92,7 @@ final class GlobalSearchViewModel: ObservableObject {
searchText: "",
mode: .filtered(name: name, limitObjectIds: data.relatedLinks)
)
storeState()
modeChanged = true
AnytypeAnalytics.instance().logSearchBacklink(spaceId: moduleData.spaceId)
}
Expand All @@ -97,6 +102,7 @@ final class GlobalSearchViewModel: ObservableObject {
searchText: "",
mode: .default
)
storeState()
modeChanged = true
}

Expand Down Expand Up @@ -139,4 +145,14 @@ final class GlobalSearchViewModel: ObservableObject {
modeChanged = false
return false
}

private func restoreState() {
let restoredState = globalSearchSavedStatesService.restoreState(for: moduleData.spaceId)
guard let restoredState else { return }
state = restoredState
}

private func storeState() {
globalSearchSavedStatesService.storeState(state, spaceId: moduleData.spaceId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Services
import AnytypeCore

protocol GlobalSearchSavedStatesServiceProtocol: AnyObject {
func storeState(_ state: GlobalSearchState, spaceId: String)
func restoreState(for spaceId: String) -> GlobalSearchState?
func clear()
}

final class GlobalSearchSavedStatesService: GlobalSearchSavedStatesServiceProtocol {

// [SpaceId : GlobalSearchState]
@UserDefault("UserData.GlobalSearchSavedStates", defaultValue: [:])
private var spacesSearchStates: [String: GlobalSearchState]

// MARK: - GlobalSearchSavedStatesServiceProtocol

func restoreState(for spaceId: String) -> GlobalSearchState? {
spacesSearchStates[spaceId]
}

func storeState(_ state: GlobalSearchState, spaceId: String) {
spacesSearchStates[spaceId] = state
}

func clear() {
spacesSearchStates.removeAll()
}
}
4 changes: 4 additions & 0 deletions Anytype/Sources/ServiceLayer/ServicesDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,8 @@ extension Container {
var participantSubscription: ParameterFactory<String, any ParticipantsSubscriptionProtocol> {
self { Container.shared.participantSubscriptionProvider().subscription(spaceId: $0) }
}

var globalSearchSavedStatesService: Factory<any GlobalSearchSavedStatesServiceProtocol> {
self { GlobalSearchSavedStatesService() }.shared
}
}
Loading