-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QE: Share image #572
Open
pinarol
wants to merge
19
commits into
trunk
Choose a base branch
from
wppinar/share-image
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
QE: Share image #572
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c9c689b
Add shareURL to the AvatarImageModel
pinarol d9a22af
Add NSPhotoLibraryAddUsageDescription
pinarol e818ef9
Implement fetchOriginalSizeAvatar`
pinarol d172fbe
Add ShareSheet
pinarol d52e17f
Add AvatarShareItem
pinarol 8651fa7
Open share sheet on share action
pinarol 36e2d81
Format
pinarol 675d058
Add doc
pinarol c0e3333
Apply color scheme
pinarol 8e15be8
Add doc
pinarol 5eafec5
Merge branch 'trunk' into wppinar/share-image
pinarol 37feeb6
Add tests
pinarol 657b2e1
Fix
pinarol 4ab59d2
Fix test
pinarol ef774af
Add more tests
pinarol b39ff6e
Update permission message
pinarol 03ed12c
Add ... at the end of the share
pinarol bcc9f1b
Update strings in base locale
pinarol 8821ead
Use ZStack to put the ellipsis button instead of overlay
pinarol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Sources/GravatarUI/SwiftUI/AvatarPicker/AvatarShareItem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import UIKit | ||
|
||
struct AvatarShareItem: Identifiable { | ||
let id: String | ||
let image: UIImage | ||
let url: URL | ||
} |
22 changes: 22 additions & 0 deletions
22
Sources/GravatarUI/SwiftUI/AvatarPicker/Views/ShareSheet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import SwiftUI | ||
|
||
/// Use `ShareLink` after iOS 16+. | ||
struct ShareSheet: UIViewControllerRepresentable { | ||
var items: [Any] | ||
var activities: [UIActivity]? = nil | ||
|
||
func makeUIViewController(context: Context) -> UIActivityViewController { | ||
let controller = UIActivityViewController(activityItems: items, applicationActivities: activities) | ||
controller.excludedActivityTypes = [.print, | ||
.postToWeibo, | ||
.postToTencentWeibo, | ||
.addToReadingList, | ||
.postToVimeo, | ||
.openInIBooks] | ||
return controller | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) { | ||
// No need to update dynamically | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Foundation | ||
|
||
package class TestURLSessionError: NSError, @unchecked Sendable { | ||
let message: String | ||
|
||
package init(message: String) { | ||
self.message = message | ||
super.init(domain: NSURLErrorDomain, code: 1) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override package var localizedDescription: String { | ||
message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,16 @@ final class AvatarPickerViewModelTests { | |
model = Self.createModel() | ||
} | ||
|
||
static func createModel(session: URLSessionAvatarPickerMock = .init()) -> AvatarPickerViewModel { | ||
static func createModel( | ||
session: URLSessionAvatarPickerMock = .init(), | ||
imageDownloader: ImageDownloader = TestImageFetcher(result: .success) | ||
) -> AvatarPickerViewModel { | ||
.init( | ||
email: .init("[email protected]"), | ||
authToken: "token", | ||
profileService: ProfileService(urlSession: session), | ||
avatarService: AvatarService(urlSession: session) | ||
avatarService: AvatarService(urlSession: session), | ||
imageDownloader: imageDownloader | ||
) | ||
} | ||
|
||
|
@@ -64,6 +68,103 @@ final class AvatarPickerViewModelTests { | |
} | ||
} | ||
|
||
@Test | ||
func testFetchOriginalSizeAvatarSuccess() async throws { | ||
await model.refresh() | ||
guard let avatar = model.grid.avatars.first else { | ||
#expect(Bool(false), "No avatar found") | ||
return | ||
} | ||
|
||
await confirmation(expectedCount: 2) { confirmation in | ||
model.toastManager.$toasts.sink { toasts in | ||
#expect(toasts.count == 0, "No toast should be shown in success case") | ||
confirmation.confirm() | ||
}.store(in: &cancellables) | ||
|
||
var observedStates: [AvatarImageModel.State] = [] | ||
model.grid.$avatars.sink { models in | ||
observedStates.append(models[0].state) | ||
if observedStates.count == 3 { | ||
#expect(observedStates[0] == .loaded) | ||
#expect(observedStates[1] == .loading) | ||
#expect(observedStates[2] == .loaded) | ||
confirmation.confirm() | ||
} | ||
}.store(in: &cancellables) | ||
let result = await model.fetchOriginalSizeAvatar(for: avatar) | ||
#expect(result != nil) | ||
} | ||
} | ||
|
||
@Test | ||
func testFetchOriginalSizeFailsWithURLSessionError() async throws { | ||
let model = Self.createModel(imageDownloader: TestImageFetcher(result: .urlSessionError)) | ||
await model.refresh() | ||
guard let avatar = model.grid.avatars.first else { | ||
#expect(Bool(false), "No avatar found") | ||
return | ||
} | ||
|
||
await confirmation(expectedCount: 2) { confirmation in | ||
model.toastManager.$toasts.sink { toasts in | ||
#expect(toasts.count <= 1) | ||
if toasts.count == 1 { | ||
#expect(toasts.first?.message == TestImageFetcher.sessionErrorMessage) | ||
#expect(toasts.first?.type == .error) | ||
confirmation.confirm() | ||
} | ||
}.store(in: &cancellables) | ||
|
||
var observedStates: [AvatarImageModel.State] = [] | ||
model.grid.$avatars.sink { models in | ||
observedStates.append(models[0].state) | ||
if observedStates.count == 3 { | ||
#expect(observedStates[0] == .loaded) | ||
#expect(observedStates[1] == .loading) | ||
#expect(observedStates[2] == .loaded) | ||
confirmation.confirm() | ||
} | ||
}.store(in: &cancellables) | ||
let result = await model.fetchOriginalSizeAvatar(for: avatar) | ||
#expect(result == nil) | ||
} | ||
} | ||
|
||
@Test | ||
func testFetchOriginalSizeFailsWithGenericError() async throws { | ||
let model = Self.createModel(imageDownloader: TestImageFetcher(result: .fail)) | ||
await model.refresh() | ||
guard let avatar = model.grid.avatars.first else { | ||
#expect(Bool(false), "No avatar found") | ||
return | ||
} | ||
|
||
await confirmation(expectedCount: 2) { confirmation in | ||
model.toastManager.$toasts.sink { toasts in | ||
#expect(toasts.count <= 1) | ||
if toasts.count == 1 { | ||
#expect(toasts.first?.message == AvatarPickerViewModel.Localized.avatarDownloadFail) | ||
#expect(toasts.first?.type == .error) | ||
confirmation.confirm() | ||
} | ||
}.store(in: &cancellables) | ||
|
||
var observedStates: [AvatarImageModel.State] = [] | ||
model.grid.$avatars.sink { models in | ||
observedStates.append(models[0].state) | ||
if observedStates.count == 3 { | ||
#expect(observedStates[0] == .loaded) | ||
#expect(observedStates[1] == .loading) | ||
#expect(observedStates[2] == .loaded) | ||
confirmation.confirm() | ||
} | ||
}.store(in: &cancellables) | ||
let result = await model.fetchOriginalSizeAvatar(for: avatar) | ||
#expect(result == nil) | ||
} | ||
} | ||
|
||
@Test | ||
func testUploadAvatar() async throws { | ||
model.grid.setAvatars([]) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been a
@Published
var from the beginning imo. Otherwise the UI can fail to get updated properly.