Skip to content

Commit

Permalink
refactor: infoview + bgbutton
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrighetti committed Nov 5, 2023
1 parent b4f470e commit db8b862
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 34 deletions.
12 changes: 12 additions & 0 deletions HNReader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
5F109D602AF704F700AE6AF3 /* IBMPlexSerif-Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 5F109D5C2AF704F700AE6AF3 /* IBMPlexSerif-Bold.ttf */; };
5F109D612AF704F700AE6AF3 /* IBMPlexSerif-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 5F109D5D2AF704F700AE6AF3 /* IBMPlexSerif-Regular.ttf */; };
5F109D622AF704F700AE6AF3 /* IBMPlexSerif-SemiBold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 5F109D5E2AF704F700AE6AF3 /* IBMPlexSerif-SemiBold.ttf */; };
5FCE20A72AF7B25A00BF4097 /* InfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE20A62AF7B25A00BF4097 /* InfoView.swift */; };
5FCE20A92AF7B47B00BF4097 /* BgButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE20A82AF7B47B00BF4097 /* BgButton.swift */; };
5FCE20AB2AF7B8EE00BF4097 /* +View.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE20AA2AF7B8EE00BF4097 /* +View.swift */; };
C93F99B6267554F00046F870 /* ItemCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B5267554F00046F870 /* ItemCell.swift */; };
C93F99B8267557FC0046F870 /* ItemList.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B7267557FC0046F870 /* ItemList.swift */; };
C93F99BA267580CE0046F870 /* HTMLText.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B9267580CE0046F870 /* HTMLText.swift */; };
Expand Down Expand Up @@ -59,6 +62,9 @@
5F109D5C2AF704F700AE6AF3 /* IBMPlexSerif-Bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "IBMPlexSerif-Bold.ttf"; sourceTree = "<group>"; };
5F109D5D2AF704F700AE6AF3 /* IBMPlexSerif-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "IBMPlexSerif-Regular.ttf"; sourceTree = "<group>"; };
5F109D5E2AF704F700AE6AF3 /* IBMPlexSerif-SemiBold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "IBMPlexSerif-SemiBold.ttf"; sourceTree = "<group>"; };
5FCE20A62AF7B25A00BF4097 /* InfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; };
5FCE20A82AF7B47B00BF4097 /* BgButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BgButton.swift; sourceTree = "<group>"; };
5FCE20AA2AF7B8EE00BF4097 /* +View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "+View.swift"; sourceTree = "<group>"; };
C93F99B5267554F00046F870 /* ItemCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemCell.swift; sourceTree = "<group>"; };
C93F99B7267557FC0046F870 /* ItemList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemList.swift; sourceTree = "<group>"; };
C93F99B9267580CE0046F870 /* HTMLText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTMLText.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -101,6 +107,7 @@
isa = PBXGroup;
children = (
3307159309D438EFAA1259C7 /* +Date.swift */,
5FCE20AA2AF7B8EE00BF4097 /* +View.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -145,6 +152,7 @@
isa = PBXGroup;
children = (
C93F99B9267580CE0046F870 /* HTMLText.swift */,
5FCE20A82AF7B47B00BF4097 /* BgButton.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -213,6 +221,7 @@
C93F99B7267557FC0046F870 /* ItemList.swift */,
C9926691267588B80035A88F /* Components */,
5F109D582AF6F50D00AE6AF3 /* ConditionalRedactedModifier.swift */,
5FCE20A62AF7B25A00BF4097 /* InfoView.swift */,
);
path = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -358,6 +367,8 @@
C9D0938026741BBF002CC786 /* Persistence.swift in Sources */,
C9D0937926741BBE002CC786 /* HomeView.swift in Sources */,
C93F99B6267554F00046F870 /* ItemCell.swift in Sources */,
5FCE20A92AF7B47B00BF4097 /* BgButton.swift in Sources */,
5FCE20A72AF7B25A00BF4097 /* InfoView.swift in Sources */,
5F109D592AF6F50D00AE6AF3 /* ConditionalRedactedModifier.swift in Sources */,
C93F99B8267557FC0046F870 /* ItemList.swift in Sources */,
C9E9BCFD2674C80E001B4E19 /* AppState.swift in Sources */,
Expand All @@ -369,6 +380,7 @@
330713D3016ED410AFD53FDF /* ItemListViewModel.swift in Sources */,
330711A9216E762026AF98A0 /* +Date.swift in Sources */,
33071F1C64D4742E1F947FAA /* ItemDownloader.swift in Sources */,
5FCE20AB2AF7B8EE00BF4097 /* +View.swift in Sources */,
3307147AB95F03650FC40B97 /* ItemCache.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
20 changes: 20 additions & 0 deletions HNReader/Utils/+View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// +View.swift
// HNReader
//
// Created by Mattia Righetti on 05/11/23.
//

import SwiftUI

extension View {
@discardableResult
func openInWindow(title: String, sender: Any?) -> NSWindow {
let controller = NSHostingController(rootView: self)
let win = NSWindow(contentViewController: controller)
win.contentViewController = controller
win.title = title
win.makeKeyAndOrderFront(sender)
return win
}
}
47 changes: 47 additions & 0 deletions HNReader/View/Components/BgButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// BgButton.swift
// HNReader
//
// Created by Mattia Righetti on 05/11/23.
//

import SwiftUI

struct BgButton: View {
var text: String? = nil
var icon: String
var disabled: Bool = false
var minSize: CGSize
var onHover: ((Bool) -> Void)? = nil
var action: (() -> ())? = nil

var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundStyle(Color.gray.opacity(0.1))
.frame(width: minSize.width, height: minSize.height)

Label(title: {
if text != nil {
Text(text!)
.font(.custom("IBMPlexSerif-Regular", size: 13))
}
}, icon: {
Image(systemName: icon)
})
.foregroundStyle(disabled ? .tertiary : .primary)
.padding()
}
.frame(width: minSize.width, height: minSize.height)
.onHover {
onHover?($0)
}
.onTapGesture {
action?()
}
}
}

#Preview {
BgButton(text: "Rate app", icon: "star", minSize: CGSize(width: 150, height: 50)).padding()
}
50 changes: 50 additions & 0 deletions HNReader/View/InfoView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// InfoView.swift
// HNReader
//
// Created by Mattia Righetti on 05/11/23.
//

import SwiftUI

struct InfoView: View {
var body: some View {
VStack {
Text("HNReader v1.2")
.font(.title2)
.padding(.bottom)

Text("This project is open source")

BgButton(text: "Rate HNReader", icon: "star", minSize: CGSize(width: 200, height: 50), onHover: { h in
DispatchQueue.main.async {
if (h) {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}, action: {
NSWorkspace.shared.open(URL(string: "https://github.com/mattrighetti/HNReader.git")!)
})

BgButton(text: "Open on GitHub", icon: "arrow.up.right", minSize: CGSize(width: 200, height: 50), onHover: { h in
DispatchQueue.main.async {
if (h) {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}, action: {
NSWorkspace.shared.open(URL(string: "https://github.com/mattrighetti/HNReader.git")!)
})
}
.padding()
.frame(minWidth: 300)
}
}

#Preview {
InfoView()
}
42 changes: 8 additions & 34 deletions HNReader/View/ItemCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,22 @@ struct ItemCell: View {
}

HStack {

ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundStyle(Color.gray.opacity(0.1))
.frame(width: 50, height: 50)

Label(title: {}, icon: {
Image(systemName: "bubble.left")
})
.foregroundStyle(.primary)
.padding()
}
.frame(width: 50, height: 50)
.onHover { isHovered in
BgButton(icon: "bubble.left", minSize: CGSize(width: 50, height: 50), onHover: { isHovered in
DispatchQueue.main.async {
if (isHovered) {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}
.onTapGesture {
}, action: {
if let item = item {
guard let url = URL(string: "https://news.ycombinator.com/item?id=\(item.id)") else { return }
NSWorkspace.shared.open(url)
}
}
})

ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundStyle(Color.gray.opacity(0.1))
.frame(width: 50, height: 50)

Label(title: {}, icon: {
Image(systemName: "arrow.up.right")
})
.foregroundStyle(item?.url != nil ? .primary : .tertiary)
.padding()
}
.frame(width: 50, height: 50)
.onHover { isHovered in
BgButton(icon: "arrow.up.right", disabled: item?.url == nil, minSize: CGSize(width: 50, height: 50), onHover: { isHovered in
DispatchQueue.main.async {
if (isHovered) {
if item?.url == nil {
Expand All @@ -108,12 +82,12 @@ struct ItemCell: View {
NSCursor.pop()
}
}
}
.onTapGesture {
}, action: {
guard let url = item?.url, let url = URL(string: url) else { return }
NSWorkspace.shared.open(url)
}
}.padding(.leading)
})
}
.padding(.leading)
}
.padding()
.background(colorScheme == .dark ? Color.black.opacity(0.3) : Color.gray.opacity(0.1))
Expand Down
5 changes: 5 additions & 0 deletions HNReader/View/ItemList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ struct ItemList: View {
Button(action: viewModel.refreshStories) {
Label("Refresh news", systemImage: "arrow.counterclockwise.circle")
}
Button(action: {
InfoView().openInWindow(title: "Info", sender: nil)
}, label: {
Label("Info", systemImage: "info.circle")
})
}
.navigationTitle("Hacker News")
}
Expand Down

0 comments on commit db8b862

Please sign in to comment.