Skip to content

Commit d00bc83

Browse files
authored
Lemmyverse.link support (#1876)
1 parent 99be6de commit d00bc83

File tree

6 files changed

+121
-29
lines changed

6 files changed

+121
-29
lines changed

Mlem/App/Utility/Extensions/Content Models/Sharable+Extensions.swift

+7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import MlemMiddleware
1010
import SwiftUI
1111

1212
extension Sharable {
13+
var lemmyverseUrl: URL? {
14+
(URL(string: "https://lemmyverse.link/")?
15+
.appendingPathComponent(actorId.host)
16+
.appendingPathComponent(actorId.url.path()))
17+
}
18+
1319
func shareAction(navigation: NavigationLayer?) -> BasicAction {
1420
.init(id: "share\(actorId)", appearance: .share(), callback: {
1521
let url: URL? = switch Settings.main.linkSharingMode {
1622
case .myInstance: self.url()
1723
case .originalInstance: self.actorId.url
24+
case .lemmyverse: self.lemmyverseUrl
1825
case .askEveryTime: nil
1926
}
2027
if let url, let navigation {

Mlem/App/Views/Root/Tabs/Settings/LinkSettingsView.swift

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct LinkSettingsView: View {
8282
switch linkSharingMode {
8383
case .myInstance: "My Instance"
8484
case .originalInstance: "Original Instance"
85+
case .lemmyverse: "Universal"
8586
case .askEveryTime: "Ask Every Time"
8687
}
8788
}

Mlem/App/Views/Root/Tabs/Settings/SharingLinksSettingsView.swift

+68-26
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,81 @@ import SwiftUI
99

1010
struct SharingLinksSettingsView: View {
1111
@Setting(\.linkSharingMode) var linkSharingMode
12-
12+
@Setting(\.showSettingsIcons) var showSettingsIcons
13+
1314
var body: some View {
1415
Form {
15-
Section("Share links using...") {
16-
Picker("Share links using...", selection: $linkSharingMode) {
17-
ForEach(LinkSharingMode.allCases, id: \.self) { mode in
18-
Label(String(localized: mode.label), systemImage: mode.systemImage)
19-
}
20-
}
21-
.labelsHidden()
22-
.pickerStyle(.inline)
23-
}
16+
SettingsHeaderView(
17+
title: "Share Links",
18+
// swiftlint:disable:next line_length
19+
description: "In the Fediverse, many different links can point to the same piece of content. Choose which site to use when sharing content.",
20+
systemImage: Icons.share
21+
)
22+
.tint(.themedColorfulAccent(3))
23+
24+
pickerItemView(
25+
mode: .myInstance,
26+
title: "My Instance",
27+
description: "Share links using the instance you are currently connected to.",
28+
systemImage: Icons.instance
29+
)
30+
31+
pickerItemView(
32+
mode: .originalInstance,
33+
title: "Original Instance",
34+
description: "Share links using the instance that the content originated from.",
35+
systemImage: "signature"
36+
)
37+
38+
pickerItemView(
39+
mode: .lemmyverse,
40+
title: "Universal Link",
41+
description: "Share links using \("https://lemmyverse.link"). When someone opens the link, they can choose which instance to use.",
42+
systemImage: "globe"
43+
)
44+
pickerItemView(
45+
mode: .askEveryTime,
46+
title: "Ask Every Time",
47+
description: "Every time I share a link, show a popup asking which instance to use.",
48+
systemImage: "questionmark.circle"
49+
)
2450
}
51+
.contentMargins(.top, 16)
2552
.labelStyle(.conditional)
53+
.animation(.easeInOut(duration: 0.1), value: linkSharingMode)
2654
}
27-
}
28-
29-
enum LinkSharingMode: String, Codable, CaseIterable {
30-
case myInstance, originalInstance, askEveryTime
3155

32-
var label: LocalizedStringResource {
33-
switch self {
34-
case .myInstance: "My Instance"
35-
case .originalInstance: "Original Instance"
36-
case .askEveryTime: "Ask Every Time"
56+
@ViewBuilder
57+
func pickerItemView(
58+
mode: LinkSharingMode,
59+
title: LocalizedStringResource,
60+
description: LocalizedStringResource,
61+
systemImage: String
62+
) -> some View {
63+
HStack(alignment: .top) {
64+
if showSettingsIcons {
65+
Image(systemName: systemImage)
66+
.foregroundStyle(.themedAccent)
67+
.frame(width: 30)
68+
.padding(.top, 2)
69+
}
70+
VStack(alignment: .leading) {
71+
Text(title)
72+
Text(description)
73+
.font(.footnote)
74+
.foregroundStyle(.themedSecondary)
75+
}
76+
.frame(maxWidth: .infinity, alignment: .leading)
77+
Checkbox(isOn: linkSharingMode == mode)
3778
}
38-
}
39-
40-
var systemImage: String {
41-
switch self {
42-
case .myInstance: Icons.instance
43-
case .originalInstance: "signature"
44-
case .askEveryTime: "questionmark.circle"
79+
.contentShape(.rect)
80+
.onTapGesture {
81+
linkSharingMode = mode
4582
}
83+
.listRowInsets(.init(top: 10, leading: showSettingsIcons ? 10 : 16, bottom: 10, trailing: 16))
4684
}
4785
}
86+
87+
enum LinkSharingMode: String, Codable, CaseIterable {
88+
case myInstance, originalInstance, lemmyverse, askEveryTime
89+
}

Mlem/App/Views/Shared/HandleLemmyLinksModifier.swift

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ struct HandleLemmyLinksModifier: ViewModifier {
6161
return .handled
6262
}
6363

64+
// Handles https://lemmyverse.link
65+
if host == "lemmyverse.link", url.pathComponents.count > 3 {
66+
var components = URLComponents()
67+
components.scheme = "https"
68+
components.host = url.pathComponents[1]
69+
components.path = "/" + url.pathComponents.dropFirst(2).joined(separator: "/")
70+
if let newUrl = components.url, interpretLemmyUrlPath(url: newUrl) { return .handled }
71+
}
72+
6473
// If the link is in our Lemmy domain list, push a page to the NavigationStack straight away
6574
if isLemmyHost(host), interpretLemmyUrlPath(url: url) {
6675
return .handled

Mlem/App/Views/Shared/ShareInstancePickerView.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ struct ShareInstancePickerView: View {
3030
instanceTargetRow(entity.api.host, label: "My Instance", url: entity.url())
3131
Divider()
3232
instanceTargetRow(entity.actorId.host, label: "Original Instance", url: entity.actorId.url)
33+
if let lemmyverseUrl = entity.lemmyverseUrl {
34+
Divider()
35+
instanceTargetRow("lemmyverse.link", label: "Universal", url: lemmyverseUrl)
36+
}
3337
}
3438
.frame(maxWidth: .infinity)
3539
.background(.themedSecondaryGroupedBackground, in: .rect(cornerRadius: 16))
@@ -58,7 +62,14 @@ struct ShareInstancePickerView: View {
5862
}
5963
} label: {
6064
HStack(spacing: 16) {
61-
CircleCroppedImageView(url: faviconUrl(for: url), frame: 42, fallback: .instanceAvatar)
65+
if host == "lemmyverse.link" {
66+
Image(systemName: "globe")
67+
.foregroundStyle(.themedAccent)
68+
.frame(width: 42, height: 42)
69+
.background(.themedAccent.opacity(0.2), in: .circle)
70+
} else {
71+
CircleCroppedImageView(url: faviconUrl(for: url), frame: 42, fallback: .instanceAvatar)
72+
}
6273
VStack(alignment: .leading, spacing: 5) {
6374
Text(host)
6475
.foregroundStyle(.themedPrimary)
@@ -72,7 +83,7 @@ struct ShareInstancePickerView: View {
7283
.padding(.vertical, 12)
7384
}
7485
}
75-
86+
7687
func faviconUrl(for instanceUrl: URL) -> URL? {
7788
guard let host = instanceUrl.host() else { return nil }
7889
let summary = MlemStats.main.instances?.first(where: { $0.host == host })

Mlem/Localizable.xcstrings

+23-1
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@
691691
}
692692
}
693693
}
694+
},
695+
"Add an image..." : {
696+
694697
},
695698
"Add Guest" : {
696699
"localizations" : {
@@ -2933,6 +2936,9 @@
29332936
}
29342937
}
29352938
}
2939+
},
2940+
"Every time I share a link, show a popup asking which instance to use." : {
2941+
29362942
},
29372943
"example.com" : {
29382944
"localizations" : {
@@ -3732,6 +3738,9 @@
37323738
}
37333739
}
37343740
}
3741+
},
3742+
"In the Fediverse, many different links can point to the same piece of content. Choose which site to use when sharing content." : {
3743+
37353744
},
37363745
"In-App" : {
37373746
"extractionState" : "stale",
@@ -6743,7 +6752,13 @@
67436752
"Share Links" : {
67446753

67456754
},
6746-
"Share links using..." : {
6755+
"Share links using %@. When someone opens the link, they can choose which instance to use." : {
6756+
6757+
},
6758+
"Share links using the instance that the content originated from." : {
6759+
6760+
},
6761+
"Share links using the instance you are currently connected to." : {
67476762

67486763
},
67496764
"Share using..." : {
@@ -8253,6 +8268,12 @@
82538268
}
82548269
}
82558270
}
8271+
},
8272+
"Universal" : {
8273+
8274+
},
8275+
"Universal Link" : {
8276+
82568277
},
82578278
"Unknown" : {
82588279
"localizations" : {
@@ -8365,6 +8386,7 @@
83658386
}
83668387
},
83678388
"Upload an image..." : {
8389+
"extractionState" : "stale",
83688390
"localizations" : {
83698391
"fr" : {
83708392
"stringUnit" : {

0 commit comments

Comments
 (0)