Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: add checks to prevent decreasing #2106

Merged
merged 4 commits into from
Aug 30, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Update podcast list when episode is archived or marked as played from player [#1976](https://github.com/Automattic/pocket-casts-ios/issues/1976)
- Dismiss search if already active when double tap is performed on tab bar [#1652](https://github.com/Automattic/pocket-casts-ios/issues/1652)
- Show transcript for episodes that support it [#2102](https://github.com/Automattic/pocket-casts-ios/issues/2102)
- Add a check to prevent stats from decreasing [#2106](https://github.com/Automattic/pocket-casts-ios/issues/2106)

7.71
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class StatsManager {

public func addTimeSavedDynamicSpeed(_ seconds: TimeInterval) {
updateQueue.async { [weak self] in
self?.savedDynamicSpeed += seconds
self?.savedDynamicSpeed += max(seconds, 0)
self?.isSynced = false
}
}
Expand All @@ -49,7 +49,7 @@ public class StatsManager {

public func addTimeSavedVariableSpeed(_ seconds: TimeInterval) {
updateQueue.async { [weak self] in
self?.savedVariableSpeed += seconds
self?.savedVariableSpeed += max(seconds, 0)
self?.isSynced = false
}
}
Expand All @@ -62,7 +62,7 @@ public class StatsManager {

public func addTotalListeningTime(_ seconds: TimeInterval) {
updateQueue.async { [weak self] in
self?.totalListenedTo += seconds
self?.totalListenedTo += max(seconds, 0)
self?.isSynced = false
}
}
Expand All @@ -75,7 +75,7 @@ public class StatsManager {

public func addSkippedTime(_ seconds: TimeInterval) {
updateQueue.async { [weak self] in
self?.totalSkipped += seconds
self?.totalSkipped += max(seconds, 0)
self?.isSynced = false
}
}
Expand All @@ -88,7 +88,7 @@ public class StatsManager {

public func addAutoSkipTime(_ seconds: TimeInterval) {
updateQueue.async { [weak self] in
self?.savedAutoSkipping += seconds
self?.savedAutoSkipping += max(seconds, 0)
self?.isSynced = false
}
}
Expand Down Expand Up @@ -216,7 +216,7 @@ public class StatsManager {
}

private func saveTime(_ time: TimeInterval, key: String) {
if time < 0 { return }
if time < 0, time < timeForKey(key) { return }

UserDefaults.standard.set(time, forKey: key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,23 @@ extension SyncTask {
}

func changedStats() -> Api_Record? {
let timeSavedDynamicSpeed = convertStat(StatsManager.shared.timeSavedDynamicSpeed())
let totalSkippedTime = convertStat(StatsManager.shared.totalSkippedTime())
let totalIntroSkippedTime = convertStat(StatsManager.shared.totalAutoSkippedTime())
let timeSavedVariableSpeed = convertStat(StatsManager.shared.timeSavedVariableSpeed())
let totalListeningTime = convertStat(StatsManager.shared.totalListeningTime())
let startSyncTime = Int64(StatsManager.shared.statsStartDate().timeIntervalSince1970)

// check to see if there's actually any stats we need to sync
if StatsManager.shared.syncStatus() != .notSynced || (timeSavedDynamicSpeed == nil && totalSkippedTime == nil && totalSkippedTime == nil && timeSavedVariableSpeed == nil && totalListeningTime == nil) {
guard StatsManager.shared.syncStatus() == .notSynced,
let timeSavedDynamicSpeed = convertStat(StatsManager.shared.timeSavedDynamicSpeed()),
let totalSkippedTime = convertStat(StatsManager.shared.totalSkippedTime()),
let totalIntroSkippedTime = convertStat(StatsManager.shared.totalAutoSkippedTime()),
let timeSavedVariableSpeed = convertStat(StatsManager.shared.timeSavedVariableSpeed()),
let totalListeningTime = convertStat(StatsManager.shared.totalListeningTime()) else {
return nil
}

let startSyncTime = Int64(StatsManager.shared.statsStartDate().timeIntervalSince1970)

var deviceRecord = Api_SyncUserDevice()
deviceRecord.timeSilenceRemoval.value = timeSavedDynamicSpeed ?? 0
deviceRecord.timeSkipping.value = totalSkippedTime ?? 0
deviceRecord.timeIntroSkipping.value = totalIntroSkippedTime ?? 0
deviceRecord.timeVariableSpeed.value = timeSavedVariableSpeed ?? 0
deviceRecord.timeListened.value = totalListeningTime ?? 0
deviceRecord.timeSilenceRemoval.value = timeSavedDynamicSpeed
deviceRecord.timeSkipping.value = totalSkippedTime
deviceRecord.timeIntroSkipping.value = totalIntroSkippedTime
deviceRecord.timeVariableSpeed.value = timeSavedVariableSpeed
deviceRecord.timeListened.value = totalListeningTime
deviceRecord.timesStartedAt.value = startSyncTime
deviceRecord.deviceID.value = ServerConfig.shared.syncDelegate?.uniqueAppId() ?? ""
deviceRecord.deviceType.value = ServerConstants.Values.deviceTypeiOS
Expand Down