Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Improve handling of link taps
Browse files Browse the repository at this point in the history
  • Loading branch information
bfollington committed Oct 9, 2023
1 parent ea690ea commit 5a5bdcc
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct ProfileStatisticView: View {
struct RecentTabView: View {
var state: UserProfileDetailModel
var send: (UserProfileDetailAction) -> Void
var onNavigateToNote: (Slashlink) -> Void
var action: (Slashlink) -> Void
var onLink: (Slashlink, SubSlashlinkLink) -> Void

var body: some View {
if let user = state.user {
Expand All @@ -38,7 +39,8 @@ struct RecentTabView: View {
author: user,
entry: entry
),
action: { address, _ in onNavigateToNote(address) }
action: { address, _ in action(address) },
onLink: { link in onLink(entry.address, link) }
)

Divider()
Expand Down Expand Up @@ -98,6 +100,7 @@ struct UserProfileView: View {

var onNavigateToNote: (Slashlink) -> Void
var onNavigateToUser: (UserProfile) -> Void
var onNavigateToLink: (Slashlink, SubSlashlinkLink) -> Void

var onProfileAction: (UserProfile, UserProfileAction) -> Void
var onRefresh: () async -> Void
Expand All @@ -115,7 +118,8 @@ struct UserProfileView: View {
view: RecentTabView(
state: state,
send: send,
onNavigateToNote: onNavigateToNote
action: onNavigateToNote,
onLink: onNavigateToLink
)
)
}
Expand Down Expand Up @@ -287,6 +291,7 @@ struct UserProfileView_Previews: PreviewProvider {
),
onNavigateToNote: { _ in print("navigate to note") },
onNavigateToUser: { _ in print("navigate to user") },
onNavigateToLink: { _, _ in print("navigate to link") },
onProfileAction: { user, action in print("profile action") },
onRefresh: { }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private struct StoryEntryUserDetailsView: View {
struct StoryEntryView: View {
var story: StoryEntry
var action: (Slashlink, String) -> Void
var onLink: (SubSlashlinkLink) -> Void
var sharedNote: String {
"""
\(story.entry.excerpt)
Expand Down Expand Up @@ -76,6 +77,16 @@ struct StoryEntryView: View {
}
)
.padding([.leading, .trailing], AppTheme.padding)
// Handle tapped slashlinks in the preview
.environment(\.openURL, OpenURLAction { url in

guard let subslashlink = url.toSubSlashlinkURL() else {
return .systemAction
}

onLink(subslashlink)
return .handled
})

// MARK: footer
HStack(alignment: .center, spacing: AppTheme.unit) {
Expand Down Expand Up @@ -135,7 +146,8 @@ struct StoryPlainView_Previews: PreviewProvider {
)
)
),
action: { _, _ in }
action: { _, _ in },
onLink: { _ in }
)
}
}
9 changes: 0 additions & 9 deletions xcode/Subconscious/Shared/Components/Common/SubtextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ struct SubtextView: View {
if renderable.entries.isEmpty ||
!shouldReplaceBlockWithTransclude(block: renderable.block) {
Text(Self.renderer.render(renderable.block.description))
// Handle tapped slashlinks in the preview
.environment(\.openURL, OpenURLAction { url in
guard let subslashlink = url.toSubSlashlinkURL() else {
return .systemAction
}

onViewSlashlink(subslashlink.slashlink)
return .handled
})
}

ForEach(renderable.entries, id: \.self) { entry in
Expand Down
93 changes: 73 additions & 20 deletions xcode/Subconscious/Shared/Components/Detail/DetailStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ enum DetailStackAction: Hashable {
/// If slashlink does not have a peer part, this will
/// request an editor detail.
case findAndPushDetail(
address: Slashlink
)

case findAndPushLinkDetail(
address: Slashlink,
link: SubSlashlinkLink
)
Expand All @@ -119,7 +123,7 @@ enum DetailStackAction: Hashable {
case pushDetail(MemoDetailDescription)

case requestOurProfileDetail
case pushOurProfileDetail(UserProfile)
case pushOurProfileDetail
case failPushDetail(_ message: String)

case pushRandomDetail(autofocus: Bool)
Expand Down Expand Up @@ -177,13 +181,19 @@ struct DetailStackModel: Hashable, ModelProtocol {
environment: environment,
details: details
)
case let .findAndPushDetail(address, link):
return findAndPushDetail(
case let .findAndPushLinkDetail(address, link):
return findAndPushLinkDetail(
state: state,
environment: environment,
address: address,
link: link
)
case let .findAndPushDetail(address):
return findAndPushDetail(
state: state,
environment: environment,
address: address
)
case let .findAndPushMemoEditorDetail(slug, fallback):
return findAndPushMemoEditorDetail(
state: state,
Expand Down Expand Up @@ -220,11 +230,10 @@ struct DetailStackModel: Hashable, ModelProtocol {
state: state,
environment: environment
)
case .pushOurProfileDetail(let user):
case .pushOurProfileDetail:
return pushOurProfileDetail(
state: state,
environment: environment,
user: user
environment: environment
)
case let .requestDeleteMemo(address):
return requestDeleteMemo(
Expand Down Expand Up @@ -282,8 +291,8 @@ struct DetailStackModel: Hashable, ModelProtocol {
model.details = details
return Update(state: model)
}

static func findAndPushDetail(
static func findAndPushLinkDetail(
state: Self,
environment: Environment,
address: Slashlink,
Expand All @@ -300,15 +309,56 @@ struct DetailStackModel: Hashable, ModelProtocol {
}
return link.slashlink.rebaseIfNeeded(petname: basePetname)
}

let fx: Fx<DetailStackAction> = Future.detached {
let identity = try await environment.noosphere.identity()
let did = try await environment.noosphere.resolve(peer: slashlink.peer)
let following = await environment.addressBook.followingStatus(
did: did,
expectedName: address.petname?.leaf
)

// Is this us? Trim off the peer
guard did != identity else {
return .findAndPushDetail(
address: Slashlink(slug: slashlink.slug)
)
}

switch following {
// Use the shortest name we know for this user
case .following(let name):
return .findAndPushDetail(
address: Slashlink(
petname: name.toPetname(),
slug: slashlink.slug
)
)
case .notFollowing:
return .findAndPushDetail(address: slashlink)
}
}
.recover { error in
DetailStackAction.failPushDetail("Could not resolve link: \(error)")
}
.eraseToAnyPublisher()

return Update(state: state, fx: fx)
}

static func findAndPushDetail(
state: Self,
environment: Environment,
address: Slashlink
) -> Update<Self> {
// Intercept profile visits and use the correct view
guard !slashlink.slug.isProfile else {
guard !address.slug.isProfile else {
return update(
state: state,
action: .pushDetail(
.profile(
UserProfileDetailDescription(
address: slashlink
address: address
)
)
),
Expand All @@ -318,12 +368,12 @@ struct DetailStackModel: Hashable, ModelProtocol {

// If slashlink pointing to our sphere, dispatch findAndPushEditDetail
// to find in local or sphere content and then push editor detail.
guard slashlink.peer != nil else {
guard address.peer != nil else {
return update(
state: state,
action: .findAndPushMemoEditorDetail(
slug: slashlink.toSlug(),
fallback: link.fallback
slug: address.toSlug(),
fallback: ""
),
environment: environment
)
Expand All @@ -336,7 +386,7 @@ struct DetailStackModel: Hashable, ModelProtocol {
action: .pushDetail(
.viewer(
MemoViewerDetailDescription(
address: slashlink
address: address
)
)
),
Expand Down Expand Up @@ -429,7 +479,7 @@ struct DetailStackModel: Hashable, ModelProtocol {
let fx: Fx<Action> = environment.userProfile
.loadOurProfileFromMemoPublisher()
.map { user in
Action.pushOurProfileDetail(user)
Action.pushOurProfileDetail
}
.recover { error in
Action.failPushDetail(error.localizedDescription)
Expand All @@ -441,12 +491,10 @@ struct DetailStackModel: Hashable, ModelProtocol {

static func pushOurProfileDetail(
state: Self,
environment: Environment,
user: UserProfile
environment: Environment
) -> Update<Self> {
let detail = UserProfileDetailDescription(
address: Slashlink.ourProfile,
user: user,
// Focus following list by default
// We can already see our recent notes in our notebook so no
// point showing it again
Expand Down Expand Up @@ -603,7 +651,7 @@ extension DetailStackAction {
case let .requestDetail(detail):
return .pushDetail(detail)
case let .requestFindLinkDetail(link):
return .findAndPushDetail(
return .findAndPushLinkDetail(
address: Slashlink.ourProfile,
link: link
)
Expand All @@ -623,7 +671,7 @@ extension DetailStackAction {
case let .requestDetail(detail):
return .pushDetail(detail)
case let .requestFindLinkDetail(address, link):
return .findAndPushDetail(
return .findAndPushLinkDetail(
address: address,
link: link
)
Expand All @@ -643,6 +691,11 @@ extension DetailStackAction {
switch action {
case let .requestDetail(detail):
return .pushDetail(detail)
case let .requestFindLinkDetail(address, link):
return .findAndPushLinkDetail(
address: address,
link: link
)
case let .requestNavigateToProfile(user):
let user = Func.run {
switch (user.category, user.ourFollowStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct UserProfileDetailView: View {
notify(.requestNavigateToProfile(user))
}

func onNavigateToLink(address: Slashlink, link: SubSlashlinkLink) {
notify(.requestFindLinkDetail(address: address, link: link))
}

func onProfileAction(user: UserProfile, action: UserProfileAction) {
switch (action) {
case .requestFollow:
Expand All @@ -54,6 +58,7 @@ struct UserProfileDetailView: View {
store: store,
onNavigateToNote: self.onNavigateToNote,
onNavigateToUser: self.onNavigateToUser,
onNavigateToLink: self.onNavigateToLink,
onProfileAction: self.onProfileAction,
onRefresh: {
app.send(.syncAll)
Expand Down Expand Up @@ -93,6 +98,10 @@ struct UserProfileDetailView: View {
enum UserProfileDetailNotification: Hashable {
case requestNavigateToProfile(_ user: UserProfile)
case requestDetail(MemoDetailDescription)
case requestFindLinkDetail(
address: Slashlink,
link: SubSlashlinkLink
)
}

extension UserProfileDetailAction {
Expand Down
13 changes: 12 additions & 1 deletion xcode/Subconscious/Shared/Components/Feed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ struct FeedNavigationView: View {
entry: entry
),
action: { address, _ in
store.send(.detailStack(.pushDetail(
store.send(.detailStack(
.pushDetail(
MemoDetailDescription.from(
address: address,
fallback: ""
)
)))
},
onLink: { link in
store.send(
.detailStack(
.findAndPushLinkDetail(
address: entry.address,
link: link
)
)
)
}
)

Expand Down

0 comments on commit 5a5bdcc

Please sign in to comment.