|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +# ControlKit |
| 5 | + |
| 6 | +**ControlKit** is a minimal Swift Package enabling control of media playback and system volume. |
| 7 | + |
| 8 | +    |
| 9 | + |
| 10 | +### TLDR |
| 11 | + |
| 12 | +Control the device's volume with one line: |
| 13 | + |
| 14 | +```swift |
| 15 | +Control.Volume.increase() // 🔊🆙 |
| 16 | +``` |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Add ControlKit as a dependency in your Xcode project: |
| 21 | + |
| 22 | +1. Go to **File > Add Package Dependencies…** |
| 23 | + |
| 24 | +2. Enter the package URL in the search bar: |
| 25 | + |
| 26 | +``` |
| 27 | +https://github.com/superturboryan/ControlKit.git |
| 28 | +``` |
| 29 | + |
| 30 | +3. Choose the libraries you want to include: |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +## Requirements |
| 35 | + |
| 36 | +[`SpotifyController`](Sources/Controllers/SpotifyController.swift) requires a Spotify _Client ID_ |
| 37 | +and _Redirect URL_ to authorize with & control the Spotify app. |
| 38 | + |
| 39 | +1. [Define a custom URL scheme for your app](https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app). |
| 40 | +Add a `URL Type` to the target's `Info.plist`. |
| 41 | + |
| 42 | +2. Create an app in the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard) |
| 43 | +to get a _client ID_ and register your _redirect URL_ (scheme). |
| 44 | + |
| 45 | +<img width="1007" alt="Screenshot 2024-10-29 at 17 31 24" src="https://github.com/user-attachments/assets/895c8092-4a9d-4526-9f85-5da7b868fbc1"> |
| 46 | + |
| 47 | +### Warning 👇 |
| 48 | + |
| 49 | +The Spotify access token is **not persisted across app launches** by default. |
| 50 | + |
| 51 | +You must provide an object conforming to **`DAO<String>`** if you want the access token to be persisted. |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +### Control |
| 56 | + |
| 57 | +```swift |
| 58 | +import Control |
| 59 | + |
| 60 | +// 🔊 Decrement system volume |
| 61 | +Control.Volume.decreaseVolume() |
| 62 | + |
| 63 | +// 🕵️♂️ Check if audio is being played (by another app) |
| 64 | +if Control.Playback.isAudioPlaying { |
| 65 | + // 💃🕺 |
| 66 | +} |
| 67 | + |
| 68 | +// ⏭️ Skip to next track (Apple Music only - use Controllers.SpotifyController for Spotify 💚) |
| 69 | +Control.Playback.AppleMusic.skipToNextTrack() |
| 70 | + |
| 71 | +// 🫨 Vibrate |
| 72 | +Control.Haptics.vibrate() |
| 73 | +``` |
| 74 | + |
| 75 | +### Controllers |
| 76 | + |
| 77 | +```swift |
| 78 | +// App.swift |
| 79 | + |
| 80 | +import Controllers |
| 81 | +import SwiftUI |
| 82 | + |
| 83 | +@main struct YourApp: App { |
| 84 | + |
| 85 | + @StateObject var spotify = SpotifyController( |
| 86 | + config: .init( |
| 87 | + clientID: Secrets.clientID, |
| 88 | + redirectURL: "controlkit://spotify" |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + var body: some Scene { |
| 93 | + WindowGroup { |
| 94 | + ContentView() |
| 95 | + .environmentObject(spotify) |
| 96 | + .onOpenURL { spotify.setAccessToken(from: $0) } // Parse access token from URL |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + func skipToNextSpotifyTrack() { |
| 101 | + spotify.skipToNextTrack() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Secrets.swift 🔐 |
| 106 | +// Don't forget to gitignore this 🙈 |
| 107 | + |
| 108 | +enum Secrets { |
| 109 | + static let clientID = "<your client id>" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Dependencies |
| 114 | + |
| 115 | +📚 [AVFAudio](https://developer.apple.com/documentation/avfaudio) |
| 116 | +📚 [Media Player](https://developer.apple.com/documentation/mediaplayer/) |
| 117 | +📦 [SpotifyiOS](https://github.com/spotify/ios-sdk) |
| 118 | + |
| 119 | +## Contributing |
| 120 | + |
| 121 | +Contributions and feedback are welcome! 🧑💻👩💻 |
| 122 | + |
| 123 | +Here are a few guidelines: |
| 124 | + |
| 125 | +- You can [open an Issue](https://github.com/superturboryan/ControlKit/issues/new) or raise a PR 🤝 |
| 126 | +- Commit messages should contain emojis ❤️ and be [signed](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits) 🔏 |
| 127 | +- [Workflows](https://github.com/superturboryan/ControlKit/actions) should be green 🟢 |
| 128 | +- `main` should be [linear](https://stackoverflow.com/questions/20348629/what-are-the-advantages-of-keeping-linear-history-in-git) 🎋 |
0 commit comments