Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: lyrics translate into any language with your own Gemini key
My-Name-Is-Jeff3655791803e0
diff --git a/harness/lyrics/build.sh b/harness/lyrics/build.shindex d182aead..008d25b4 100755--- a/harness/lyrics/build.sh+++ b/harness/lyrics/build.sh@@ -18,7 +18,7 @@ fiSDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g ${OPT:--O0} \-I"$SRC" -I"$SRC/Redesigned/Lyrics" -isysroot "$SDK" -Wall -Wno-deprecated-declarations \- "$HERE/main.m" "$HERE/stubs.m" \+ "$HERE/main.m" "$HERE/../scene.m" "$HERE/stubs.m" \"$SRC"/Redesigned/Lyrics/SGRKaraokeView.m $( [ -f "$SRC"/Redesigned/Lyrics/LyricsText.m ] && echo "$SRC"/Redesigned/Lyrics/LyricsText.m ) "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/Lyrics/Protobuf.m \"$SRC"/Shared/LyricsSources/SGTTML.m "$SRC"/Redesigned/Kit/SGRTokens.m \$( [ -f "$SRC"/Shared/LyricsMeanings/Meanings.m ] && echo "$SRC"/Shared/LyricsMeanings/Meanings.m "$SRC"/Redesigned/Lyrics/MeaningSheet.m ) \@@ -40,6 +40,12 @@ cat > "$APP/Info.plist" <<PLIST<key>UILaunchScreen</key><dict/><key>UIApplicationSceneManifest</key><dict><key>UIApplicationSupportsMultipleScenes</key><false/>+ <key>UISceneConfigurations</key><dict>+ <key>UIWindowSceneSessionRoleApplication</key><array><dict>+ <key>UISceneConfigurationName</key><string>Default</string>+ <key>UISceneDelegateClassName</key><string>SGRHarnessScene</string>+ </dict></array>+ </dict></dict></dict></plist>PLISTdiff --git a/harness/lyrics/stubs.m b/harness/lyrics/stubs.mindex 7b90a8ee..287976b5 100644--- a/harness/lyrics/stubs.m+++ b/harness/lyrics/stubs.m@@ -16,6 +16,16 @@ NSString *SGLyricsCreditFor(NSString *trackID) { return @"the harness"; }// -translateTo es: the language the Lyrics page would ask translations for.NSString *SGLyricsTranslationLanguage(void) { return [NSUserDefaults.standardUserDefaults stringForKey:@"translateTo"]; }+// -gemini 1: a Gemini key is set, and Translate with Gemini answers each line with itself, marked.+BOOL SGGeminiKeySet(void) { return [NSUserDefaults.standardUserDefaults boolForKey:@"gemini"]; }+NSString *SGLyricsGeminiLanguage(void) { return SGLyricsTranslationLanguage() ?: @"en"; }+void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *lines, NSString *languageTag,+ void (^done)(NSArray<NSString *> *translations, NSString *error)) {+ NSMutableArray<NSString *> *translations = [NSMutableArray array];+ for (SGKaraokeLine *line in lines) [translations addObject:[@"Gemini: " stringByAppendingString:SGKaraokeLineText(line)]];+ done(translations, nil);+}+// Line meanings: -title and -artist name the track Genius is searched for, and the setting's key// (-spotifyglass.lyricsMeanings 3) turns them on.@interface SGHarnessTrack : NSObjectdiff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.mindex 1b79fbea..04e74f90 100644--- a/tweak/Sources/App/Pages.m+++ b/tweak/Sources/App/Pages.m@@ -1,4 +1,5 @@#import "Core/SGCore.h"+#import "Shared/LyricsTranslation/LyricsTranslation.h"#import "Redesigned/Player/Player.h"#import "Settings/SGModPage.h"#import "Settings/SGPageStyle.h"@@ -79,7 +80,8 @@ static UIViewController *lyricsPage(void) {NSMutableArray<SGModSection *> *sections = [NSMutableArray arrayWithObject:SGLyricsSourcesSection(redesigned)];if (redesigned) {[sections addObject:SGSection(@"Display", @[SGLyricsWordTimingRow(), SGRLyricsTextSizesRow(), SGLyricsTranslationLanguageRow(), SGLyricsMeaningsRow(),- SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide)])];+ SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide),+ SGGeminiKeyRow()])];}[sections addObject:SGSection(nil, more)];return [[SGModPage alloc] initWithTitle:@"Lyrics" intro:SGRestartNote sections:sections footer:nil];diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex eb0b454d..b65aef3f 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -12,6 +12,8 @@#import "LyricsText.h"#import "MeaningSheet.h"#import "Shared/LyricsSources/LyricsSources.h"+#import "Shared/LyricsTranslation/LyricsTranslation.h"+#import "Settings/SGPageStyle.h"#import "Shared/Player/PlayerEvents.h"#import "Shared/Haptics/Haptics.h"#import "Redesigned/Kit/SGRTokens.h"@@ -1436,7 +1438,8 @@ typedef struct {// The button shows only for a song with a pronunciation or a translation to show, and its menu only// what the song has: a switch for each, reading what tapping it will do.- (void)offerExtras {- BOOL offered = _lines && (_hasSpoken || _hasTranslation);+ BOOL gemini = SGGeminiKeySet();+ BOOL offered = _lines && (_hasSpoken || _hasTranslation || gemini);if (!offered) {_extras.hidden = YES;return;@@ -1477,11 +1480,40 @@ typedef struct {image:[UIImage systemImageNamed:@"character.bubble"] identifier:nilhandler:^(UIAction *action) { SGRSetLyricsTextShown(SGRLyricsTextTranslation, !on); }]];}+ if (gemini && !_hasTranslation) {+ __weak SGRKaraokeView *weakSelf = self;+ [items addObject:[UIAction actionWithTitle:@"Translate with Gemini" image:[UIImage systemImageNamed:@"sparkles"]+ identifier:nil handler:^(UIAction *action) { [weakSelf translateWithGemini]; }]];+ }_extras.menu = [UIMenu menuWithChildren:items];_extras.hidden = NO;[self setNeedsLayout];}+// The song's lines into the Lyrics page's language, or the phone's: each line takes its translation, and+// translations are switched on so the answer shows.+- (void)translateWithGemini {+ NSArray<SGKaraokeLine *> *lines = _lines;+ NSString *track = _track;+ if (!lines.count) return;+ SGLyricsTranslateWithGemini(track, lines, SGLyricsGeminiLanguage(), ^(NSArray<NSString *> *translations, NSString *error) {+ if (!translations) {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No translation" message:error+ preferredStyle:UIAlertControllerStyleAlert];+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];+ [SGTopController() presentViewController:alert animated:YES completion:nil];+ return;+ }+ if (lines != self->_lines) return; // the song moved on meanwhile+ [lines enumerateObjectsUsingBlock:^(SGKaraokeLine *line, NSUInteger i, BOOL *stop) {+ if (translations[i].length) line.translation = translations[i];+ }];+ self->_hasTranslation = YES;+ if (SGFlag(SGRKeyLyricsTranslation, NO)) [self restyle];+ else SGRSetLyricsTextShown(SGRLyricsTextTranslation, YES);+ });+}+// Where a line starts on the page, for the stack as it is arranged now: an open break holds the room// of one row at the anchor, and the lines from the one after it on are moved down by it.- (CGFloat)topOfLine:(NSInteger)index {diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.hnew file mode 100644index 00000000..3235c173--- /dev/null+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h@@ -0,0 +1,19 @@+// A song's lines translated into any language by Gemini, on the user's own API key, at a tap in the+// lyrics' corner menu. The key lives in the Keychain, not in the settings, so a settings export never+// carries it. Only the lyrics' text and the language go out, to Google's Gemini API.+#import <UIKit/UIKit.h>++@class SGKaraokeLine, SGModRow;++BOOL SGGeminiKeySet(void);++// One translation per line, in order, or nil and a message to show. Main queue. The same song in the+// same language is asked once a launch.+void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *lines, NSString *languageTag,+ void (^done)(NSArray<NSString *> *translations, NSString *error));++// The language a translation is asked in: the Lyrics page's, else the phone's own.+NSString *SGLyricsGeminiLanguage(void);++// The Lyrics page's row: shows whether a key is set, and sets or removes it.+SGModRow *SGGeminiKeyRow(void);diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.mnew file mode 100644index 00000000..5501c7f4--- /dev/null+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m@@ -0,0 +1,146 @@+#import <Security/Security.h>+#import "Core/SGCore.h"+#import "Settings/SGModPage.h"+#import "Settings/SGPageStyle.h"+#import "Shared/Lyrics/Lyrics.h"+#import "Shared/LyricsSources/LyricsSources.h"+#import "LyricsTranslation.h"++// gemini-flash-latest follows each Flash release; Google gives two weeks' notice of a breaking change.+static NSString *const kEndpoint = @"https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";+static NSString *const kService = @"Vitrine.Gemini", *const kAccount = @"api-key";++#pragma mark - the key++static NSDictionary *keyQuery(void) {+ return @{(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,+ (__bridge id)kSecAttrService: kService, (__bridge id)kSecAttrAccount: kAccount};+}++static NSString *storedKey(void) {+ NSMutableDictionary *query = [keyQuery() mutableCopy];+ query[(__bridge id)kSecReturnData] = @YES;+ query[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;+ CFTypeRef data = NULL;+ if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &data) != errSecSuccess || !data) return nil;+ NSString *key = [[NSString alloc] initWithData:(__bridge_transfer NSData *)data encoding:NSUTF8StringEncoding];+ return key.length ? key : nil;+}++static void storeKey(NSString *key) {+ SecItemDelete((__bridge CFDictionaryRef)keyQuery());+ if (!key.length) return;+ NSMutableDictionary *item = [keyQuery() mutableCopy];+ item[(__bridge id)kSecValueData] = [key dataUsingEncoding:NSUTF8StringEncoding];+ item[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;+ OSStatus status = SecItemAdd((__bridge CFDictionaryRef)item, NULL);+ if (status != errSecSuccess) SGLog(@"gemini: the key could not be kept (%d)", (int)status);+}++BOOL SGGeminiKeySet(void) {+ return storedKey() != nil;+}++#pragma mark - asking++NSString *SGLyricsGeminiLanguage(void) {+ return SGLyricsTranslationLanguage() ?: [NSLocale.preferredLanguages.firstObject componentsSeparatedByString:@"-"].firstObject ?: @"en";+}++static NSMutableDictionary<NSString *, NSArray<NSString *> *> *sg_done; // per launch, by track and language++void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *lines, NSString *languageTag,+ void (^done)(NSArray<NSString *> *translations, NSString *error)) {+ NSString *key = storedKey();+ if (!key) {+ done(nil, @"Add a Gemini API key on the Lyrics page first.");+ return;+ }+ if (!sg_done) sg_done = [NSMutableDictionary dictionary];+ NSString *memo = [NSString stringWithFormat:@"%@|%@", trackID, languageTag];+ if (trackID && sg_done[memo].count == lines.count) {+ done(sg_done[memo], nil);+ return;+ }+ NSMutableArray<NSString *> *texts = [NSMutableArray arrayWithCapacity:lines.count];+ for (SGKaraokeLine *line in lines) [texts addObject:SGKaraokeLineText(line) ?: @""];+ NSString *language = [[NSLocale localeWithLocaleIdentifier:@"en"] localizedStringForLanguageCode:languageTag] ?: languageTag;+ NSData *input = [NSJSONSerialization dataWithJSONObject:texts options:0 error:nil];+ NSString *prompt = [NSString stringWithFormat:+ @"Translate the lines of this song into %@. Answer with a JSON array of exactly %lu strings, one "+ @"per input line and in the same order. Translate the meaning naturally, keep each line short like a "+ @"lyric, and keep an empty line empty. A line already in %@ stays as it is.\n\n%@",+ language, (unsigned long)texts.count, language, [[NSString alloc] initWithData:input encoding:NSUTF8StringEncoding]];+ NSDictionary *body = @{+ @"contents": @[@{@"parts": @[@{@"text": prompt}]}],+ @"generationConfig": @{+ @"responseMimeType": @"application/json",+ @"responseSchema": @{@"type": @"ARRAY", @"items": @{@"type": @"STRING"}},+ },+ };+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kEndpoint]];+ request.HTTPMethod = @"POST";+ request.timeoutInterval = 60;+ [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];+ [request setValue:key forHTTPHeaderField:@"x-goog-api-key"];+ request.HTTPBody = [NSJSONSerialization dataWithJSONObject:body options:0 error:nil];+ [[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;+ id root = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;+ NSArray *translations = nil;+ NSString *problem = nil;+ id candidates = [root isKindOfClass:NSDictionary.class] ? root[@"candidates"] : nil;+ id first = [candidates isKindOfClass:NSArray.class] ? [candidates firstObject] : nil;+ id parts = [first isKindOfClass:NSDictionary.class] && [first[@"content"] isKindOfClass:NSDictionary.class] ? first[@"content"][@"parts"] : nil;+ id text = [parts isKindOfClass:NSArray.class] && [[parts firstObject] isKindOfClass:NSDictionary.class] ? [parts firstObject][@"text"] : nil;+ id parsed = [text isKindOfClass:NSString.class] ? [NSJSONSerialization JSONObjectWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] : nil;+ if ([parsed isKindOfClass:NSArray.class] && [parsed count] == texts.count) {+ NSMutableArray<NSString *> *clean = [NSMutableArray arrayWithCapacity:texts.count];+ for (id line in parsed) [clean addObject:[line isKindOfClass:NSString.class] ? line : @""];+ translations = clean;+ } else if (error) {+ problem = @"Gemini could not be reached.";+ } else if (status == 400 || status == 403) {+ problem = @"Gemini turned the key down. Check it on the Lyrics page.";+ } else if (status == 429) {+ problem = @"Gemini's limit for this key is reached for now.";+ } else {+ problem = [NSString stringWithFormat:@"Gemini did not translate the song (%ld).", (long)status];+ }+ SGLog(@"gemini: %@ lines into %@: %@", @(texts.count), languageTag, translations ? @"translated" : problem);+ dispatch_async(dispatch_get_main_queue(), ^{+ if (translations && trackID) sg_done[memo] = translations;+ done(translations, problem);+ });+ }] resume];+}++#pragma mark - the row++static void askForKey(void) {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Gemini API key"+ message:@"Lyrics translate into any language with your own key, from Google AI Studio. It stays on this iPhone."+ preferredStyle:UIAlertControllerStyleAlert];+ [alert addTextFieldWithConfigurationHandler:^(UITextField *field) {+ field.placeholder = @"API key";+ field.secureTextEntry = YES;+ field.autocorrectionType = UITextAutocorrectionTypeNo;+ field.autocapitalizationType = UITextAutocapitalizationTypeNone;+ }];+ [alert addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {+ NSString *key = [alert.textFields.firstObject.text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];+ storeKey(key);+ }]];+ if (SGGeminiKeySet()) {+ [alert addAction:[UIAlertAction actionWithTitle:@"Remove" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {+ storeKey(nil);+ }]];+ }+ [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];+ [SGTopController() presentViewController:alert animated:YES completion:nil];+}++SGModRow *SGGeminiKeyRow(void) {+ return SGStatActionRow(@"Gemini API key", @"Translate any song from the lyrics' corner menu",+ ^NSString *{ return SGGeminiKeySet() ? @"Set" : @"Off"; }, ^{ askForKey(); });+}