-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alessandro/onboarding choose app icon (#3330)
Task/Issue URL:mhttps://app.asana.com/0/1206329551987282/1208084960726996/f **Description**: Add AppIcon screen selection to the onboarding.
- Loading branch information
1 parent
ddbd2d5
commit 4603706
Showing
6 changed files
with
386 additions
and
9 deletions.
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
73 changes: 73 additions & 0 deletions
73
DuckDuckGo/OnboardingExperiment/AppIconPicker/AppIconPicker.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,73 @@ | ||
// | ||
// AppIconPicker.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import SwiftUI | ||
import DuckUI | ||
|
||
private enum Metrics { | ||
static let cornerRadius: CGFloat = 13.0 | ||
static let iconSize: CGFloat = 56.0 | ||
static let spacing: CGFloat = 16.0 | ||
static let strokeFrameSize: CGFloat = 60 | ||
static let strokeWidth: CGFloat = 3 | ||
static let strokeInset: CGFloat = 1.5 | ||
} | ||
|
||
struct AppIconPicker: View { | ||
@Environment(\.colorScheme) private var color | ||
|
||
@StateObject private var viewModel = AppIconPickerViewModel() | ||
|
||
let layout = [GridItem(.adaptive(minimum: Metrics.iconSize, maximum: Metrics.iconSize), spacing: Metrics.spacing)] | ||
|
||
var body: some View { | ||
LazyVGrid(columns: layout, spacing: Metrics.spacing) { | ||
ForEach(viewModel.items, id: \.icon) { item in | ||
Image(uiImage: item.icon.mediumImage ?? UIImage()) | ||
.resizable() | ||
.frame(width: Metrics.iconSize, height: Metrics.iconSize) | ||
.cornerRadius(Metrics.cornerRadius) | ||
.overlay { | ||
strokeOverlay(isSelected: item.isSelected) | ||
} | ||
.onTapGesture { | ||
viewModel.changeApp(icon: item.icon) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private func strokeOverlay(isSelected: Bool) -> some View { | ||
if isSelected { | ||
RoundedRectangle(cornerRadius: Metrics.cornerRadius) | ||
.foregroundColor(.clear) | ||
.frame(width: Metrics.strokeFrameSize, height: Metrics.strokeFrameSize) | ||
.overlay( | ||
RoundedRectangle(cornerRadius: Metrics.cornerRadius) | ||
.inset(by: -Metrics.strokeInset) | ||
.stroke(.blue, lineWidth: Metrics.strokeWidth) | ||
) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
AppIconPicker() | ||
} |
64 changes: 64 additions & 0 deletions
64
DuckDuckGo/OnboardingExperiment/AppIconPicker/AppIconPickerViewModel.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,64 @@ | ||
// | ||
// AppIconPickerViewModel.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
@MainActor | ||
final class AppIconPickerViewModel: ObservableObject { | ||
|
||
struct DisplayModel { | ||
let icon: AppIcon | ||
let isSelected: Bool | ||
} | ||
|
||
@Published private(set) var items: [DisplayModel] = [] | ||
|
||
private let appIconManager: AppIconManaging | ||
|
||
init(appIconManager: AppIconManaging = AppIconManager.shared) { | ||
self.appIconManager = appIconManager | ||
items = makeDisplayModels() | ||
} | ||
|
||
func changeApp(icon: AppIcon) { | ||
appIconManager.changeAppIcon(icon) { [weak self] error in | ||
guard let self, error == nil else { return } | ||
items = makeDisplayModels() | ||
} | ||
} | ||
|
||
private func makeDisplayModels() -> [DisplayModel] { | ||
AppIcon.allCases.map { appIcon in | ||
DisplayModel(icon: appIcon, isSelected: appIconManager.appIcon == appIcon) | ||
} | ||
} | ||
} | ||
|
||
protocol AppIconManaging { | ||
var appIcon: AppIcon { get } | ||
func changeAppIcon(_ appIcon: AppIcon, completionHandler: ((Error?) -> Void)?) | ||
} | ||
|
||
extension AppIconManaging { | ||
func changeAppIcon(_ appIcon: AppIcon) { | ||
changeAppIcon(appIcon, completionHandler: nil) | ||
} | ||
} | ||
|
||
extension AppIconManager: AppIconManaging {} |
88 changes: 88 additions & 0 deletions
88
DuckDuckGo/OnboardingExperiment/OnboardingIntro/OnboardingView+AppIconPickerContent.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,88 @@ | ||
// | ||
// OnboardingView+AppIconPickerContent.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import SwiftUI | ||
import DuckUI | ||
import Onboarding | ||
|
||
extension OnboardingView { | ||
|
||
struct AppIconPickerContentState { | ||
var animateTitle = true | ||
var animateMessage = false | ||
var showContent = false | ||
} | ||
|
||
struct AppIconPickerContent: View { | ||
|
||
private var animateTitle: Binding<Bool> | ||
private var animateMessage: Binding<Bool> | ||
private var showContent: Binding<Bool> | ||
private let action: () -> Void | ||
|
||
init( | ||
animateTitle: Binding<Bool> = .constant(true), | ||
animateMessage: Binding<Bool> = .constant(true), | ||
showContent: Binding<Bool> = .constant(false), | ||
action: @escaping () -> Void | ||
) { | ||
self.animateTitle = animateTitle | ||
self.animateMessage = animateMessage | ||
self.showContent = showContent | ||
self.action = action | ||
} | ||
|
||
var body: some View { | ||
VStack(spacing: 16.0) { | ||
AnimatableTypingText(UserText.HighlightsOnboardingExperiment.AppIconSelection.title, startAnimating: animateTitle) { | ||
animateMessage.wrappedValue = true | ||
} | ||
.foregroundColor(.primary) | ||
.font(Metrics.titleFont) | ||
|
||
AnimatableTypingText(UserText.HighlightsOnboardingExperiment.AppIconSelection.message, startAnimating: animateMessage) { | ||
withAnimation { | ||
showContent.wrappedValue = true | ||
} | ||
} | ||
.foregroundColor(.primary) | ||
.font(Metrics.messageFont) | ||
|
||
VStack(spacing: 24) { | ||
AppIconPicker() | ||
.offset(x: Metrics.pickerLeadingOffset) // Remove left padding for the first item | ||
|
||
Button(action: action) { | ||
Text(UserText.HighlightsOnboardingExperiment.AppIconSelection.cta) | ||
} | ||
.buttonStyle(PrimaryButtonStyle()) | ||
} | ||
.visibility(showContent.wrappedValue ? .visible : .invisible) | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
private enum Metrics { | ||
static let titleFont = Font.system(size: 20, weight: .semibold) | ||
static let messageFont = Font.system(size: 16) | ||
static let pickerLeadingOffset: CGFloat = -20 | ||
} |
Oops, something went wrong.