Skip to content

Commit

Permalink
Swipe to delete recent search (#703)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0b5a75e)
  • Loading branch information
Sjmarf authored and boscojwho committed Nov 23, 2023
1 parent d596d0b commit 47ef2af
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
16 changes: 12 additions & 4 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@
CD18DC6F2A5209C3002C56BC /* MarkPrivateMessageAsReadRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD18DC6E2A5209C3002C56BC /* MarkPrivateMessageAsReadRequest.swift */; };
CD18DC732A522A7C002C56BC /* CreatePrivateMessageRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD18DC722A522A7C002C56BC /* CreatePrivateMessageRequest.swift */; };
CD2053102AC878B50000AA38 /* UpdatedTimestampView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD20530F2AC878B50000AA38 /* UpdatedTimestampView.swift */; };
CD2053122ACB72190000AA38 /* AccountTransitionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2053112ACB72190000AA38 /* AccountTransitionView.swift */; };
CD2053142ACBAF150000AA38 /* AvatarType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2053132ACBAF150000AA38 /* AvatarType.swift */; };
CD2053172ACBBB5A0000AA38 /* DefaultAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2053162ACBBB5A0000AA38 /* DefaultAvatarView.swift */; };
CD2053122ACB72190000AA38 /* AccountTransitionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2053112ACB72190000AA38 /* AccountTransitionView.swift */; };
CD2BD6782A79F55800ECFF89 /* ImageSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2BD6772A79F55800ECFF89 /* ImageSize.swift */; };
CD2E182B2A3B708500224F8A /* Settings Options.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2E182A2A3B708500224F8A /* Settings Options.swift */; };
CD309C462A93FBD300988F95 /* Logo View.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD309C452A93FBD300988F95 /* Logo View.swift */; };
Expand Down Expand Up @@ -818,9 +818,9 @@
CD18DC6E2A5209C3002C56BC /* MarkPrivateMessageAsReadRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkPrivateMessageAsReadRequest.swift; sourceTree = "<group>"; };
CD18DC722A522A7C002C56BC /* CreatePrivateMessageRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreatePrivateMessageRequest.swift; sourceTree = "<group>"; };
CD20530F2AC878B50000AA38 /* UpdatedTimestampView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatedTimestampView.swift; sourceTree = "<group>"; };
CD2053112ACB72190000AA38 /* AccountTransitionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountTransitionView.swift; sourceTree = "<group>"; };
CD2053132ACBAF150000AA38 /* AvatarType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarType.swift; sourceTree = "<group>"; };
CD2053162ACBBB5A0000AA38 /* DefaultAvatarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultAvatarView.swift; sourceTree = "<group>"; };
CD2053112ACB72190000AA38 /* AccountTransitionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountTransitionView.swift; sourceTree = "<group>"; };
CD2BD6772A79F55800ECFF89 /* ImageSize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageSize.swift; sourceTree = "<group>"; };
CD2E182A2A3B708500224F8A /* Settings Options.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Settings Options.swift"; sourceTree = "<group>"; };
CD309C452A93FBD300988F95 /* Logo View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Logo View.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1098,6 +1098,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 @@ -1960,8 +1969,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 47ef2af

Please sign in to comment.