Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
76 changes: 76 additions & 0 deletions .github/workflows/ios.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: iOS

on:
push:
branches: [main]
paths: [ios/**, .github/workflows/ios.yml]
pull_request:
paths: [ios/**, .github/workflows/ios.yml]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ios-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
# 想换机器(如 Hayabuild 的 hayabuild-macos),设仓库变量 IOS_RUNNER 即可,不用改这里。
runs-on: ${{ vars.IOS_RUNNER || 'macos-26' }}
timeout-minutes: 45
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version-file: server/go.mod
cache-dependency-path: server/go.sum
- name: 生成 Xcode 工程
run: |
command -v xcodegen >/dev/null || HOMEBREW_NO_AUTO_UPDATE=1 brew install xcodegen
cd ios && xcodegen
- name: 测试 MusicGoKit
run: swift test --package-path ios/MusicGoKit
- name: 选择模拟器
run: |
id="$(xcrun simctl list devices available --json | python3 -c '
import json, sys
found = [(r, d) for r, ds in json.load(sys.stdin)["devices"].items() if "iOS" in r for d in ds if d["name"].startswith("iPhone")]
version = lambda r: [int(n) for n in r.rsplit("iOS-", 1)[1].split("-")]
newest = max((r for r, _ in found), key=version)
print(next(d["udid"] for r, d in found if r == newest))')"
xcrun simctl list devices | grep "$id"
echo "DEVICE=$id" >> "$GITHUB_ENV"
- name: 编译
run: |
set -o pipefail
xcodebuild -version
xcodebuild build-for-testing -quiet -project ios/MusicGo.xcodeproj -scheme MusicGo \
-destination "id=$DEVICE"
- name: 启动测试服务端
run: echo "TEST_RUNNER_MUSICGO_LINK=$(ios/scripts/test-server.sh "$RUNNER_TEMP/server")" >> "$GITHUB_ENV"
- name: 启动模拟器
run: xcrun simctl bootstatus "$DEVICE" -b
- name: 界面测试
id: ui
run: |
xcodebuild test-without-building \
-project ios/MusicGo.xcodeproj -scheme MusicGo -destination "id=$DEVICE" \
-resultBundlePath "$RUNNER_TEMP/result.xcresult"
- name: 取出截图
if: always() && steps.ui.outcome != 'skipped'
run: |
kill "$(cat "$RUNNER_TEMP/server/pid")" || true
xcrun xcresulttool export attachments --path "$RUNNER_TEMP/result.xcresult" \
--output-path "$RUNNER_TEMP/screenshots"
cp "$RUNNER_TEMP/server/log" "$RUNNER_TEMP/screenshots/server.log"
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: always()
with:
name: ios-screenshots
path: ${{ runner.temp }}/screenshots
if-no-files-found: ignore
retention-days: 14
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ filter-allow.txt
.DS_Store
.idea/
.vscode/

# iOS:Xcode 工程由 ios/project.yml 生成
/ios/*.xcodeproj/
/ios/App/Info.plist
.build/
/ios/build/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ http://127.0.0.1:8870/rooms/<房间 ID>#token=<令牌>
|---|---|
| `server/` | Go 服务端和命令行:房间、队列、播放、音源、歌词、飞书 |
| `web/` | 网页大屏:React、TypeScript、Vite |
| `ios/` | iOS 客户端(SwiftUI),见 [ios/README.md](ios/README.md) |
| `mcp/` | 给 AI Agent 用的 MCP 工具:服务端自带的 `/mcp` 远程入口,以及本地程序 |
| `deploy/` | systemd、Caddy 配置,以及发布、备份、升级脚本 |
| `docs/` | 文档 |
Expand Down
2 changes: 2 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ MusicGo 以 [GNU AGPL-3.0](LICENSE) 发布。它包含或改写了下面这些
| [mygoify](https://github.com/aronnaxlin/mygoify)(fork 自 `egoist/mygo-cloudmusic`) | AGPL-3.0 | 歌词,[`server/internal/lyrics`](server/internal/lyrics/NOTICE.md);网页底幕的暗角样式 |
| [Lyricify Lyrics Helper](https://github.com/WXRIW/Lyricify-Lyrics-Helper) | Apache-2.0 | 网易云 EAPI 签名算法 |
| [metadata-filter](https://github.com/web-scrobbler/metadata-filter) | MIT | 标题清理规则 |
| [AppleMusicStylePlayer](https://github.com/f728743/AppleMusicStylePlayer) | MIT | iOS 播放页,[`ios/App/Player`](ios/App/Player/NOTICE.md) |
| [MelismaKit for iOS](https://github.com/kmgcc/melismakit-ios) | AGPL-3.0 | iOS 歌词的滚动和逐字高亮,作为依赖使用,见 [`ios/project.yml`](ios/project.yml) |

所用的提交、改动和许可全文见各目录的 `NOTICE.md`。

Expand Down
37 changes: 37 additions & 0 deletions ios/App/CredentialStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import MusicGoKit
import Security

/// 进入链接里有令牌,所以存钥匙串,且不随备份换到别的设备。
enum CredentialStore {
private static var item: [CFString: Any] {
[
kSecClass: kSecClassGenericPassword,
kSecAttrService: "musicgo.entry-link",
kSecAttrAccount: "default",
]
}

static func load() -> EntryLink? {
var query = item
query[kSecReturnData] = true
var result: CFTypeRef?
guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, let data = result as? Data else {
return nil
}
return try? JSONDecoder().decode(EntryLink.self, from: data)
}

static func save(_ link: EntryLink) {
clear()
guard let data = try? JSONEncoder().encode(link) else { return }
var attributes = item
attributes[kSecValueData] = data
attributes[kSecAttrAccessible] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
SecItemAdd(attributes as CFDictionary, nil)
}

static func clear() {
SecItemDelete(item as CFDictionary)
}
}
62 changes: 62 additions & 0 deletions ios/App/LoginView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import MusicGoKit
import SwiftUI

struct LoginView: View {
let onLogin: (EntryLink) -> Void

@State private var text = ""
@State private var problem: String?
@State private var checking = false

var body: some View {
NavigationStack {
Form {
Section {
TextField("https://…/rooms/…#token=…", text: $text)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.URL)
.submitLabel(.go)
.onSubmit(login)
.accessibilityIdentifier("entry-link")
} header: {
Text("进入链接")
} footer: {
if let problem {
Text(problem).foregroundStyle(.red)
} else {
Text("粘贴 MusicGo 房间的完整链接。链接里有你的登录凭据,不要发给别人。")
}
}
Section {
Button(action: login) {
HStack {
Text("进入房间")
if checking {
Spacer()
ProgressView()
}
}
}
.disabled(text.isEmpty || checking)
.accessibilityIdentifier("enter-room")
}
}
.navigationTitle("MusicGo")
}
}

private func login() {
guard !checking else { return }
problem = nil
checking = true
Task {
defer { checking = false }
do {
onLogin(try await EntryLink(text).enter())
} catch {
problem = error.localizedDescription
}
}
}
}
24 changes: 24 additions & 0 deletions ios/App/MusicGoApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import MusicGoKit
import SwiftUI

@main
struct MusicGoApp: App {
@State private var session = CredentialStore.load().map { RoomSession(link: $0) }

var body: some Scene {
WindowGroup {
if let session {
RoomView(session: session) {
session.stop()
CredentialStore.clear()
self.session = nil
}
} else {
LoginView { link in
CredentialStore.save(link)
session = RoomSession(link: link)
}
}
}
}
}
92 changes: 92 additions & 0 deletions ios/App/Player/Artwork.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import SwiftUI
import UIKit

/// 当前歌曲的封面,以及从它取出的背景颜色。
@MainActor @Observable
final class Artwork {
private(set) var image: UIImage?
/// 3×3 个颜色,按封面上的位置排列;没有封面时用默认颜色。
private(set) var colors = Artwork.fallback

static let fallback: [Color] = (0..<9).map {
Color(hue: 0.62 + Double($0 % 3) * 0.04, saturation: 0.45, brightness: 0.23 + Double($0 / 3) * 0.05)
}

func load(_ url: URL?) async {
var loaded: UIImage?
if let url { loaded = await Self.fetch(url) }
guard !Task.isCancelled else { return }
withAnimation(.smooth(duration: 0.6)) {
image = loaded
colors = loaded.flatMap(Self.colors) ?? Self.fallback
}
}

/// 原图可能有几千像素,先在后台缩小,显示时才不会卡。
private static func fetch(_ url: URL) async -> UIImage? {
for attempt in 0..<3 {
if attempt > 0 { try? await Task.sleep(for: .seconds(attempt * 2)) }
guard !Task.isCancelled else { return nil }
guard let (data, _) = try? await URLSession.shared.data(from: url) else { continue }
guard let image = UIImage(data: data) else { return nil }
let pixels = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
let ratio = min(1, 1000 / max(pixels.width, pixels.height, 1))
return await image.byPreparingThumbnail(
ofSize: CGSize(width: pixels.width * ratio, height: pixels.height * ratio)) ?? image
}
return nil
}

/// 缩到 30×30,再按九宫格取平均色。
private static func colors(of image: UIImage) -> [Color]? {
let side = 30
var pixels = [UInt8](repeating: 0, count: side * side * 4)
guard let cgImage = image.cgImage,
let context = CGContext(
data: &pixels, width: side, height: side, bitsPerComponent: 8, bytesPerRow: side * 4,
space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
else { return nil }
context.interpolationQuality = .medium
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: side, height: side))

let cell = side / 3
return (0..<9).map { index in
var sum = [0.0, 0.0, 0.0]
for y in 0..<cell {
for x in 0..<cell {
let offset = ((index / 3 * cell + y) * side + index % 3 * cell + x) * 4
for channel in 0..<3 { sum[channel] += Double(pixels[offset + channel]) }
}
}
// 更鲜艳一些,再压暗,保证白字看得清。在这里算好,背景就不用每帧套滤镜。
let average = sum.map { $0 / (255 * Double(cell * cell)) }
let gray = 0.21 * average[0] + 0.72 * average[1] + 0.07 * average[2]
let vivid = average.map { min(1, max(0, gray + ($0 - gray) * 1.3)) * 0.65 }
return Color(red: vivid[0], green: vivid[1], blue: vivid[2])
}
}
}

/// 封面颜色铺成的背景,缓慢流动。
struct ArtworkBackground: View {
let colors: [Color]

@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var isDrifting = false

var body: some View {
MeshGradient(
width: 3, height: 3,
points: [
[0, 0], [0.5, 0], [1, 0],
[0, 0.5], isDrifting ? [0.7, 0.35] : [0.3, 0.65], [1, 0.5],
[0, 1], [0.5, 1], [1, 1],
],
colors: colors
)
.onAppear {
guard !reduceMotion else { return }
withAnimation(.easeInOut(duration: 9).repeatForever(autoreverses: true)) { isDrifting = true }
}
}
}
Loading
Loading