Hook up Damus Purple translation service

This commit integrates the Damus Purple translation service:
- Automatically handles translation settings change after purchase
- Asks for permission to override translation settings if the user already has translation setup
- Translation settings can be changed with Damus Purple, if desired
- Translation requests working with the Damus API server

Testing
--------

PASS

Device: iPhone 15 simulator
iOS: 17.2
Damus: This commit
Damus Purple API server: `9397201d7d55ddcec4c18fcd337f759b61dce697` running on Ubuntu 22.04 LTS VM (npm run dev)
iOS setting: English set as the only preferred language.
Steps:
1. Enable Damus Purple feature flag on developer settings, set purple localhost mode, and restart app
2. Set translation setting to something other than none (e.g. DeepL)
3. Simulate Damus Purple purchase
4. Check that when dismissing welcome view, a confirmation prompt will ask the user whether they want to switch translator to Damus Purple. PASS
5. Click "Yes".
6. Go to translation settings. Check that translation settings are set to "Purple". PASS
7. Go to a non-English profile. Check that translations appear with "Mock translation" (Which is the translation text provided by the mock translation server). PASS
8. Reinstall app
9. Repeat the test, but this time starting with no translation settings. Make sure that translation settings will automatically switch to Damus Purple. PASS

Feature flag testing
--------------------

PASS

Preconditions: Same as above
Steps:
1. Turn off translation
2. Turn off Damus Purple feature flag
3. Go to translation settings. Make sure that Damus Purple is not an option. PASS

Closes: https://github.com/damus-io/damus/issues/1836
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2023-12-30 04:31:32 +00:00
committed by William Casarin
parent 39b6dfb47e
commit 9a547077c1
10 changed files with 103 additions and 18 deletions

View File

@@ -42,7 +42,7 @@ struct PurchasedProduct {
}
struct DamusPurpleView: View {
let purple_api: DamusPurple
let damus_state: DamusState
let keypair: Keypair
@State var products: ProductState
@@ -50,13 +50,14 @@ struct DamusPurpleView: View {
@State var selection: DamusPurpleType = .yearly
@State var show_welcome_sheet: Bool = false
@State var show_manage_subscriptions = false
@State var show_settings_change_confirmation_dialog = false
@Environment(\.dismiss) var dismiss
init(purple: DamusPurple, keypair: Keypair) {
init(damus_state: DamusState) {
self._products = State(wrappedValue: .loading)
self.purple_api = purple
self.keypair = keypair
self.damus_state = damus_state
self.keypair = damus_state.keypair
}
var body: some View {
@@ -94,12 +95,38 @@ struct DamusPurpleView: View {
await load_products()
}
.ignoresSafeArea(.all)
.sheet(isPresented: $show_welcome_sheet, content: {
.sheet(isPresented: $show_welcome_sheet, onDismiss: {
update_user_settings_to_purple()
}, content: {
DamusPurpleWelcomeView()
})
.confirmationDialog(
NSLocalizedString("It seems that you already have a translation service configured. Would you like to switch to Damus Purple as your translator?", comment: "Confirmation dialog question asking users if they want their translation settings to be automatically switched to the Damus Purple translation service"),
isPresented: $show_settings_change_confirmation_dialog,
titleVisibility: .visible
) {
Button("Yes") {
set_translation_settings_to_purple()
}.keyboardShortcut(.defaultAction)
Button("No", role: .cancel) {}
}
.manageSubscriptionsSheet(isPresented: $show_manage_subscriptions)
}
func update_user_settings_to_purple() {
if damus_state.settings.translation_service == .none {
set_translation_settings_to_purple()
}
else {
show_settings_change_confirmation_dialog = true
}
}
func set_translation_settings_to_purple() {
damus_state.settings.translation_service = .purple
damus_state.settings.auto_translate = true
}
func handle_transactions(products: [Product]) async {
for await update in StoreKit.Transaction.updates {
switch update {
@@ -203,9 +230,9 @@ struct DamusPurpleView: View {
switch result {
case .success:
self.purple_api.starred_profiles_cache[keypair.pubkey] = nil
self.damus_state.purple.starred_profiles_cache[keypair.pubkey] = nil
Task {
await self.purple_api.send_receipt()
await self.damus_state.purple.send_receipt()
}
default:
break
@@ -423,6 +450,6 @@ struct DamusPurpleView_Previews: PreviewProvider {
])
*/
DamusPurpleView(purple: test_damus_state.purple, keypair: test_damus_state.keypair)
DamusPurpleView(damus_state: test_damus_state)
}
}

View File

@@ -9,6 +9,7 @@ import SwiftUI
struct TranslationSettingsView: View {
@ObservedObject var settings: UserSettingsStore
var damus_state: DamusState
@Environment(\.dismiss) var dismiss
@@ -19,11 +20,17 @@ struct TranslationSettingsView: View {
.toggleStyle(.switch)
Picker(NSLocalizedString("Service", comment: "Prompt selection of translation service provider."), selection: $settings.translation_service) {
ForEach(TranslationService.allCases, id: \.self) { server in
ForEach(TranslationService.allCases.filter({ settings.enable_experimental_purple_api ? true : $0 != .purple }), id: \.self) { server in
Text(server.model.displayName)
.tag(server.model.tag)
}
}
if settings.translation_service == .purple && settings.enable_experimental_purple_api {
NavigationLink(destination: DamusPurpleView(damus_state: damus_state)) {
Text(NSLocalizedString("Configure Damus Purple", comment: "Button to allow Damus Purple to be configured"))
}
}
if settings.translation_service == .libretranslate {
Picker(NSLocalizedString("Server", comment: "Prompt selection of LibreTranslate server to perform machine translations on notes"), selection: $settings.libretranslate_server) {
@@ -103,6 +110,6 @@ struct TranslationSettingsView: View {
struct TranslationSettingsView_Previews: PreviewProvider {
static var previews: some View {
TranslationSettingsView(settings: UserSettingsStore())
TranslationSettingsView(settings: UserSettingsStore(), damus_state: test_damus_state)
}
}

View File

@@ -55,7 +55,7 @@ struct SideMenuView: View {
}
if damus_state.settings.enable_experimental_purple_api {
NavigationLink(destination: DamusPurpleView(purple: damus_state.purple, keypair: damus_state.keypair)) {
NavigationLink(destination: DamusPurpleView(damus_state: damus_state)) {
HStack(spacing: 13) {
Image("nostr-hashtag")
Text("Purple")