Skip to content

Commit

Permalink
Performance improvement; swipe fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
liang2kl committed May 30, 2021
1 parent 6f4011a commit 15ce3ed
Show file tree
Hide file tree
Showing 24 changed files with 228 additions and 188 deletions.
8 changes: 4 additions & 4 deletions Source/Hollow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Hollow/Hollow.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 64;
CURRENT_PROJECT_VERSION = 66;
DEVELOPMENT_TEAM = C5UH93T368;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = Hollow/Info.plist;
Expand All @@ -2106,7 +2106,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Hollow/Hollow.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 64;
CURRENT_PROJECT_VERSION = 66;
DEVELOPMENT_TEAM = C5UH93T368;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = Hollow/Info.plist;
Expand All @@ -2131,7 +2131,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = HollowWidget/HollowWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 64;
CURRENT_PROJECT_VERSION = 66;
DEVELOPMENT_TEAM = C5UH93T368;
INFOPLIST_FILE = HollowWidget/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
Expand Down Expand Up @@ -2159,7 +2159,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = HollowWidget/HollowWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 64;
CURRENT_PROJECT_VERSION = 66;
DEVELOPMENT_TEAM = C5UH93T368;
INFOPLIST_FILE = HollowWidget/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
Expand Down
1 change: 1 addition & 0 deletions Source/Hollow/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppModel: ObservableObject {
private var cancellables = Set<AnyCancellable>()

@Published var isInMainView = Defaults[.accessToken] != nil && Defaults[.hollowConfig] != nil
var widgetReloadCount = 0

private init() {
// Chcek for version update
Expand Down
5 changes: 4 additions & 1 deletion Source/Hollow/App/HollowApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ struct HollowApp: App {
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification),
perform: { _ in
appDelegate.fetchConfig()
WidgetCenter.shared.reloadAllTimelines()
if appModel.widgetReloadCount % 3 == 0 {
WidgetCenter.shared.reloadAllTimelines()
appModel.widgetReloadCount += 1
}
}
)
.onOpenURL { url in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct HollowContentView: View {
options.contains(.replaceForImageOnly) {
text = "[" + NSLocalizedString("TEXTVIEW_PHOTO_PLACEHOLDER_TEXT", comment: "") + "]"
}
return HollowTextView(postData: postDataWrapper.post, inDetail: !options.contains(.compactText), highlight: postDataWrapper.post.renderHighlight && options.contains(.showHyperlinks), compactLineLimit: options.contains(.compactText) ? lineLimit : nil)
return HollowTextView(postData: postDataWrapper.post, inDetail: !options.contains(.compactText), highlight: postDataWrapper.post.renderHighlight && options.contains(.showHyperlinks), links: postDataWrapper.post.url, citedNumbers: postDataWrapper.post.citedNumbers, compactLineLimit: options.contains(.compactText) ? lineLimit : nil)
}

private func tagView(text: String, deleted: Bool) -> some View {
Expand Down
63 changes: 30 additions & 33 deletions Source/Hollow/View/Hierarchy/Hollow/Content/HollowTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ import Introspect

struct HollowTextView: View {
var text: String
var hasURL: Bool
var hasCitedNumbers: Bool
var inDetail: Bool
var highlight: Bool
var links: [String] = []
var citedNumbers: [Int] = []

var compactLineLimit: Int? = nil

init(postData: PostData, inDetail: Bool, highlight: Bool, compactLineLimit: Int? = nil) {
init(postData: PostData, inDetail: Bool, highlight: Bool, links: [String], citedNumbers: [Int], compactLineLimit: Int? = nil) {
self.text = postData.text
self.hasURL = postData.hasURL
self.hasCitedNumbers = postData.hasCitedNumbers
self.inDetail = inDetail
self.highlight = highlight
self.links = links
self.citedNumbers = citedNumbers
self.compactLineLimit = compactLineLimit
}

init(text: String, hasURL: Bool = true, hasCitedNumbers: Bool = true, inDetail: Bool, highlight: Bool, compactLineLimit: Int? = nil) {
self.text = text
self.hasURL = hasURL
self.hasCitedNumbers = hasCitedNumbers
self.inDetail = inDetail
self.highlight = highlight
self.links = text.links()
self.citedNumbers = text.citationNumbers()
self.compactLineLimit = compactLineLimit
}

Expand All @@ -59,13 +59,13 @@ struct HollowTextView: View {
$0.underline()
.foregroundColor(.hollowContentText)
})

} else {
Text(text)

}
}

.modifier(TextModifier(inDetail: inDetail, compactLineLimit: compactLineLimit))
}

Expand All @@ -78,34 +78,31 @@ struct HollowTextView: View {
})
Divider()
}
if hasURL {
let links = Array(text.links().compactMap({ URL(string: $0) }))
if !links.isEmpty {
Divider()
ForEach(links, id: \.self) { link in
Button(action: {
let helper = OpenURLHelper(openURL: openURL)
try? helper.tryOpen(link, method: Defaults[.openURLMethod])
}) {
Label(link.absoluteString, systemImage: "link")
}
let links = Array(links.compactMap({ URL(string: $0) }))
if !links.isEmpty {
Divider()
ForEach(links, id: \.self) { link in
Button(action: {
let helper = OpenURLHelper(openURL: openURL)
try? helper.tryOpen(link, method: Defaults[.openURLMethod])
}) {
Label(link.absoluteString, systemImage: "link")
}
Divider()
}
Divider()
}
if hasCitedNumbers {
let citedPosts = text.citationNumbers()
if !citedPosts.isEmpty {
ForEach(citedPosts, id: \.self) { post in
let wrapper = PostDataWrapper.templatePost(for: post)
Button(action: {
IntegrationUtilities.conditionallyPresentDetail(store: .init(bindingPostWrapper: .constant(wrapper)))
}) {
Label("#\(post.string)", systemImage: "text.quote")
}

let citedPosts = citedNumbers
if !citedPosts.isEmpty {
ForEach(citedPosts, id: \.self) { post in
let wrapper = PostDataWrapper.templatePost(for: post)
Button(action: {
IntegrationUtilities.conditionallyPresentDetail(store: .init(bindingPostWrapper: .constant(wrapper)))
}) {
Label("#\(post.string)", systemImage: "text.quote")
}
Divider()
}
Divider()
}
}

Expand Down
166 changes: 78 additions & 88 deletions Source/Hollow/View/Hierarchy/Hollow/Detail/HollowDetailSubViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,109 +77,99 @@ extension HollowDetailView {
}

func commentView(for comment: CommentData) -> some View {
var hideLabel: Bool = false
let index = store.postDataWrapper.post.comments.firstIndex(where: { $0.commentId == comment.commentId })
let hideLabel: Bool = showOnlyName == comment.name ?
false :
(reverseComments ? !comment.showAvatarWhenReversed : !comment.showAvatar)

if showOnlyName != comment.name {
let firstIndex = reverseComments ? store.postDataWrapper.post.comments.count - 1 : 0
if index == firstIndex { hideLabel = false }
else if let index = index {
let nextIndex = reverseComments ? index + 1 : index - 1
hideLabel = comment.name == store.postDataWrapper.post.comments[nextIndex].name
let bindingComment = Binding(
get: { comment },
set: { comment in
if let index = store.postDataWrapper.post.comments.firstIndex(where: { $0.commentId == comment.commentId }) {
store.postDataWrapper.post.comments[index] = comment
}
}
}
)

return Group { if let index = index {
let bindingComment = Binding(
get: { comment },
set: { comment in
if let index = store.postDataWrapper.post.comments.firstIndex(where: { $0.commentId == comment.commentId }) {
store.postDataWrapper.post.comments[index] = comment
}
}
)

let highlighted = store.replyToIndex == index || jumpedToCommentId == comment.commentId
let highlighted = store.replyToId == comment.commentId || jumpedToCommentId == comment.commentId

return HollowCommentContentView(
commentData: bindingComment,
compact: false,
contentVerticalPadding: UIDevice.isMac ? 13 : 10,
hideLabel: hideLabel,
postColorIndex: store.postDataWrapper.post.colorIndex,
postHash: store.postDataWrapper.post.hash,
imageReloadHandler: { store.reloadImage($0, commentId: comment.commentId) },
jumpToReplyingHandler: { jumpToComment(commentId: comment.replyTo) }
)
.padding(.horizontal)
.background(
Group {
highlighted ? Color.background : Color.hollowCardBackground
}
)
.onClickGesture {
guard !store.isSendingComment && !store.isLoading else { return }
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
withAnimation(scrollAnimation) {
store.replyToId = comment.commentId
jumpedToCommentId = nil
}
}
.contextMenu {
if comment.text != "" {
Button(action: {
UIPasteboard.general.string = comment.text
}, label: {
Label(NSLocalizedString("COMMENT_VIEW_COPY_TEXT_LABEL", comment: ""), systemImage: "doc.on.doc")
})
}

HollowCommentContentView(
commentData: bindingComment,
compact: false,
contentVerticalPadding: UIDevice.isMac ? 13 : 10,
hideLabel: hideLabel,
postColorIndex: store.postDataWrapper.post.colorIndex,
postHash: store.postDataWrapper.post.hash,
imageReloadHandler: { store.reloadImage($0, commentId: comment.commentId) },
jumpToReplyingHandler: { jumpToComment(commentId: comment.replyTo) }
)
.padding(.horizontal)
.background(
Group {
highlighted ? Color.background : Color.hollowCardBackground
}
)
.onClickGesture {
guard !store.isSendingComment && !store.isLoading else { return }
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
withAnimation(scrollAnimation) {
store.replyToIndex = index
jumpedToCommentId = nil
if showOnlyName == nil {
Button(action: { withAnimation { showOnlyName = comment.name } }) {
Label("COMMENT_VIEW_SHOW_ONLY_LABEL", systemImage: "line.horizontal.3.decrease.circle")
}
Divider()
}
.contextMenu {
if comment.text != "" {

let links = Array(comment.url.compactMap({ URL(string: $0) }))
if !links.isEmpty {
Divider()
ForEach(links, id: \.self) { link in
Button(action: {
UIPasteboard.general.string = comment.text
}, label: {
Label(NSLocalizedString("COMMENT_VIEW_COPY_TEXT_LABEL", comment: ""), systemImage: "doc.on.doc")
})
}

if showOnlyName == nil {
Button(action: { withAnimation { showOnlyName = comment.name } }) {
Label("COMMENT_VIEW_SHOW_ONLY_LABEL", systemImage: "line.horizontal.3.decrease.circle")
}
Divider()
}

if comment.hasURL {
let links = Array(comment.text.links().compactMap({ URL(string: $0) }))
Divider()
ForEach(links, id: \.self) { link in
Button(action: {
let helper = OpenURLHelper(openURL: openURL)
try? helper.tryOpen(link, method: Defaults[.openURLMethod])
}) {
Label(link.absoluteString, systemImage: "link")
}
let helper = OpenURLHelper(openURL: openURL)
try? helper.tryOpen(link, method: Defaults[.openURLMethod])
}) {
Label(link.absoluteString, systemImage: "link")
}
Divider()
}
if comment.hasCitedNumbers {
let citedPosts = comment.text.citationNumbers()
Divider()
ForEach(citedPosts, id: \.self) { post in
let wrapper = PostDataWrapper.templatePost(for: post)
Button(action: {
IntegrationUtilities.conditionallyPresentDetail(store: .init(bindingPostWrapper: .constant(wrapper)))
}) {
Label("#\(post.string)", systemImage: "text.quote")
}
Divider()
}
let citedPosts = comment.citedNumbers
if !citedPosts.isEmpty {
Divider()
ForEach(citedPosts, id: \.self) { post in
let wrapper = PostDataWrapper.templatePost(for: post)
Button(action: {
IntegrationUtilities.conditionallyPresentDetail(store: .init(bindingPostWrapper: .constant(wrapper)))
}) {
Label("#\(post.string)", systemImage: "text.quote")
}
Divider()
}
ReportMenuContent(
store: store,
permissions: comment.permissions,
commentId: comment.commentId
)
Divider()
}
}}

ReportMenuContent(
store: store,
permissions: comment.permissions,
commentId: comment.commentId
)
}

}

func jumpToComment(commentId: Int) {
withAnimation(scrollAnimation) { store.replyToIndex = -2 }
jumpedFromCommentId = commentId
withAnimation(scrollAnimation) { store.replyToId = -2 }
withAnimation(scrollAnimation) { jumpedFromCommentId = commentId }
}

struct PlaceholderComment: View {
Expand Down
Loading

0 comments on commit 15ce3ed

Please sign in to comment.