Skip to content

Commit

Permalink
SubsonicAPI: improve initial sync
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed Mar 24, 2024
1 parent 0d80518 commit 4eb7287
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
13 changes: 10 additions & 3 deletions Amperfy/Screens/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@
<color key="progressTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="trackTintColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</progressView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100.0%" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KKh-tQ-byV">
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100.0%" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KKh-tQ-byV">
<rect key="frame" x="329" y="226" width="60" height="21"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="60" id="hbQ-OO-Ail"/>
Expand All @@ -688,6 +688,10 @@
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" animating="YES" style="medium" translatesAutoresizingMaskIntoConstraints="NO" id="3en-Wv-ZMc">
<rect key="frame" x="369" y="226" width="20" height="20"/>
<color key="color" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</activityIndicatorView>
</subviews>
<viewLayoutGuide key="safeArea" id="fzc-Vu-2Fw"/>
<color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -697,14 +701,17 @@
<constraint firstItem="8Hu-Ks-SWY" firstAttribute="top" secondItem="fzc-Vu-2Fw" secondAttribute="top" constant="105" id="AFT-rh-m1X"/>
<constraint firstItem="npf-bB-pkf" firstAttribute="leading" secondItem="fzc-Vu-2Fw" secondAttribute="leading" constant="25" id="Fhv-0h-2ew"/>
<constraint firstItem="KKh-tQ-byV" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="qe5-xR-nU8" secondAttribute="trailing" constant="8" id="NIu-Bu-c0c"/>
<constraint firstItem="3en-Wv-ZMc" firstAttribute="trailing" secondItem="npf-bB-pkf" secondAttribute="trailing" id="UKn-Xo-PcW"/>
<constraint firstItem="KKh-tQ-byV" firstAttribute="trailing" secondItem="npf-bB-pkf" secondAttribute="trailing" id="Yo9-zw-fD3"/>
<constraint firstItem="fzc-Vu-2Fw" firstAttribute="trailing" secondItem="npf-bB-pkf" secondAttribute="trailing" constant="25" id="Z9Z-7x-Dmt"/>
<constraint firstItem="qe5-xR-nU8" firstAttribute="leading" secondItem="npf-bB-pkf" secondAttribute="leading" id="dAw-V3-zzn"/>
<constraint firstItem="npf-bB-pkf" firstAttribute="top" secondItem="8Hu-Ks-SWY" secondAttribute="bottom" constant="88" id="etY-SN-P1i"/>
<constraint firstItem="3en-Wv-ZMc" firstAttribute="top" secondItem="npf-bB-pkf" secondAttribute="bottom" constant="8" id="h5V-IR-KPi"/>
<constraint firstItem="KKh-tQ-byV" firstAttribute="centerY" secondItem="qe5-xR-nU8" secondAttribute="centerY" id="zZb-RW-x56"/>
</constraints>
</view>
<connections>
<outlet property="activitySpinner" destination="3en-Wv-ZMc" id="jKV-Yl-cfg"/>
<outlet property="progressBar" destination="npf-bB-pkf" id="C83-ms-bZn"/>
<outlet property="progressInfo" destination="qe5-xR-nU8" id="9mn-5u-yc5"/>
<outlet property="progressLabel" destination="KKh-tQ-byV" id="Wib-rh-Gpj"/>
Expand Down Expand Up @@ -971,13 +978,13 @@
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="secondarySystemBackgroundColor">
<color red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBlueColor">
<color red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.0" green="0.47843137254901963" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
19 changes: 17 additions & 2 deletions Amperfy/Screens/ViewController/SyncVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SyncVC: UIViewController {
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var progressInfo: UILabel!
@IBOutlet weak var activitySpinner: UIActivityIndicatorView!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -59,6 +60,11 @@ class SyncVC: UIViewController {
}.catch { error in
self.appDelegate.eventLogger.report(topic: "Initial Sync", error: error, displayPopup: false)
}.finally {
self.progressInfo.text = "Done"
self.activitySpinner.stopAnimating()
self.activitySpinner.isHidden = true
self.progressLabel.isHidden = true

self.appDelegate.storage.librarySyncVersion = .newestVersion
self.appDelegate.storage.isLibrarySynced = true
self.appDelegate.intentManager.registerXCallbackURLs()
Expand All @@ -77,7 +83,7 @@ class SyncVC: UIViewController {
if let infoText = infoText {
self.progressInfo.text = infoText
}
self.progressBar.setProgress(percentParsed, animated: true)
self.progressBar.setProgress(percentParsed, animated: percentParsed != 0.0)
self.progressLabel.text = String(format: "%.1f", percentParsed * 100) + "%"
}
}
Expand All @@ -96,7 +102,7 @@ extension SyncVC: SyncCallbacks {

var parsePercent: Float = 0.0
if self.libObjectsToParseCount > 0 {
parsePercent = Float(self.parsedObjectCount) / Float(self.libObjectsToParseCount)
parsePercent = min(Float(self.parsedObjectCount) / Float(self.libObjectsToParseCount), 1.0)
}
let percentDiff = Int(parsePercent*1000)-Int(self.parsedObjectPercent*1000)
if percentDiff > 0 {
Expand All @@ -113,6 +119,15 @@ extension SyncVC: SyncCallbacks {
self.state = parsedObjectType
self.libObjectsToParseCount = totalCount > 0 ? totalCount : 1

if totalCount > 0 {
activitySpinner.stopAnimating()
activitySpinner.isHidden = true
} else {
activitySpinner.startAnimating()
activitySpinner.isHidden = false
}
progressLabel.isHidden = totalCount <= 0

switch parsedObjectType {
case .artist:
self.updateSyncInfo(infoText: "Syncing artists ...", percentParsed: 0.0)
Expand Down
4 changes: 2 additions & 2 deletions AmperfyKit/Api/Ampache/AmpacheLibrarySyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AmpacheLibrarySyncer: LibrarySyncer {
self.ampacheXmlServerApi.requesetLibraryMetaData()
}.then { auth -> Promise<Void> in
statusNotifyier?.notifySyncStarted(ofType: .artist, totalCount: auth.artistCount)
let pollCountArtist = auth.artistCount / AmpacheXmlServerApi.maxItemCountToPollAtOnce
let pollCountArtist = max(1, Int(ceil(Double(auth.artistCount) / Double(AmpacheXmlServerApi.maxItemCountToPollAtOnce))))
let artistPromises: [() -> Promise<Void>] = Array(0...pollCountArtist).compactMap { index in return {
return firstly {
self.ampacheXmlServerApi.requestArtists(startIndex: index*AmpacheXmlServerApi.maxItemCountToPollAtOnce)
Expand All @@ -73,7 +73,7 @@ class AmpacheLibrarySyncer: LibrarySyncer {
self.ampacheXmlServerApi.requesetLibraryMetaData()
}.then { auth -> Promise<AuthentificationHandshake> in
statusNotifyier?.notifySyncStarted(ofType: .album, totalCount: auth.albumCount)
let pollCountAlbum = auth.albumCount / AmpacheXmlServerApi.maxItemCountToPollAtOnce
let pollCountAlbum = max(1, Int(ceil(Double(auth.albumCount) / Double(AmpacheXmlServerApi.maxItemCountToPollAtOnce))))
let albumPromises: [() -> Promise<Void>] = Array(0...pollCountAlbum).compactMap { index in return {
firstly {
self.ampacheXmlServerApi.requestAlbums(startIndex: index*AmpacheXmlServerApi.maxItemCountToPollAtOnce)
Expand Down
11 changes: 7 additions & 4 deletions AmperfyKit/Api/Subsonic/SubsonicLibrarySyncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SubsonicLibrarySyncer: LibrarySyncer {
private let eventLogger: EventLogger
private let log = OSLog(subsystem: "Amperfy", category: "SubsonicLibSyncer")

private static let maxItemCountToPollAtOnce: Int = 500

var isSyncAllowed: Bool {
return Reachability.isConnectedToNetwork()
}
Expand Down Expand Up @@ -60,11 +62,12 @@ class SubsonicLibrarySyncer: LibrarySyncer {
}
}.then { auth -> Promise<Void> in
let artists = self.storage.main.library.getArtists().filter{ !$0.id.isEmpty }
statusNotifyier?.notifySyncStarted(ofType: .album, totalCount: artists.count)

let artistPromises: [() -> Promise<Void>] = artists.compactMap { artist in return {
let albumCount = artists.reduce(0, { $0 + $1.albumCount })
let pollCountArtist = max(1, Int(ceil(Double(albumCount) / Double(Self.maxItemCountToPollAtOnce))))
statusNotifyier?.notifySyncStarted(ofType: .album, totalCount: pollCountArtist)
let artistPromises: [() -> Promise<Void>] = Array(0...pollCountArtist).compactMap { index in return {
return firstly {
self.subsonicServerApi.requestArtist(id: artist.id)
self.subsonicServerApi.requestAlbums(offset: index*Self.maxItemCountToPollAtOnce, count: Self.maxItemCountToPollAtOnce)
}.then { response in
self.storage.async.perform { asyncCompanion in
let parserDelegate = SsAlbumParserDelegate(library: asyncCompanion.library, subsonicUrlCreator: self.subsonicServerApi, parseNotifier: statusNotifyier)
Expand Down
10 changes: 10 additions & 0 deletions AmperfyKit/Api/Subsonic/SubsonicServerApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@ class SubsonicServerApi: URLCleanser {
}
}

func requestAlbums(offset: Int, count: Int) -> Promise<APIDataResponse> {
return request { version in
var urlComp = try self.createAuthApiUrlComponent(version: version, forAction: "getAlbumList2")
urlComp.addQueryItem(name: "type", value: "alphabeticalByName")
urlComp.addQueryItem(name: "size", value: count)
urlComp.addQueryItem(name: "offset", value: offset)
return try self.createUrl(from: urlComp)
}
}

func requestRandomSongs(count: Int) -> Promise<APIDataResponse> {
return request { version in
var urlComp = try self.createAuthApiUrlComponent(version: version, forAction: "getRandomSongs")
Expand Down

0 comments on commit 4eb7287

Please sign in to comment.