WIP Status Editor

This commit is contained in:
Thomas Ricouard 2022-12-25 06:55:33 +01:00
parent 44d36c4cf0
commit 4be33b4f67
4 changed files with 85 additions and 6 deletions

View file

@ -26,6 +26,15 @@
"revision" : "965a7cbcbf094cbcf22b9251a2323bdc3432e171",
"version" : "1.1.0"
}
},
{
"identity" : "textview",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Dimillian/TextView",
"state" : {
"branch" : "main",
"revision" : "0316b7df487f5e6f1b199d326e903310b9044c0d"
}
}
],
"version" : 2

View file

@ -18,7 +18,8 @@ let package = Package(
.package(name: "Network", path: "../Network"),
.package(name: "Env", path: "../Env"),
.package(name: "DesignSystem", path: "../DesignSystem"),
.package(url: "https://github.com/markiv/SwiftUI-Shimmer", exact: "1.1.0")
.package(url: "https://github.com/markiv/SwiftUI-Shimmer", exact: "1.1.0"),
.package(url: "https://github.com/Dimillian/TextView", branch: "main")
],
targets: [
.target(
@ -28,7 +29,8 @@ let package = Package(
.product(name: "Network", package: "Network"),
.product(name: "Env", package: "Env"),
.product(name: "DesignSystem", package: "DesignSystem"),
.product(name: "Shimmer", package: "SwiftUI-Shimmer")
.product(name: "Shimmer", package: "SwiftUI-Shimmer"),
.product(name: "TextView", package: "TextView")
]),
]
)

View file

@ -1,19 +1,30 @@
import SwiftUI
import Accounts
import Env
import DesignSystem
import TextView
public struct StatusEditorView: View {
@EnvironmentObject private var currentAccount: CurrentAccount
@Environment(\.dismiss) private var dismiss
@State private var statusText: String = ""
@StateObject private var viewModel = StatusEditorViewModel()
public init() {
}
public var body: some View {
NavigationStack {
Form {
TextEditor(text: $statusText)
VStack {
accountHeaderView
TextView($viewModel.statusText)
.placeholder("What's on your mind")
.foregroundColor(.clear)
Spacer()
}
.navigationTitle("Post a toot")
.padding(.horizontal, DS.Constants.layoutPadding)
.navigationTitle("New post")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
@ -33,4 +44,22 @@ public struct StatusEditorView: View {
}
}
}
@ViewBuilder private var accountHeaderView: some View {
if let account = currentAccount.account {
HStack {
AvatarView(url: account.avatar, size: .status)
VStack(alignment: .leading, spacing: 0) {
account.displayNameWithEmojis
.font(.subheadline)
.fontWeight(.semibold)
Text("@\(account.acct)")
.font(.footnote)
.foregroundColor(.gray)
}
Spacer()
}
}
}
}

View file

@ -0,0 +1,39 @@
import SwiftUI
import DesignSystem
@MainActor
class StatusEditorViewModel: ObservableObject {
@Published var statusText = NSAttributedString(string: "") {
didSet {
guard !internalUpdate else { return }
highlightMeta()
}
}
private var internalUpdate: Bool = false
func highlightMeta() {
let mutableString = NSMutableAttributedString(attributedString: statusText)
let hashtagPattern = "(#+[a-zA-Z0-9(_)]{1,})"
let mentionPattern = "(@+[a-zA-Z0-9(_)]{1,})"
var ranges: [NSRange] = [NSRange]()
let hashtagRegex = try! NSRegularExpression(pattern: hashtagPattern, options: [])
let mentionRegex = try! NSRegularExpression(pattern: mentionPattern, options: [])
ranges = hashtagRegex.matches(in: mutableString.string,
options: [],
range: NSMakeRange(0, mutableString.string.utf8.count)).map {$0.range}
ranges.append(contentsOf: mentionRegex.matches(in: mutableString.string,
options: [],
range: NSMakeRange(0, mutableString.string.utf8.count)).map {$0.range})
for range in ranges {
mutableString.addAttributes([.foregroundColor: UIColor(Color.brand)],
range: NSRange(location: range.location, length: range.length))
}
internalUpdate = true
statusText = mutableString
internalUpdate = false
}
}