Skip to content

Commit

Permalink
custom truncation length options + remove sp dc cookie length limit (…
Browse files Browse the repository at this point in the history
…lot of emails from users who have abnormally long cookies)
  • Loading branch information
aviwad committed Jan 26, 2024
1 parent d1d1775 commit 905ccff
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions SpotifyLyricsInMenubar/Assets.xcassets/30.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "30.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions SpotifyLyricsInMenubar/Assets.xcassets/40.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "40.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions SpotifyLyricsInMenubar/Assets.xcassets/50.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "50.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions SpotifyLyricsInMenubar/Assets.xcassets/60.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "60.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
64 changes: 56 additions & 8 deletions SpotifyLyricsInMenubar/OnboardingWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct ZeroView: View {
NSWorkspace.shared.open(url)
})
Spacer()
NavigationLink(destination: FirstView(), isActive: $isShowingDetailView) {EmptyView()}
NavigationLink(destination: BetweenZeroAndFirstView(), isActive: $isShowingDetailView) {EmptyView()}
.hidden()
if error && !isLoading {
Text("WRONG SP DC COOKIE TRY AGAIN ⚠️")
Expand All @@ -80,11 +80,7 @@ struct ZeroView: View {
Button("Next") {
Task {
isLoading = true
if spDcCookie.count != 159 {
error = true
isLoading = false
}
else if let url = URL(string: "https://open.spotify.com/get_access_token?reason=transport&productType=web_player") {
if let url = URL(string: "https://open.spotify.com/get_access_token?reason=transport&productType=web_player") {
do {
var request = URLRequest(url: url)
request.setValue("sp_dc=\(spDcCookie)", forHTTPHeaderField: "Cookie")
Expand Down Expand Up @@ -132,14 +128,64 @@ struct ZeroView: View {
}
}

struct BetweenZeroAndFirstView: View {
@Environment(\.dismiss) var dismiss
//@AppStorage("truncationLength") var truncationLength: Int = 50
@State var truncationLength: Int = UserDefaults.standard.integer(forKey: "truncationLength")
@Environment(\.controlActiveState) var controlActiveState
let allTruncations = [30,40,50,60]
var body: some View {
VStack(alignment: .leading, spacing: 16) {
StepView(title: "2. Set the Lyric Size", description: "This depends on how much free space you have in your menu bar!")

Image("\(truncationLength)")
.resizable()
.scaledToFit()

HStack {
Spacer()
Picker("Truncation Length", selection: $truncationLength) {
ForEach(allTruncations, id:\.self) { oneThing in
Text("\(oneThing) Characters")
}
}
.pickerStyle(.radioGroup)
Spacer()
}

HStack {
Button("Back") {
dismiss()
}
Spacer()
NavigationLink("Next", destination: FirstView())
.buttonStyle(.borderedProminent)
}

}
.onChange(of: truncationLength) { newLength in
UserDefaults.standard.set(newLength, forKey: "truncationLength")
}
.padding(.horizontal, 20)
.navigationBarBackButtonHidden(true)
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { newValue in
dismiss()
dismiss()
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willMiniaturizeNotification)) { newValue in
dismiss()
dismiss()
}
}
}

struct FirstView: View {
@Environment(\.dismiss) var dismiss
@Environment(\.controlActiveState) var controlActiveState
@State var isAnimating = true
var body: some View {
VStack(alignment: .leading, spacing: 16) {
StepView(title: "2. Make sure you give Automation permission", description: "We need this permission to read the current song from Spotify, so that we can play the correct lyrics! Watch the following gif to correctly give permission.")
StepView(title: "3. Make sure you give Automation permission", description: "We need this permission to read the current song from Spotify, so that we can play the correct lyrics! Watch the following gif to correctly give permission.")

HStack {
Spacer()
Expand Down Expand Up @@ -168,10 +214,12 @@ struct FirstView: View {
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { newValue in
dismiss()
dismiss()
dismiss()
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willMiniaturizeNotification)) { newValue in
dismiss()
dismiss()
dismiss()
}
.onChange(of: controlActiveState) { newState in
if newState == .inactive {
Expand All @@ -189,7 +237,7 @@ struct SecondView: View {
@State var isAnimating = true
var body: some View {
VStack(alignment: .leading, spacing: 16) {
StepView(title: "3. Make sure you disable crossfades", description: "Because of a glitch within Spotify, crossfades make the lyrics appear out-of-sync on occasion.")
StepView(title: "4. Make sure you disable crossfades", description: "Because of a glitch within Spotify, crossfades make the lyrics appear out-of-sync on occasion.")

HStack {
Spacer()
Expand Down
6 changes: 4 additions & 2 deletions SpotifyLyricsInMenubar/SpotifyLyricsInMenubarApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import SwiftUI
import ServiceManagement

@main
struct SpotifyLyricsInMenubarApp: App {
@StateObject var viewmodel = viewModel.shared
@AppStorage("launchOnLogin") var launchOnLogin: Bool = false
@AppStorage("showLyrics") var showLyrics: Bool = true
@AppStorage("hasOnboarded") var hasOnboarded: Bool = false
@AppStorage("truncationLength") var truncationLength: Int = 50
@Environment(\.openWindow) var openWindow
var body: some Scene {
MenuBarExtra(content: {
Expand Down Expand Up @@ -147,9 +149,9 @@ struct SpotifyLyricsInMenubarApp: App {

var menuBarTitle: String {
if viewmodel.isPlaying, showLyrics, let currentlyPlayingLyricsIndex = viewmodel.currentlyPlayingLyricsIndex {
return viewmodel.currentlyPlayingLyrics[currentlyPlayingLyricsIndex].words.trunc(length: 50)
return viewmodel.currentlyPlayingLyrics[currentlyPlayingLyricsIndex].words.trunc(length: truncationLength)
} else if let currentlyPlayingName = viewmodel.currentlyPlayingName {
return "Now \(viewmodel.isPlaying ? "Playing" : "Paused"): \(currentlyPlayingName.trunc(length: 50))"
return "Now \(viewmodel.isPlaying ? "Playing" : "Paused"): \(currentlyPlayingName)".trunc(length: truncationLength)
}
return "Nothing Playing on Spotify"
}
Expand Down
4 changes: 2 additions & 2 deletions SpotifyLyricsInMenubar/viewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ import SwiftUI
decoder.userInfo[CodingUserInfoKey.trackName] = trackName
decoder.userInfo[CodingUserInfoKey.duration] = TimeInterval(intDuration+10)
/*
check if saved access token is bigger than current time, then continue with url shit
check if saved access token is bigger than current time, then continue with lyric fetch
else
check if we have spdc cookie, then access token stuff
then save access token in this observable object
then continue with url shit
then continue with lyric fetch
otherwise []
*/
if accessToken == nil || (accessToken!.accessTokenExpirationTimestampMs <= Date().timeIntervalSince1970*1000) {
Expand Down

0 comments on commit 905ccff

Please sign in to comment.