mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 10:33:45 +00:00
quick and dirty /join command (#3288)
Co-authored-by: Mauro Romito <mauro.romito@element.io> Co-authored-by: Mauro <34335419+Velin92@users.noreply.github.com>
This commit is contained in:
@@ -72,6 +72,7 @@ enum TimelineViewAction {
|
|||||||
case hasSwitchedTimeline
|
case hasSwitchedTimeline
|
||||||
|
|
||||||
case hasScrolled(direction: ScrollDirection)
|
case hasScrolled(direction: ScrollDirection)
|
||||||
|
case setOpenURLAction(OpenURLAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum TimelineComposerAction {
|
enum TimelineComposerAction {
|
||||||
@@ -101,6 +102,9 @@ struct TimelineViewState: BindableState {
|
|||||||
// It's updated from the room info, so it's faster than using the timeline
|
// It's updated from the room info, so it's faster than using the timeline
|
||||||
var pinnedEventIDs: Set<String> = []
|
var pinnedEventIDs: Set<String> = []
|
||||||
|
|
||||||
|
/// an openURL closure which opens URLs first using the App's environment rather than skipping out to external apps
|
||||||
|
var openURL: OpenURLAction?
|
||||||
|
|
||||||
var bindings: TimelineViewStateBindings
|
var bindings: TimelineViewStateBindings
|
||||||
|
|
||||||
/// A closure providing the associated audio player state for an item in the timeline.
|
/// A closure providing the associated audio player state for an item in the timeline.
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
import Algorithms
|
import Algorithms
|
||||||
import Combine
|
import Combine
|
||||||
|
import MatrixRustSDK
|
||||||
import OrderedCollections
|
import OrderedCollections
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
@@ -167,6 +168,8 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
|
|||||||
Task { state.timelineViewState.isSwitchingTimelines = false }
|
Task { state.timelineViewState.isSwitchingTimelines = false }
|
||||||
case let .hasScrolled(direction):
|
case let .hasScrolled(direction):
|
||||||
actionsSubject.send(.hasScrolled(direction: direction))
|
actionsSubject.send(.hasScrolled(direction: direction))
|
||||||
|
case .setOpenURLAction(let action):
|
||||||
|
state.openURL = action
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -561,6 +564,26 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func slashCommand(message: String) -> SlashCommand? {
|
||||||
|
for command in SlashCommand.allCases {
|
||||||
|
if message.starts(with: command.rawValue) {
|
||||||
|
return command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleJoinCommand(message: String) {
|
||||||
|
guard let alias = String(message.dropFirst(SlashCommand.join.rawValue.count))
|
||||||
|
.components(separatedBy: .whitespacesAndNewlines)
|
||||||
|
.first,
|
||||||
|
let urlString = try? matrixToRoomAliasPermalink(roomAlias: alias),
|
||||||
|
let url = URL(string: urlString) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
state.openURL?(url)
|
||||||
|
}
|
||||||
|
|
||||||
private func sendCurrentMessage(_ message: String, html: String?, mode: ComposerMode, intentionalMentions: IntentionalMentions) async {
|
private func sendCurrentMessage(_ message: String, html: String?, mode: ComposerMode, intentionalMentions: IntentionalMentions) async {
|
||||||
guard !message.isEmpty else {
|
guard !message.isEmpty else {
|
||||||
fatalError("This message should never be empty")
|
fatalError("This message should never be empty")
|
||||||
@@ -580,9 +603,14 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
|
|||||||
html: html,
|
html: html,
|
||||||
intentionalMentions: intentionalMentions)
|
intentionalMentions: intentionalMentions)
|
||||||
case .default:
|
case .default:
|
||||||
await timelineController.sendMessage(message,
|
switch slashCommand(message: message) {
|
||||||
html: html,
|
case .join:
|
||||||
intentionalMentions: intentionalMentions)
|
handleJoinCommand(message: message)
|
||||||
|
case .none:
|
||||||
|
await timelineController.sendMessage(message,
|
||||||
|
html: html,
|
||||||
|
intentionalMentions: intentionalMentions)
|
||||||
|
}
|
||||||
case .recordVoiceMessage, .previewVoiceMessage:
|
case .recordVoiceMessage, .previewVoiceMessage:
|
||||||
fatalError("invalid composer mode.")
|
fatalError("invalid composer mode.")
|
||||||
}
|
}
|
||||||
@@ -862,3 +890,7 @@ extension EnvironmentValues {
|
|||||||
set { self[FocussedEventID.self] = newValue }
|
set { self[FocussedEventID.self] = newValue }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum SlashCommand: String, CaseIterable {
|
||||||
|
case join = "/join "
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,11 +11,16 @@ import WysiwygComposer
|
|||||||
/// A table view wrapper that displays the timeline of a room.
|
/// A table view wrapper that displays the timeline of a room.
|
||||||
struct TimelineView: UIViewControllerRepresentable {
|
struct TimelineView: UIViewControllerRepresentable {
|
||||||
@EnvironmentObject private var viewModelContext: TimelineViewModel.Context
|
@EnvironmentObject private var viewModelContext: TimelineViewModel.Context
|
||||||
|
@Environment(\.openURL) var openURL
|
||||||
|
|
||||||
func makeUIViewController(context: Context) -> TimelineTableViewController {
|
func makeUIViewController(context: Context) -> TimelineTableViewController {
|
||||||
let tableViewController = TimelineTableViewController(coordinator: context.coordinator,
|
let tableViewController = TimelineTableViewController(coordinator: context.coordinator,
|
||||||
isScrolledToBottom: $viewModelContext.isScrolledToBottom,
|
isScrolledToBottom: $viewModelContext.isScrolledToBottom,
|
||||||
scrollToBottomPublisher: viewModelContext.viewState.timelineViewState.scrollToBottomPublisher)
|
scrollToBottomPublisher: viewModelContext.viewState.timelineViewState.scrollToBottomPublisher)
|
||||||
|
// Needs to be dispatched on main asynchronously otherwise we get a runtime warning
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
viewModelContext.send(viewAction: .setOpenURLAction(openURL))
|
||||||
|
}
|
||||||
return tableViewController
|
return tableViewController
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user