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

Add "Send to Lemmy User" action to share sheet #1710

Merged
merged 2 commits into from
Feb 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ import MlemMiddleware

extension ActorIdentifiable {
func shareAction() -> ShareAction {
.init(id: "share\(actorId)", url: actorId.url)
.init(id: "share\(actorId)", url: actorId.url, actions: [sendLinkInPrivateMessageAction()])
}

func sendLinkInPrivateMessageAction() -> BasicAction {
.init(
id: "sendLinkInPrivateMessage\(actorId)",
appearance: .init(
label: "Send to Lemmy User",
color: Palette.main.accent,
icon: Icons.personCircle
),
callback: {
NavigationModel.main.openSheet(.personPicker(callback: { person, navigation in
navigation.push(
.messageFeed(person, messageContent: String(describing: actorId), focusTextField: true)
)
}))
}
)
}

func openInstanceAction(navigation: NavigationLayer?) -> BasicAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ extension Post1Providing {

// Overrides the `ActorIdentifiable+Extensions` implementation
func shareAction() -> ShareAction {
.init(id: "share\(actorId)", url: actorId.url, actions: [crossPostAction()])
.init(
id: "share\(actorId)",
url: actorId.url,
actions: [crossPostAction(), sendLinkInPrivateMessageAction()]
)
}

func crossPostAction() -> BasicAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import MlemMiddleware
import SwiftUI

extension MessageFeedView {
var shouldDelayBecomeFirstResponder: Bool {
// Only delay the keyboard opening if being pushed onto the navigation stack rather than opening in sheet
!navigation.isAtRoot
}

func sendMessage(_ scrollProxy: ScrollViewProxy) async {
do {
guard let person = person.wrappedValue as? any Person, !textView.text.isEmpty else { return }
Expand Down
25 changes: 18 additions & 7 deletions Mlem/App/Views/Pages/MessageFeedView/MessageFeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import MlemMiddleware
import SwiftUI

// swiftlint:disable:next type_body_length
struct MessageFeedView: View {
@Environment(AppState.self) var appState
@Environment(NavigationLayer.self) var navigation
Expand All @@ -20,15 +21,18 @@ struct MessageFeedView: View {

let timer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()

init(person: AnyPerson, focusTextField: Bool, editing: (any Message)?) {
init(
person: AnyPerson,
messageContent: String = "",
focusTextField: Bool,
editing: (any Message)?
) {
self.person = person
self.focusTextField = focusTextField
self._editing = .init(wrappedValue: editing)
if let editing {
let textView = UITextView()
textView.text = editing.content
_textView = .init(wrappedValue: textView)
}
let textView = UITextView()
textView.text = editing?.content ?? messageContent
_textView = .init(wrappedValue: textView)
}

@State var feedLoader: MessageFeedLoader?
Expand Down Expand Up @@ -235,7 +239,7 @@ struct MessageFeedView: View {
bottom: Constants.main.standardSpacing,
right: Constants.main.standardSpacing
),
firstResponder: focusTextField,
firstResponder: focusTextField && !shouldDelayBecomeFirstResponder,
sizingOffset: 5,
content: {
MarkdownEditorToolbarView(
Expand All @@ -249,6 +253,13 @@ struct MessageFeedView: View {
maxWidth: .infinity,
minHeight: minTextEditorHeight
)
.onAppear {
if focusTextField, shouldDelayBecomeFirstResponder {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
textView.becomeFirstResponder()
}
}
}
}

@ViewBuilder
Expand Down
4 changes: 4 additions & 0 deletions Mlem/App/Views/Shared/Navigation/NavigationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class NavigationLayer: Identifiable {
@MainActor
func push(_ page: NavigationPage) {
if hasNavigationStack {
// This prevents keyboard animation glitches when navigating whilst the keyboard is open
UIApplication.shared.firstKeyWindow?.endEditing(true)
path.append(page)
} else {
openSheet(page)
Expand Down Expand Up @@ -99,6 +101,8 @@ class NavigationLayer: Identifiable {
path.removeAll()
}

var isAtRoot: Bool { path.isEmpty }

/// Open a new sheet, optionally with navigation enabled. If `nil` is specified for `hasNavigationStack`, the value of `page.hasNavigationStack` will be used.
@MainActor
func openSheet(_ page: NavigationPage, hasNavigationStack: Bool? = nil) {
Expand Down
9 changes: 7 additions & 2 deletions Mlem/App/Views/Shared/Navigation/NavigationPage+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,13 @@ extension NavigationPage {
AdvancedSortView(selectedSort: sort.wrappedValue)
case let .votesList(target):
VotesListView(target: target)
case let .messageFeed(person, focusTextField: focusTextField, editing: editing):
MessageFeedView(person: person, focusTextField: focusTextField, editing: editing?.wrappedValue)
case let .messageFeed(person, messageContent: messageContent, focusTextField: focusTextField, editing: editing):
MessageFeedView(
person: person,
messageContent: messageContent,
focusTextField: focusTextField,
editing: editing?.wrappedValue
)
case let .modlog(target):
ModlogView(initialTarget: target)
case let .denyApplication(application):
Expand Down
10 changes: 8 additions & 2 deletions Mlem/App/Views/Shared/Navigation/NavigationPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum NavigationPage: Hashable {
case person(_ person: AnyPerson, visitContext: VisitHistory.VisitContext)
case instance(_ instance: InstanceHashWrapper, visitContext: VisitHistory.VisitContext)
case instanceOpinionList(instance: InstanceHashWrapper, opinionType: FediseerOpinionType, data: FediseerData)
case messageFeed(_ person: AnyPerson, focusTextField: Bool, editing: MessageHashWrapper?)
case messageFeed(_ person: AnyPerson, messageContent: String, focusTextField: Bool, editing: MessageHashWrapper?)
case fediseerInfo
case externalApiInfo(api: ApiClient, actorId: ActorIdentifier)
case imageViewer(_ url: URL)
Expand Down Expand Up @@ -133,14 +133,20 @@ enum NavigationPage: Hashable {

static func messageFeed(
_ person: any PersonStubProviding,
messageContent: String = "",
focusTextField: Bool = false,
editing: (any Message1Providing)? = nil
) -> NavigationPage {
var editingWrapper: MessageHashWrapper?
if let editing {
editingWrapper = .init(wrappedValue: editing)
}
return messageFeed(.init(person), focusTextField: focusTextField, editing: editingWrapper)
return messageFeed(
.init(person),
messageContent: messageContent,
focusTextField: focusTextField,
editing: editingWrapper
)
}

static func instance(
Expand Down
3 changes: 3 additions & 0 deletions Mlem/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,9 @@
},
"Send Notifications to Email" : {

},
"Send to Lemmy User" : {

},
"Sent %@" : {

Expand Down