Skip to content

Commit

Permalink
~ Consolidated version UI
Browse files Browse the repository at this point in the history
  • Loading branch information
buresdv committed Nov 4, 2024
1 parent 8a26216 commit 09783fd
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Cork/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -5962,6 +5962,9 @@
}
}
}
},
"add-package.error.could-not-construct-version-string.%@" : {

},
"add-package.error.timed-out.title" : {
"localizations" : {
Expand Down
6 changes: 3 additions & 3 deletions Cork/Logic/Search/Search for Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ private func processSearchResults(packageNameArray: [String], packageType: Packa
{
if finalArray[indexOfPreviouslyFoundPackage].additionalVersions == nil
{
finalArray[indexOfPreviouslyFoundPackage].additionalVersions = [additionalVersion]
finalArray[indexOfPreviouslyFoundPackage].additionalVersions = [.init(versionIdentifier: additionalVersion)]
}
else
{
finalArray[indexOfPreviouslyFoundPackage].additionalVersions?.append(additionalVersion)
finalArray[indexOfPreviouslyFoundPackage].additionalVersions?.append(.init(versionIdentifier: additionalVersion))
}
}
}
Expand All @@ -69,7 +69,7 @@ private func processSearchResults(packageNameArray: [String], packageType: Packa
.init(
packageName: splitPackageNameAndVersion.packageName,
packageType: packageType,
additionalVersions: [additionalVersion]
additionalVersions: [.init(versionIdentifier: additionalVersion)]
)
)
}
Expand Down
37 changes: 35 additions & 2 deletions Cork/Models/Package Installation/Search Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,52 @@ struct SearchResult: Identifiable, Hashable, Codable
let packageName: String
let packageType: PackageType

var additionalVersions: [String]?
var additionalVersions: [PackageVersion]?

}

enum SearchResultConstructionError: LocalizedError
{
case couldNotCreateHomebrewVersionString

var errorDescription: String?
{
switch self {
case .couldNotCreateHomebrewVersionString:
"error.package-install.could-not-create-homebrew-version-string"
}
}
}

extension SearchResult
{
func createHomebrewVersionString(fromVersion additionalVersion: PackageVersion?) throws(SearchResultConstructionError) -> String
{
if let additionalVersion
{
return "\(self.packageName)@\(additionalVersion.versionIdentifier)"
}
else
{
if self.additionalVersions == nil
{
return "\(self.packageName)"
}
else
{
throw .couldNotCreateHomebrewVersionString
}
}
}

/// Convert the search result to a primitive ``BrewPackage``
func convertToPackage() -> BrewPackage
{
return .init(
name: self.packageName,
type: self.packageType,
installedOn: nil,
versions: self.additionalVersions ?? .init(),
versions: .init(),
sizeInBytes: nil
)
}
Expand Down
18 changes: 18 additions & 0 deletions Cork/Models/Packages/Package Version.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Package Version.swift
// Cork
//
// Created by David Bureš on 11.10.2024.
//

import Foundation

/// A version for a package
struct PackageVersion: Identifiable, Hashable, Codable
{
let id: UUID = .init()

/// Identifier for the version
/// For most packages, a number (like "14" or "16") is used. Some packages also use different identifiers, such as "develop".
let versionIdentifier: String
}
16 changes: 14 additions & 2 deletions Cork/Views/Installation/Reusables/Search Result Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct SearchResultRow: View, Sendable

@State private var isLoadingDescription: Bool = true
@State private var descriptionParsingFailed: Bool = false

var descriptionLoadingForciblyDisabled: Bool = false

var body: some View
{
Expand Down Expand Up @@ -64,7 +66,7 @@ struct SearchResultRow: View, Sendable
}
}

if showDescriptionsInSearchResults
if showDescriptionsInSearchResults && !descriptionLoadingForciblyDisabled
{
if !descriptionParsingFailed
{ // Show this if the description got properly parsed
Expand Down Expand Up @@ -97,7 +99,7 @@ struct SearchResultRow: View, Sendable
}
.task
{
if showDescriptionsInSearchResults
if showDescriptionsInSearchResults && !descriptionLoadingForciblyDisabled
{
AppConstants.shared.logger.info("\(searchedForPackage.name, privacy: .auto) came into view")

Expand Down Expand Up @@ -138,3 +140,13 @@ struct SearchResultRow: View, Sendable
}
}
}

extension SearchResultRow
{
func packageDescriptionsAlwaysDisabled() -> SearchResultRow
{
var modifiedView: SearchResultRow = self
modifiedView.descriptionLoadingForciblyDisabled = true
return modifiedView
}
}
21 changes: 15 additions & 6 deletions Cork/Views/Installation/Sub-Views/Presenting Search Results.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,22 @@ private struct SearchResultsSection: View
{
DisclosureGroup
{
ForEach(additionalVersions, id: \.self)
ForEach(additionalVersions)
{ additionalVersion in
SearchResultRow(
searchResult: .init(
packageName: "\(package.packageName)@\(additionalVersion)",
packageType: package.packageType)
)
if let versionString = try? package.createHomebrewVersionString(fromVersion: additionalVersion)
{
SearchResultRow(
searchResult: .init(
packageName: versionString,
packageType: package.packageType)
)
.packageDescriptionsAlwaysDisabled()
}
else
{
Text("add-package.error.could-not-construct-version-string.\(additionalVersion.versionIdentifier)")
}

}
} label: {
SearchResultRow(searchResult: package)
Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Shared/Sanitized Package Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct SanitizedPackageName: View
{
/// The version is the length of the package name, + 1 due to the @ character
Text("v. \(String(packageNameWithoutTapName.dropFirst(sanitizedName.count + 1)))")
.foregroundColor(.gray)
.foregroundColor(.secondary)
.font(.subheadline)
}
}
Expand Down

0 comments on commit 09783fd

Please sign in to comment.