Skip to content

Commit

Permalink
Merge pull request #15 from morishin/update-check
Browse files Browse the repository at this point in the history
Update check
  • Loading branch information
morishin authored Jun 11, 2024
2 parents 2208459 + ff09188 commit 49b42a1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 31 deletions.
20 changes: 17 additions & 3 deletions XDeck.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
5B90B8CA2A8E6D7400185EAA /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5B90B8C92A8E6D7400185EAA /* Preview Assets.xcassets */; };
5B90B8D22A8E6E6400185EAA /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B90B8D12A8E6E6400185EAA /* WebView.swift */; };
5B90B8D52A8E7A2F00185EAA /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B90B8D42A8E7A2F00185EAA /* LoginView.swift */; };
5BC7F4812C17EA42002A37C2 /* UpdateButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BC7F4802C17EA42002A37C2 /* UpdateButton.swift */; };
5BD50E852C14BBFE004D4A7E /* HideAdsToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BD50E842C14BBFE004D4A7E /* HideAdsToggle.swift */; };
/* End PBXBuildFile section */

Expand All @@ -47,6 +48,7 @@
5B90B8CB2A8E6D7400185EAA /* XDeck.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = XDeck.entitlements; sourceTree = "<group>"; };
5B90B8D12A8E6E6400185EAA /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = "<group>"; };
5B90B8D42A8E7A2F00185EAA /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
5BC7F4802C17EA42002A37C2 /* UpdateButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateButton.swift; sourceTree = "<group>"; };
5BD50E842C14BBFE004D4A7E /* HideAdsToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HideAdsToggle.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -66,6 +68,7 @@
children = (
5B66C9672AE7FBF400D51830 /* Icon */,
5B90B8C42A8E6D7300185EAA /* ContentView.swift */,
5BC7F4802C17EA42002A37C2 /* UpdateButton.swift */,
5B1CC26E2A95944A00DF7D83 /* AppearanceToggle.swift */,
5BD50E842C14BBFE004D4A7E /* HideAdsToggle.swift */,
5B90B8D42A8E7A2F00185EAA /* LoginView.swift */,
Expand Down Expand Up @@ -171,7 +174,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1430;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1530;
TargetAttributes = {
5B90B8BE2A8E6D7300185EAA = {
CreatedOnToolsVersion = 14.3;
Expand Down Expand Up @@ -224,6 +227,7 @@
5B90B8C32A8E6D7300185EAA /* XDeckApp.swift in Sources */,
5B1CC2732A95969600DF7D83 /* MoonIcon.swift in Sources */,
5B4A3FE22C17646F00043FC1 /* GitHubSponsorIcon.swift in Sources */,
5BC7F4812C17EA42002A37C2 /* UpdateButton.swift in Sources */,
5B0DC3542A8FC79400268684 /* WebViewConfigurations.swift in Sources */,
5BD50E852C14BBFE004D4A7E /* HideAdsToggle.swift in Sources */,
5B5F42252AAA368E0031658B /* AppConfig.swift in Sources */,
Expand All @@ -239,6 +243,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
Expand Down Expand Up @@ -268,9 +273,11 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand All @@ -286,7 +293,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 2.1;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand All @@ -300,6 +307,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
Expand Down Expand Up @@ -329,9 +337,11 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -341,7 +351,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 2.1;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -359,11 +369,13 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"XDeck/Preview Content\"";
DEVELOPMENT_TEAM = 4GERXBURZN;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -385,11 +397,13 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"XDeck/Preview Content\"";
DEVELOPMENT_TEAM = 4GERXBURZN;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
2 changes: 1 addition & 1 deletion XDeck.xcodeproj/xcshareddata/xcschemes/XDeck.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
58 changes: 31 additions & 27 deletions XDeck/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,21 @@ struct ContentView: View {
Alert(title: Text(alertMessage ?? ""))
}
HStack(spacing: 24) {
Button {
openURL(URL(string: "https://github.com/morishin/XDeck")!)
} label: {
GitHubIcon().foregroundColor(
Self.textColor(for: backgroundColor)
).frame(width: 20, height: 20)
}.buttonStyle(.plain).onHover { inside in
if inside {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
HStack(spacing: 8) {
Button {
openURL(URL(string: "https://github.com/morishin/XDeck")!)
} label: {
GitHubIcon().foregroundColor(
Self.textColor(for: backgroundColor)
).frame(width: 20, height: 20)
}.buttonStyle(.plain).onHover { inside in
if inside {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
UpdateButton()
}
AppearanceToggle(isOn: $isDarkMode) {}.onChange(
of: isDarkMode,
Expand All @@ -218,7 +221,7 @@ struct ContentView: View {
} label: {
Label {
Text("Sponsor")
.foregroundStyle(isDarkMode ? Color.white : Self.textColor(for: backgroundColor))
.foregroundStyle(Color.init(nsColor: .textColor))
} icon: {
GitHubSponsorIcon()
.foregroundColor(Color(hex: "#BF3989")).frame(
Expand Down Expand Up @@ -253,6 +256,7 @@ struct ContentView: View {
}
}
.background(backgroundColor)
.colorScheme(isDarkMode ? .dark : .light)
.onChange(
of: loginViewMessage,
perform: { loginViewMessage in
Expand Down Expand Up @@ -301,18 +305,18 @@ struct ContentView: View {
}
}

struct ContentView_Previews: PreviewProvider {
private static var defaultConfig: AppConfig {
return AppConfig(columns: [
AppConfig.Column(type: .forYou),
AppConfig.Column(type: .following),
AppConfig.Column(type: .notifications),
AppConfig.Column(type: .profile),
])
}

static var previews: some View {
ContentView(appConfig: defaultConfig)
.frame(minWidth: 1280, minHeight: 900)
}
}
//struct ContentView_Previews: PreviewProvider {
// private static var defaultConfig: AppConfig {
// return AppConfig(columns: [
// AppConfig.Column(type: .forYou),
// AppConfig.Column(type: .following),
// AppConfig.Column(type: .notifications),
// AppConfig.Column(type: .profile),
// ])
// }
//
// static var previews: some View {
// ContentView(appConfig: defaultConfig)
// .frame(minWidth: 1280, minHeight: 900)
// }
//}
82 changes: 82 additions & 0 deletions XDeck/View/UpdateButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import SwiftUI

struct UpdateButton: View {
@State private var updateAvailable: Bool = false
@State private var latestVersion: String? = nil
@Environment(\.openURL) private var openURL

private static var currentVersion: String? {
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}

var body: some View {
if let currentVersion = Self.currentVersion {
HStack {
if updateAvailable, let latestVersion = latestVersion {
HStack {
Text("v\(currentVersion) (v\(latestVersion) is available)")
Button("Update", action: {
openURL(URL(string: "https://github.com/morishin/XDeck/releases/latest")!)
})
.buttonStyle(.bordered)
}
} else {
Button("v\(currentVersion)", action: {
openURL(URL(string: "https://github.com/morishin/XDeck/releases/tag/\(currentVersion)")!)
}).buttonStyle(.plain)
}
}
.onHover { inside in
if inside {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
.onAppear {
Task {
await checkForUpdate()
}
}
} else {
EmptyView()
}
}

func checkForUpdate() async {
guard let url = URL(string: "https://github.com/morishin/XDeck/releases/latest") else { return }

var request = URLRequest(url: url)
request.httpMethod = "HEAD"

do {
let (_, response) = try await URLSession.shared.data(for: request)
if let latestReleaseUrlString = response.url?.absoluteString {
let latestVersion = extractVersion(from: latestReleaseUrlString)
let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0"
if currentVersion.compare(latestVersion, options: .numeric) == .orderedAscending {
DispatchQueue.main.async {
self.latestVersion = latestVersion
self.updateAvailable = true
}
}
}
} catch {
self.latestVersion = nil
self.updateAvailable = false
}
}

func extractVersion(from url: String) -> String {
guard let regex = try? NSRegularExpression(pattern: "/tag/([0-9]+\\.[0-9]+)"),
let match = regex.firstMatch(in: url, range: NSRange(location: 0, length: url.utf16.count)),
let range = Range(match.range(at: 1), in: url) else {
return "0.0"
}
return String(url[range])
}
}

#Preview {
UpdateButton().frame(maxWidth: .infinity)
}

0 comments on commit 49b42a1

Please sign in to comment.