Skip to content

Commit

Permalink
Decouple avatarID from Profile model (#304)
Browse files Browse the repository at this point in the history
* Decouple the avatar identifier from the model

* Update demo app

* Update minor version

* Allow warnings

* Use `profileIdentifier?.avatarIdentifier in the demo screen.
  • Loading branch information
pinarol authored Jul 1, 2024
1 parent 4deb714 commit 337a2b6
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .buildkite/commands/publish-pod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ echo "--- :rubygems: Setting up Gems"
install_gems

echo "--- :cocoapods: Publishing $PODSPEC_PATH to CocoaPods CDN"
publish_pod --synchronous "$PODSPEC_PATH"
publish_pod --allow-warnings --synchronous "$PODSPEC_PATH"

echo "--- :slack: Notifying Slack"
slack_notify_pod_published "$PODSPEC_PATH" "$SLACK_WEBHOOK"
4 changes: 2 additions & 2 deletions .buildkite/commands/validate-pods.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash -eu

echo "--- :cocoapods: Validate Gravatar.podspec"
validate_podspec Gravatar.podspec
validate_podspec --allow-warnings Gravatar.podspec

echo "--- :cocoapods: Validate GravatarUI.podspec"
validate_podspec GravatarUI.podspec
validate_podspec --allow-warnings GravatarUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class DemoProfileConfigurationViewController: UITableViewController {
guard !email.isEmpty else { return }

var config = ProfileViewConfiguration.standard()
config.avatarIdentifier = .email(email)
config.isLoading = true
models[email] = config
snapshot.appendItems([email])
Expand All @@ -75,6 +76,7 @@ class DemoProfileConfigurationViewController: UITableViewController {
do {
let profile = try await service.fetch(with: .email(email))
models[email] = .standard(model: profile)
models[email]?.avatarIdentifier = .email(email)
snapshot.reloadItems([email])
await dataSource.apply(snapshot)
} catch APIError.responseError(let reason) where reason.httpStatusCode == 404 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DemoProfilePresentationStylesViewController: DemoBaseProfileViewController
}
}
else {
let viewController = ProfileViewController(configuration: newConfig(), viewModel: .init(), profileIdentifier: profileIdentifier)
let viewController = ProfileViewController(configuration: newConfig(profileIdentifier: profileIdentifier), viewModel: .init(), profileIdentifier: profileIdentifier)
self.profileViewController = viewController
presentInBottomSheet(viewController)
}
Expand Down Expand Up @@ -74,7 +74,7 @@ class DemoProfilePresentationStylesViewController: DemoBaseProfileViewController
bottomSheetNavigationViewController?.navigationBar.scrollEdgeAppearance = appearance
}

func newConfig() -> ProfileViewConfiguration {
func newConfig(profileIdentifier: ProfileIdentifier?) -> ProfileViewConfiguration {
var config: ProfileViewConfiguration
switch preferredProfileStyle {
case .large:
Expand All @@ -86,6 +86,7 @@ class DemoProfilePresentationStylesViewController: DemoBaseProfileViewController
case .summary:
config = ProfileViewConfiguration.summary(palette: preferredPaletteType)
}
config.avatarIdentifier = profileIdentifier?.avatarIdentifier
if customizeAvatarSwitchWithLabel.isOn {
config.avatarConfiguration.borderColor = .green
config.avatarConfiguration.borderWidth = 3
Expand Down
9 changes: 5 additions & 4 deletions Demo/Demo/Gravatar-Demo/DemoProfileViewsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class DemoProfileViewsViewController: DemoBaseProfileViewController {

guard largeProfileView.isLoading == false else { return }

largeProfileView.loadAvatar(with: .email(email), defaultAvatarOption: .status404, options: [.transition(.fade(0.2))])
largeProfileSummaryView.loadAvatar(with: .email(email), defaultAvatarOption: .status404, options: [.transition(.fade(0.2))])
profileView.loadAvatar(with: .email(email), defaultAvatarOption: .status404, options: [.transition(.fade(0.2))])
profileSummaryView.loadAvatar(with: .email(email), defaultAvatarOption: .status404, options: [.transition(.fade(0.2))])

updateLoading(isLoading: true)
let service = ProfileService()
Task {
Expand All @@ -92,16 +97,12 @@ class DemoProfileViewsViewController: DemoBaseProfileViewController {
let profile = try await service.fetch(with: identifier)
largeProfileView.update(with: profile)
largeProfileView.profileButtonStyle = .view
largeProfileView.loadAvatar(with: profile.avatarIdentifier, options: [.transition(.fade(0.2))])
largeProfileSummaryView.update(with: profile)
largeProfileSummaryView.profileButtonStyle = .view
largeProfileSummaryView.loadAvatar(with: profile.avatarIdentifier, options: [.transition(.fade(0.2))])
profileView.update(with: profile)
profileView.profileButtonStyle = .view
profileView.loadAvatar(with: profile.avatarIdentifier, options: [.transition(.fade(0.2))])
profileSummaryView.update(with: profile)
profileSummaryView.profileButtonStyle = .view
profileSummaryView.loadAvatar(with: profile.avatarIdentifier, options: [.transition(.fade(0.2))])
} catch APIError.responseError(reason: let reason) where reason.httpStatusCode == 404 {
largeProfileView.updateWithClaimProfilePrompt()
largeProfileSummaryView.updateWithClaimProfilePrompt()
Expand Down
10 changes: 10 additions & 0 deletions Sources/Gravatar/Identifiers/ProfileIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ extension ProfileIdentifier: Identifiable {
hashID.id
}
}

// Returns the `AvatarIdentifier` popuplated from `self`.
public var avatarIdentifier: AvatarIdentifier {
switch self {
case .email(let email):
AvatarIdentifier.email(email.string)
case .hashID(let hashID):
AvatarIdentifier.hashID(hashID.string)
}
}
}
7 changes: 5 additions & 2 deletions Sources/GravatarUI/ProfileView/BaseProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ open class BaseProfileView: UIView, UIContentView {
}
}

private var avatarIdentifier: AvatarIdentifier?

var padding: UIEdgeInsets {
get {
// layoutMargins is automatically synced with directionalLayoutMargins
Expand Down Expand Up @@ -288,6 +290,7 @@ open class BaseProfileView: UIView, UIContentView {
defaultAvatarOption: DefaultAvatarOption? = nil,
options: [ImageSettingOption]? = nil
) {
self.avatarIdentifier = avatarIdentifier
guard let avatarIdentifier else {
avatarProvider.setImage(placeholder)
return
Expand Down Expand Up @@ -407,7 +410,7 @@ open class BaseProfileView: UIView, UIContentView {

private func loadAvatar(with config: ProfileViewConfiguration) {
loadAvatar(
with: config.avatarID,
with: config.avatarIdentifier ?? config.avatarID,
placeholder: config.avatarConfiguration.placeholder,
rating: config.avatarConfiguration.rating,
defaultAvatarOption: config.avatarConfiguration.defaultAvatarOption,
Expand All @@ -426,7 +429,7 @@ open class BaseProfileView: UIView, UIContentView {

@objc
private func avatarTapped() {
delegate?.profileView(self, didTapOnAvatarWithID: model?.avatarIdentifier)
delegate?.profileView(self, didTapOnAvatarWithID: avatarIdentifier)
}

// MARK: - Placeholder handling
Expand Down
8 changes: 8 additions & 0 deletions Sources/GravatarUI/ProfileView/ProfileViewConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ public struct ProfileViewConfiguration: UIContentConfiguration {
/// The style for the profile view.
public let profileStyle: Style
/// The identifier for the avatar image to be loaded in the profile view.
@available(
*,
deprecated,
renamed: "avatarIdentifier",
message: "Set `avatarIdentifier` explicitly and don't use the model's avatarIdentifier. It's because the `ProfileModel.avatarIdentifier` always refers to the primary email's avatar even if we query the profile with a secondary email."
)
var avatarID: AvatarIdentifier? {
model?.avatarIdentifier ?? summaryModel?.avatarIdentifier
}

/// The identifier for the avatar image to be loaded in the profile view.
public var avatarIdentifier: AvatarIdentifier?
/// The palette to be used to style the view.
public var palette: PaletteType
/// Creates a padding space around the content of the profile view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public class ProfileViewController: UIViewController {
}

public func fetchProfile(profileIdentifier: ProfileIdentifier) {
var newConfig = self.configuration
newConfig.avatarIdentifier = profileIdentifier.avatarIdentifier
self.configuration = newConfig
Task {
await viewModel.fetchProfile(profileIdentifier: profileIdentifier)
}
Expand Down
2 changes: 1 addition & 1 deletion version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Gravatar
VERSION = '2.0.2'.freeze
VERSION = '2.1.0'.freeze
end

0 comments on commit 337a2b6

Please sign in to comment.