Skip to content

Commit

Permalink
New way to tell the user IPNS is copied
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Sep 15, 2023
1 parent 8f4ac78 commit 1706b04
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Planet/Helper/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ extension Notification.Name {
static let followingArticleReadChanged = Notification.Name("PlanetFollowingArticleReadChangedNotification")

static let myArticleBuilt = Notification.Name("PlanetMyArticleBuiltNotification")

static let copiedIPNS = Notification.Name("PlanetCopiedIPNSNotification")
}

// Writer
Expand Down
115 changes: 82 additions & 33 deletions PlanetLite/AppContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

import SwiftUI

// TODO: Put all the modifiers in a separate file
struct CapsuleBar: ViewModifier {
func body(content: Content) -> some View {
content
.padding(8)
.background(
.thinMaterial,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
)
.cornerRadius(8)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color("BorderColor"), lineWidth: 0.5)
)
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 1)
}
}

struct AppContentView: View {
@StateObject private var planetStore: PlanetStore

Expand All @@ -15,6 +33,7 @@ struct AppContentView: View {
let timer = Timer.publish(every: 300, on: .current, in: .common).autoconnect()

@State private var isShowingTaskProgressIndicator: Bool = false
@State private var isShowingCopiedIPNS: Bool = false

init() {
_planetStore = StateObject(wrappedValue: PlanetStore.shared)
Expand Down Expand Up @@ -69,41 +88,19 @@ struct AppContentView: View {
)
.background(Color(NSColor.textBackgroundColor))

VStack {
Spacer()
if isShowingTaskProgressIndicator {
HStack(spacing: 8) {
switch planetStore.currentTaskProgressIndicator {
case .none:
Spacer()
.frame(width: 16, height: 16)
case .progress:
ProgressView()
.progressViewStyle(.circular)
.controlSize(.small)
case .done:
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
.font(.system(size: 16))
}
Text(PlanetStore.shared.currentTaskMessage)
.font(.footnote)
}
.padding(8)
.background(
.thinMaterial,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
)
.cornerRadius(8)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color("BorderColor"), lineWidth: 0.5)
)
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 1)
.transition(AnyTransition.move(edge: .bottom).combined(with: .opacity))
copiedIPNS()
taskProgress()
}
.onReceive(NotificationCenter.default.publisher(for: .copiedIPNS)) { n in
Task {
withAnimation(.easeInOut) {
self.isShowingCopiedIPNS = true
}
await Task.sleep(1_000_000_000)
withAnimation(.easeInOut) {
self.isShowingCopiedIPNS = false
}
}
.padding(.bottom, 10)
}
.onChange(of: planetStore.isAggregating) { newValue in
debugPrint("PlanetStore: new value of isAggregating: \(newValue)")
Expand Down Expand Up @@ -135,6 +132,58 @@ struct AppContentView: View {
}
}
}

/// Show for 1 second when the IPNS is copied to clipboard
@ViewBuilder
private func copiedIPNS() -> some View {
VStack {
if isShowingCopiedIPNS {
HStack {
HStack(spacing: 8) {
Image(systemName: "square.on.square")
Text("IPNS copied to clipboard")
}
.modifier(CapsuleBar())

Spacer()
}
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
}
Spacer()
}
.padding(.leading, 16)
.padding(.top, 16)
}

/// Show the task progress, when aggregating
@ViewBuilder
private func taskProgress() -> some View {
VStack {
Spacer()
if isShowingTaskProgressIndicator {
HStack(spacing: 8) {
switch planetStore.currentTaskProgressIndicator {
case .none:
Spacer()
.frame(width: 16, height: 16)
case .progress:
ProgressView()
.progressViewStyle(.circular)
.controlSize(.small)
case .done:
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
.font(.system(size: 16))
}
Text(PlanetStore.shared.currentTaskMessage)
.font(.footnote)
}
.modifier(CapsuleBar())
.transition(AnyTransition.move(edge: .bottom).combined(with: .opacity))
}
}
.padding(.bottom, 10)
}
}

struct AppContentView_Previews: PreviewProvider {
Expand Down
23 changes: 6 additions & 17 deletions PlanetLite/AppTitlebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import SwiftUI
import UserNotifications


struct AppTitlebarView: View {
@StateObject private var planetStore: PlanetStore
@State private var title: String = "Croptop"
@State private var subtitle: String = ""

var size: CGSize

init(size: CGSize) {
self.size = size
_planetStore = StateObject(wrappedValue: PlanetStore.shared)
Expand Down Expand Up @@ -54,27 +53,17 @@ struct AppTitlebarView: View {
}
if let theSubtitle = titles["subtitle"], theSubtitle != "" {
self.subtitle = theSubtitle
} else {
}
else {
self.subtitle = ""
}
}
}
}

private func copyIPNSAction(fromPlanet planet: MyPlanetModel) {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(planet.ipns, forType: .string)
Task(priority: .background) {
let content = UNMutableNotificationContent()
content.title = planet.name
content.subtitle = "IPNS copied."
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(
identifier: planet.ipns,
content: content,
trigger: trigger
)
try? await UNUserNotificationCenter.current().add(request)
}
NotificationCenter.default.post(name: .copiedIPNS, object: planet)
}
}

0 comments on commit 1706b04

Please sign in to comment.