Skip to content

Commit

Permalink
Refactor annotation label view into shared package
Browse files Browse the repository at this point in the history
Move AnnotationLabelView into STAnnotationsPluginShared so it can be used by
both AppKit and UIKit demo apps. Rename it to STAnnotationLabelView for
consistency with other public types in the package.

Update the view to take an action closure that is invoked when the annotation
is tapped, allowing the demo apps to remove the tapped annotation. Simplify
the container shape to use a standard RoundedRectangle.

Remove the now unused AnnotationLabelView files from the demo app projects.
  • Loading branch information
krzyzanowskim committed Dec 14, 2024
1 parent f561618 commit e6f43de
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 137 deletions.
12 changes: 0 additions & 12 deletions DemoApp.AppKit/DemoApp.AppKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
752C90732AB1AF12006C835E /* AnnotationLabelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752C90722AB1AF12006C835E /* AnnotationLabelView.swift */; };
7565EBBE2A9BE09D0005B170 /* EditorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7565EBBD2A9BE09D0005B170 /* EditorViewController.swift */; };
7565EBC12A9BE0B10005B170 /* STTextView in Frameworks */ = {isa = PBXBuildFile; productRef = 7565EBC02A9BE0B10005B170 /* STTextView */; };
7576EDCD2AB062F400540BF5 /* STTextViewAnnotationsPlugin in Frameworks */ = {isa = PBXBuildFile; productRef = 7576EDCC2AB062F400540BF5 /* STTextViewAnnotationsPlugin */; };
Expand All @@ -17,7 +16,6 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
752C90722AB1AF12006C835E /* AnnotationLabelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnnotationLabelView.swift; sourceTree = "<group>"; };
7565EBBD2A9BE09D0005B170 /* EditorViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorViewController.swift; sourceTree = "<group>"; };
7576EDCA2AB0627D00540BF5 /* STTextView-Plugin-Annotations */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "STTextView-Plugin-Annotations"; path = ..; sourceTree = "<group>"; };
758BAB4F2A9BDFC200D840BF /* DemoApp.AppKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoApp.AppKit.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -51,7 +49,6 @@
isa = PBXGroup;
children = (
7576EDCA2AB0627D00540BF5 /* STTextView-Plugin-Annotations */,
7590AFEA2B64617E00947F82 /* View */,
7565EBBD2A9BE09D0005B170 /* EditorViewController.swift */,
758BAB522A9BDFC200D840BF /* AppDelegate.swift */,
758BAB542A9BDFC300D840BF /* Assets.xcassets */,
Expand All @@ -70,14 +67,6 @@
name = Products;
sourceTree = "<group>";
};
7590AFEA2B64617E00947F82 /* View */ = {
isa = PBXGroup;
children = (
752C90722AB1AF12006C835E /* AnnotationLabelView.swift */,
);
path = View;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -156,7 +145,6 @@
buildActionMask = 2147483647;
files = (
758BAB532A9BDFC200D840BF /* AppDelegate.swift in Sources */,
752C90732AB1AF12006C835E /* AnnotationLabelView.swift in Sources */,
7565EBBE2A9BE09D0005B170 /* EditorViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion DemoApp.AppKit/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extension EditorViewController: STAnnotationsDataSource {
}

return STAnnotationView(frame: proposedViewFrame) {
AnnotationLabelView(Text(lineAnnotation.message), annotation: lineAnnotation) { [weak self] annotation in
STAnnotationLabelView(Text(lineAnnotation.message), annotation: lineAnnotation) { [weak self] annotation in
// Remove annotation
self?.annotations.removeAll(where: { $0.id == annotation.id })
}
Expand Down
12 changes: 0 additions & 12 deletions DemoApp.UIKit/DemoApp.UIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
3E8504C52C991A98004DC5CC /* Base in Resources */ = {isa = PBXBuildFile; fileRef = 3E8504C42C991A98004DC5CC /* Base */; };
3E8504C72C991A99004DC5CC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3E8504C62C991A99004DC5CC /* Assets.xcassets */; };
3E8504CA2C991A99004DC5CC /* Base in Resources */ = {isa = PBXBuildFile; fileRef = 3E8504C92C991A99004DC5CC /* Base */; };
3EB326C72C991D4D00F5C862 /* AnnotationLabelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EB326C62C991D4900F5C862 /* AnnotationLabelView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -26,7 +25,6 @@
3E8504C62C991A99004DC5CC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3E8504C92C991A99004DC5CC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3E8504CB2C991A99004DC5CC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3EB326C62C991D4900F5C862 /* AnnotationLabelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnnotationLabelView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -44,7 +42,6 @@
3E8504B12C991A98004DC5CC = {
isa = PBXGroup;
children = (
3EB326C52C991D3C00F5C862 /* View */,
3E8504BD2C991A98004DC5CC /* AppDelegate.swift */,
3E8504BF2C991A98004DC5CC /* SceneDelegate.swift */,
3E8504C12C991A98004DC5CC /* ViewController.swift */,
Expand All @@ -64,14 +61,6 @@
name = Products;
sourceTree = "<group>";
};
3EB326C52C991D3C00F5C862 /* View */ = {
isa = PBXGroup;
children = (
3EB326C62C991D4900F5C862 /* AnnotationLabelView.swift */,
);
path = View;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -150,7 +139,6 @@
buildActionMask = 2147483647;
files = (
3E8504C22C991A98004DC5CC /* ViewController.swift in Sources */,
3EB326C72C991D4D00F5C862 /* AnnotationLabelView.swift in Sources */,
3E8504BE2C991A98004DC5CC /* AppDelegate.swift in Sources */,
3E8504C02C991A98004DC5CC /* SceneDelegate.swift in Sources */,
);
Expand Down
102 changes: 0 additions & 102 deletions DemoApp.UIKit/View/AnnotationLabelView.swift

This file was deleted.

8 changes: 6 additions & 2 deletions DemoApp.UIKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit

import STTextView
import STAnnotationsPlugin
import STAnnotationsPluginShared

class ViewController: UIViewController {

Expand Down Expand Up @@ -105,8 +106,11 @@ extension ViewController: STAnnotationsDataSource {
}

return STAnnotationView(frame: proposedViewFrame) {
AnnotationLabelView(Text(lineAnnotation.message), annotation: lineAnnotation, textWidth: proposedViewFrame.width, textHeight: proposedViewFrame.height)
.font(.body)
STAnnotationLabelView(Text(lineAnnotation.message), annotation: lineAnnotation) { annotation in
// Remove annotation
self.annotations.removeAll(where: { $0.id == annotation.id })
}
.font(.body)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import SwiftUI
import Foundation
import STTextView
import STAnnotationsPlugin

struct AnnotationLabelView : View {
public struct STAnnotationLabelView: View {
@Environment(\.colorScheme) private var colorScheme

private let text: Text
private let annotation: STMessageLineAnnotation
private let action: (STMessageLineAnnotation) -> Void

init(_ text: Text, annotation: STMessageLineAnnotation, action: @escaping (STMessageLineAnnotation) -> Void) {
public init(_ text: Text, annotation: STMessageLineAnnotation, action: @escaping (STMessageLineAnnotation) -> Void) {
self.text = text
self.action = action
self.annotation = annotation
}

var body: some View {
public var body: some View {
Label {
text
.foregroundColor(.primary)
Expand Down Expand Up @@ -56,10 +55,7 @@ struct AnnotationLabelView : View {
}
)
.containerShape(
UnevenRoundedRectangle(
cornerRadii: RectangleCornerRadii(topLeading: 2, bottomLeading: 2),
style: .circular
)
RoundedRectangle(cornerRadius: 2, style: .circular)
)
}
}
Expand Down

0 comments on commit e6f43de

Please sign in to comment.