Skip to content

Commit

Permalink
Swipe to delete recent search (#703)
Browse files Browse the repository at this point in the history
Commit:
0b5a75e [0b5a75e]
  • Loading branch information
boscojwho committed Oct 11, 2023
1 parent 3189169 commit 16683a0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
12 changes: 10 additions & 2 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,15 @@
path = User;
sourceTree = "<group>";
};
030D00832AD0842900953B1D /* Results */ = {
isa = PBXGroup;
children = (
03EEEAF22AB8DCDF0087F8D8 /* CommunityResultView.swift */,
03B7AAF42ABEFA7A00068B23 /* UserResultView.swift */,
);
path = Results;
sourceTree = "<group>";
};
030E86422AC6F6CB000283A6 /* Search Bar */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1938,8 +1947,7 @@
03EC92942AC064AE007BBE7E /* SearchHomeView.swift */,
036ED3BB2ABF1058009664BC /* SearchModel.swift */,
03EEEAF62AB8ED3C0087F8D8 /* SearchTabPicker.swift */,
03EEEAF22AB8DCDF0087F8D8 /* CommunityResultView.swift */,
03B7AAF42ABEFA7A00068B23 /* UserResultView.swift */,
030D00832AD0842900953B1D /* Results */,
);
path = Search;
sourceTree = "<group>";
Expand Down
7 changes: 7 additions & 0 deletions Mlem/Extensions/Swipey Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,11 @@ extension View {
)
)
}

@ViewBuilder
func addSwipeyActions(_ configuration: SwipeConfiguration) -> some View {
modifier(
SwipeyView(configuration: configuration)
)
}
}
7 changes: 7 additions & 0 deletions Mlem/Models/Trackers/RecentSearchesTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class RecentSearchesTracker: ObservableObject {
saveRecentSearches(accountId: accountId)
}

func removeRecentSearch(_ item: AnyContentModel, accountId: String?) {
if let index = recentSearches.firstIndex(of: item) {
recentSearches.remove(at: index)
}
saveRecentSearches(accountId: accountId)
}

func clearRecentSearches(accountId: String?) {
recentSearches.removeAll()
saveRecentSearches(accountId: accountId)
Expand Down
22 changes: 20 additions & 2 deletions Mlem/Views/Tabs/Search/RecentSearchesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ struct RecentSearchesView: View {
@EnvironmentObject var recentSearchesTracker: RecentSearchesTracker
@StateObject var contentTracker: ContentTracker<AnyContentModel> = .init()

func deleteSwipeAction(_ item: AnyContentModel) -> SwipeAction {
return SwipeAction(
symbol: .init(emptyName: Icons.close, fillName: Icons.close),
color: .red,
action: {
recentSearchesTracker.removeRecentSearch(item, accountId: appState.currentActiveAccount?.stableIdString)
}
)
}

var body: some View {
Group {
if !recentSearchesTracker.recentSearches.isEmpty {
Expand Down Expand Up @@ -62,9 +72,17 @@ struct RecentSearchesView: View {
ForEach(contentTracker.items, id: \.uid) { contentModel in
Group {
if let community = contentModel.wrappedValue as? CommunityModel {
CommunityResultView(community: community, showTypeLabel: true)
CommunityResultView(
community: community,
showTypeLabel: true,
swipeActions: .init(trailingActions: [deleteSwipeAction(contentModel)])
)
} else if let user = contentModel.wrappedValue as? UserModel {
UserResultView(user: user, showTypeLabel: true)
UserResultView(
user: user,
showTypeLabel: true,
swipeActions: .init(trailingActions: [deleteSwipeAction(contentModel)])
)
}
}
.simultaneousGesture(TapGesture().onEnded {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ struct CommunityResultView: View {
@Dependency(\.hapticManager) var hapticManager

@EnvironmentObject var contentTracker: ContentTracker<AnyContentModel>

let community: CommunityModel
let showTypeLabel: Bool

var swipeActions: SwipeConfiguration?

var subscribeSwipeAction: SwipeAction {
let (emptySymbolName, fullSymbolName) = community.subscribed
? (Icons.unsubscribePerson, Icons.unsubscribePersonFill)
Expand Down Expand Up @@ -82,7 +84,6 @@ struct CommunityResultView: View {
.buttonStyle(.plain)
.padding(.vertical, 8)
.background(.background)
.addSwipeyActions(trailing: [subscribeSwipeAction])
.draggable(community.community.actorId) {
HStack {
AvatarView(community: community.community, avatarSize: 24)
Expand All @@ -101,5 +102,6 @@ struct CommunityResultView: View {
systemImage: community.subscribed ? Icons.unsubscribe : Icons.subscribe)
}
}
.addSwipeyActions(swipeActions ?? .init(trailingActions: [subscribeSwipeAction]))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ struct UserResultView: View {
@Dependency(\.hapticManager) var hapticManager

@EnvironmentObject var contentTracker: ContentTracker<AnyContentModel>

let user: UserModel
let showTypeLabel: Bool
var swipeActions: SwipeConfiguration?

var caption: String {
if let host = user.user.actorId.host {
Expand Down Expand Up @@ -87,5 +89,6 @@ struct UserResultView: View {
.background(.background)
.clipShape(RoundedRectangle(cornerRadius: 8))
}
.addSwipeyActions(swipeActions ?? .init())
}
}
2 changes: 1 addition & 1 deletion Mlem/Views/Tabs/Search/SearchTabPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct SearchTabPicker: View {
}
}
)
.animation(.spring(response: 0.15, dampingFraction: 0.825), value: selected)
.animation(.spring(response: 0.15, dampingFraction: 0.7), value: selected)
}
.buttonStyle(EmptyButtonStyle())
}
Expand Down

0 comments on commit 16683a0

Please sign in to comment.