Skip to content
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

[iOS] - Fix Leo QA - 2 #22578

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions ios/brave-ios/Sources/AIChat/Components/AIChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct AIChatView: View {
private var lastMessageId

@State
private var customFeedbackIndex: Int?
private var customFeedbackInfo: AIChatFeedbackModelToast?

@State
private var isPremiumPaywallPresented = false
Expand All @@ -37,6 +37,9 @@ public struct AIChatView: View {
@ObservedObject
private var hasSeenIntro = Preferences.AIChat.hasSeenIntro

@ObservedObject
private var shouldShowFeedbackPremiumAd = Preferences.AIChat.showPremiumFeedbackAd

var openURL: ((URL) -> Void)

public init(model: AIChatViewModel, openURL: @escaping (URL) -> Void) {
Expand Down Expand Up @@ -129,7 +132,7 @@ public struct AIChatView: View {
responseContextMenuItems(for: index, turn: turn)
}

if let feedbackIndex = customFeedbackIndex, feedbackIndex == index {
if let feedbackInfo = customFeedbackInfo, feedbackInfo.turnId == index {
feedbackView
}
}
Expand Down Expand Up @@ -190,7 +193,7 @@ public struct AIChatView: View {
.onChange(of: model.conversationHistory) { _ in
scrollViewReader.scrollTo(lastMessageId, anchor: .bottom)
}
.onChange(of: customFeedbackIndex) { _ in
.onChange(of: customFeedbackInfo) { _ in
hideKeyboard()
withAnimation {
scrollViewReader.scrollTo(lastMessageId, anchor: .bottom)
Expand Down Expand Up @@ -376,11 +379,11 @@ public struct AIChatView: View {
onSelected: {
Task { @MainActor in
let ratingId = await model.rateConversation(isLiked: false, turnId: UInt(turnIndex))
if ratingId != nil {
if let ratingId = ratingId {
feedbackToast = .success(
isLiked: false,
onAddFeedback: {
customFeedbackIndex = turnIndex
customFeedbackInfo = AIChatFeedbackModelToast(turnId: turnIndex, ratingId: ratingId)
}
)
} else {
Expand Down Expand Up @@ -429,8 +432,10 @@ public struct AIChatView: View {

private var feedbackView: some View {
AIChatFeedbackView(
premiumStatus: model.premiumStatus,
shouldShowPremiumAd: $shouldShowFeedbackPremiumAd.value,
onSubmit: { category, feedback in
guard let feedbackIndex = customFeedbackIndex else {
guard let feedbackInfo = customFeedbackInfo else {
feedbackToast = .error(message: Strings.AIChat.feedbackSubmittedErrorTitle)
return
}
Expand All @@ -439,18 +444,18 @@ public struct AIChatView: View {
let success = await model.submitFeedback(
category: category,
feedback: feedback,
ratingId: "\(feedbackIndex)"
ratingId: feedbackInfo.ratingId
)

feedbackToast =
success
? .success(isLiked: true) : .error(message: Strings.AIChat.feedbackSubmittedErrorTitle)
? .submitted : .error(message: Strings.AIChat.feedbackSubmittedErrorTitle)
}

customFeedbackIndex = nil
customFeedbackInfo = nil
},
onCancel: {
customFeedbackIndex = nil
customFeedbackInfo = nil
feedbackToast = .none
}
)
Expand All @@ -459,7 +464,7 @@ public struct AIChatView: View {
\.openURL,
OpenURLAction { url in
if url.host == "dismiss" {
//TODO: Dismiss feedback learn-more prompt
shouldShowFeedbackPremiumAd.value = false
} else {
openURL(url)
dismiss()
Expand Down Expand Up @@ -523,6 +528,8 @@ struct AIChatView_Preview: PreviewProvider {
.background(Color(braveSystemName: .containerBackground))

AIChatFeedbackView(
premiumStatus: .inactive,
shouldShowPremiumAd: .constant(true),
onSubmit: {
print("Submitted Feedback: \($0) -- \($1)")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import AVFoundation
import BraveCore
import DesignSystem
import Introspect
import SpeechRecognition
Expand Down Expand Up @@ -324,13 +325,24 @@ enum AIChatFeedbackOption: String, CaseIterable, Identifiable {
}
}

struct AIChatFeedbackModelToast: Equatable {
let turnId: Int
let ratingId: String
}

struct AIChatFeedbackView: View {
@State
private var category: AIChatFeedbackOption = .notHelpful

@State
private var feedbackText: String = ""

@State
var premiumStatus: AiChat.PremiumStatus
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need @State, you don't set it anywhere


@Binding
var shouldShowPremiumAd: Bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need @Binding, you don't set it anywhere


let onSubmit: (String, String) -> Void
let onCancel: () -> Void

Expand All @@ -353,8 +365,10 @@ struct AIChatFeedbackView: View {
AIChatFeedbackInputView(text: $feedbackText)
.padding([.horizontal, .bottom])

AIChatFeedbackLeoPremiumAdView()
.padding(.horizontal)
if premiumStatus != .active && premiumStatus != .activeDisconnected && shouldShowPremiumAd {
AIChatFeedbackLeoPremiumAdView()
.padding(.horizontal)
}

HStack {
Button {
Expand Down Expand Up @@ -385,6 +399,8 @@ struct AIChatFeedbackView: View {
struct AIChatFeedbackView_Previews: PreviewProvider {
static var previews: some View {
AIChatFeedbackView(
premiumStatus: .inactive,
shouldShowPremiumAd: .constant(true),
onSubmit: {
print("Submitted Feedback: \($0) -- \($1)")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ extension Preferences {
key: "aichat.autocompletesuggestions-enabled",
default: true
)
public static let showPremiumFeedbackAd = Option<Bool>(
key: "aichat.show-premium-feedback-ad",
default: true
)
}
}

Expand Down
Loading