-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add proxy-submissions methods * Add raiting API methods * UI for leaderboard * Refactor AdaptiveStatsPresenter * Add ranks * Create card with leaderboard * Add fake generated names * Medal assets * Add rating separator * Add current request handling * Fix separator bug * Add rating table footer * Fix footer label * Add migration * Add L10n * Fix shadow bug * Add pluralization * txt -> plist * Add adaptive SubmissionsAPI implementation * Fix weeks order * Rating update in stepSubmissionDidCorrect() * Change func signatures * New rating backend * Add rating backend URL * Fix colors
- Loading branch information
Showing
43 changed files
with
1,592 additions
and
182 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// AdaptiveAchievementsPresenter.swift | ||
// Stepic | ||
// | ||
// Created by Vladislav Kiryukhin on 15.08.2017. | ||
// Copyright © 2017 Alex Karpov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol AdaptiveAchievementsView: class { | ||
func reload() | ||
func setAchievements(records: [AchievementViewData]) | ||
} | ||
|
||
struct AchievementViewData { | ||
let name: String | ||
let info: String | ||
let type: AchievementType | ||
let cover: UIImage? | ||
let isUnlocked: Bool | ||
let currentProgress: Int | ||
let maxProgress: Int | ||
} | ||
|
||
class AdaptiveAchievementsPresenter { | ||
weak var view: AdaptiveAchievementsView? | ||
|
||
fileprivate var achievementsManager: AchievementManager | ||
|
||
private var achievements: [AchievementViewData]? | ||
|
||
init(achievementsManager: AchievementManager, view: AdaptiveAchievementsView) { | ||
self.view = view | ||
|
||
self.achievementsManager = achievementsManager | ||
} | ||
|
||
func reloadData(force: Bool = false) { | ||
if achievements == nil || force { | ||
achievements = [] | ||
achievementsManager.storedAchievements.forEach({ achievement in | ||
achievements!.append(AchievementViewData(name: achievement.name, info: achievement.info ?? "", type: achievement.type, cover: achievement.cover, isUnlocked: achievement.isUnlocked, currentProgress: achievement.progressValue, maxProgress: achievement.maxProgressValue)) | ||
}) | ||
} | ||
|
||
view?.setAchievements(records: achievements!) | ||
|
||
view?.reload() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal1.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "medal3@2x.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+2.03 KB
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal1.imageset/medal3@2x.png
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
21
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal2.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "medal2@2x.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+2.18 KB
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal2.imageset/medal2@2x.png
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
21
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal3.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "medal1@2x.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+2.24 KB
StepicAdaptiveCourse/AdaptiveAssets.xcassets/medal3.imageset/medal1@2x.png
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
21
StepicAdaptiveCourse/AdaptiveAssets.xcassets/more.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "more@2x.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+427 Bytes
StepicAdaptiveCourse/AdaptiveAssets.xcassets/more.imageset/more@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// | ||
// AdaptiveRatingsPresenter.swift | ||
// Stepic | ||
// | ||
// Created by Vladislav Kiryukhin on 15.08.2017. | ||
// Copyright © 2017 Alex Karpov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
protocol AdaptiveRatingsView: class { | ||
func reload() | ||
func setRatings(data: ScoreboardViewData) | ||
func showError() | ||
} | ||
|
||
struct RatingViewData { | ||
let position: Int | ||
let exp: Int | ||
let name: String | ||
let me: Bool | ||
} | ||
|
||
struct ScoreboardViewData { | ||
let allCount: Int | ||
let leaders: [RatingViewData] | ||
} | ||
|
||
class AdaptiveRatingsPresenter { | ||
weak var view: AdaptiveRatingsView? | ||
|
||
fileprivate var ratingsAPI: RatingsAPI | ||
fileprivate var ratingManager: RatingManager | ||
|
||
private var scoreboard: [Int: ScoreboardViewData] = [:] | ||
|
||
private var currentRequest: Request? | ||
|
||
// Names (word + grammatical gender) | ||
private var nouns: [(String, String)] = [] | ||
private var adjs: [(String, String)] = [] | ||
|
||
init(ratingsAPI: RatingsAPI, ratingManager: RatingManager, view: AdaptiveRatingsView) { | ||
self.view = view | ||
self.ratingManager = ratingManager | ||
self.ratingsAPI = ratingsAPI | ||
|
||
loadNamesFromFiles() | ||
} | ||
|
||
func reloadData(days: Int? = nil, force: Bool = false) { | ||
// Send rating first, then get rating | ||
currentRequest?.cancel() | ||
currentRequest = ratingsAPI.update(courseId: StepicApplicationsInfo.adaptiveCourseId, exp: ratingManager.rating, success: { _ in | ||
print("remote rating updated -> reload rating") | ||
self.reloadRating(days: days, force: force) | ||
}, error: { responseStatus in | ||
switch responseStatus { | ||
case .serverError: | ||
print("remote rating update failed: server error") | ||
AnalyticsReporter.reportEvent(AnalyticsEvents.Adaptive.ratingServerError) | ||
case .connectionError(let error): | ||
print("remote rating update failed: \(error)") | ||
default: | ||
print("remote rating update failed: \(responseStatus)") | ||
} | ||
self.view?.setRatings(data: ScoreboardViewData(allCount: 0, leaders: [])) | ||
self.view?.reload() | ||
self.view?.showError() | ||
}) | ||
} | ||
|
||
fileprivate func reloadRating(days: Int? = nil, force: Bool = false) { | ||
let downloadedScoreboard = scoreboard[days ?? 0] // 0 when 'days' == nil | ||
if downloadedScoreboard == nil || force { | ||
let currentUser = AuthInfo.shared.userId | ||
|
||
currentRequest?.cancel() | ||
currentRequest = ratingsAPI.retrieve(courseId: StepicApplicationsInfo.adaptiveCourseId, count: 10, days: days, success: { scoreboard in | ||
var curLeaders: [RatingViewData] = [] | ||
scoreboard.leaders.forEach { record in | ||
curLeaders.append(RatingViewData(position: record.rank, exp: record.exp, name: self.generateNameBy(userId: record.userId), me: currentUser == record.userId)) | ||
} | ||
|
||
let curScoreboard = ScoreboardViewData(allCount: scoreboard.allCount, leaders: curLeaders) | ||
self.scoreboard[days ?? 0] = curScoreboard | ||
self.view?.setRatings(data: curScoreboard) | ||
self.view?.reload() | ||
}, error: { err in | ||
print(err) | ||
self.view?.setRatings(data: ScoreboardViewData(allCount: 0, leaders: [])) | ||
self.view?.reload() | ||
self.view?.showError() | ||
}) | ||
} else { | ||
view?.setRatings(data: downloadedScoreboard!) | ||
view?.reload() | ||
} | ||
} | ||
|
||
fileprivate func loadNamesFromFiles() { | ||
func readFile(name: String) -> [String] { | ||
if let path = Bundle.main.path(forResource: name, ofType: "plist"), | ||
let words = NSArray(contentsOfFile: path) as? [String] { | ||
return words | ||
} | ||
return [] | ||
} | ||
|
||
readFile(name: "adjectives_m").forEach { adjs.append(($0, "m")) } | ||
readFile(name: "adjectives_f").forEach { adjs.append(($0, "f")) } | ||
readFile(name: "nouns_m").forEach { nouns.append(($0, "m")) } | ||
readFile(name: "nouns_f").forEach { nouns.append(($0, "f")) } | ||
|
||
assert(adjs.count % 2 == 0) | ||
} | ||
|
||
fileprivate func generateNameBy(userId: Int) -> String { | ||
func hash(_ id: Int) -> Int { | ||
var x = id | ||
x = ((x >> 16) ^ x) &* 0x45d9f3b | ||
x = ((x >> 16) ^ x) &* 0x45d9f3b | ||
x = (x >> 16) ^ x | ||
return x % (nouns.count * (adjs.count / 2)) | ||
} | ||
|
||
let noun = nouns[hash(userId) % nouns.count] | ||
let adjsByGender = adjs.flatMap { noun.1 == $0.1 ? $0 : nil } | ||
let adjNum = hash(userId) / nouns.count | ||
|
||
return "\(adjsByGender[adjNum].0.capitalized) \(noun.0)" | ||
} | ||
} |
Oops, something went wrong.