Skip to content

Commit

Permalink
MOB-1968 - Fixed refresh badges functionality (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg-Pecheneg authored Apr 11, 2024
1 parent f71be52 commit ccfbd55
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ extension NetworkService {
walletBalance: MockEntitiesFabric.DomainProfile.createPublicProfileWalletBalances())
}

public func refreshDomainBadges(for domain: DomainItem) async throws -> RefreshBadgesResponse {
.init(ok: true, refresh: true, next: Date())
}
public func fetchBadgesInfo(for domain: DomainItem) async throws -> BadgesInfo {
try await fetchBadgesInfo(for: domain.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ final class DomainProfileBadgesSection {
private let sectionAnalyticName: String = "badges"
private var isRefreshingBadges = false
private var isBadgesUpToDate = false
private var refreshBadgesTimer: AnyCancellable?

init(sectionData: SectionData,
state: DomainProfileViewController.State,
controller: DomainProfileSectionsController) {
self.badgesData = sectionData
self.controller = controller
self.state = state
setBadgesUpToDateFor(nextRefreshDate: badgesData.refresh?.next)
setBadgesUpToDateFor(nextRefreshDate: nil)
}

static func numberOfBadgesInTheRow() -> Int {
Expand Down Expand Up @@ -191,17 +190,13 @@ private extension DomainProfileBadgesSection {

func refreshDomainBadges() {
Task {
stopRefreshBadgesTimer()
guard let controller else { return }
let domain = controller.generalData.domain.toDomainItem()
updateRefreshingStatusAndUpdateSectionHeader(isRefreshingBadges: true)
do {
let refreshInfo = try await NetworkService().refreshDomainBadges(for: domain)
setBadgesUpToDateFor(nextRefreshDate: refreshInfo.next)
updateRefreshingStatusAndUpdateSectionHeader(isRefreshingBadges: refreshInfo.refresh)
if refreshInfo.refresh {
startRefreshBadgesTimer()
}
try await NetworkService().refreshDomainBadges(for: domain)
setBadgesUpToDateFor(nextRefreshDate: nil)
updateRefreshingStatusAndUpdateSectionHeader(isRefreshingBadges: false)
} catch {
appContext.toastMessageService.showToast(.failedToRefreshBadges, isSticky: false)
updateRefreshingStatusAndUpdateSectionHeader(isRefreshingBadges: false)
Expand All @@ -217,22 +212,6 @@ private extension DomainProfileBadgesSection {
}
}

@MainActor
func startRefreshBadgesTimer() {
refreshBadgesTimer = Timer
.publish(every: Constants.refreshDomainBadgesInterval, on: .main, in: .default)
.autoconnect()
.sink { [weak self] _ in
self?.refreshDomainBadges()
}
}

@MainActor
func stopRefreshBadgesTimer() {
refreshBadgesTimer?.cancel()
refreshBadgesTimer = nil
}

func setBadgesUpToDateFor(nextRefreshDate: Date?) {
guard let nextRefreshDate else {
self.isBadgesUpToDate = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,22 @@ extension Endpoint {
)
}

static func refreshDomainBadges(for domain: DomainItem) -> Endpoint {
static func refreshDomainBadges(for domain: DomainItem,
expires: UInt64,
signature: String) throws -> Endpoint {
let address = try domain.getETHAddressThrowing()
let expiresString = "\(expires)"
let headers = [
SignatureComponentHeaders.CodingKeys.domain.rawValue: domain.name,
SignatureComponentHeaders.CodingKeys.expires.rawValue: expiresString,
SignatureComponentHeaders.CodingKeys.signature.rawValue: signature
]
return Endpoint(
host: NetworkConfig.migratedEndpoint,
path: "/api/domains/\(domain.name)/sync_badges",
path: "/profile/user/\(address)/badges",
queryItems: [],
body: ""
body: "[]",
headers: headers
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ extension NetworkService: DomainProfileNetworkServiceProtocol {
return info
}

public func refreshDomainBadges(for domain: DomainItem) async throws -> RefreshBadgesResponse {
guard let url = Endpoint.refreshDomainBadges(for: domain).url else {
throw NetworkLayerError.creatingURLFailed
}
let data = try await fetchDataHandlingThrottle(for: url, method: .get)
guard let response = RefreshBadgesResponse.objectFromData(data,
dateDecodingStrategy: .defaultDateDecodingStrategy()) else {
throw NetworkLayerError.failedParseProfileData
}
return response
public func refreshDomainBadges(for domain: DomainItem) async throws {
let persistedSignature = try await getOrCreateAndStorePersistedProfileSignature(for: domain)
let signature = persistedSignature.sign
let expires = persistedSignature.expires
let endpoint = try Endpoint.refreshDomainBadges(for: domain,
expires: expires,
signature: signature)

try await fetchDataHandlingThrottleFor(endpoint: endpoint, method: .post)
}

public func fetchBadgeDetailedInfo(for badge: BadgesInfo.BadgeInfo) async throws -> BadgeDetailedInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,6 @@ struct BadgeDetailedInfo: Codable, Hashable {
}
}

struct RefreshBadgesResponse: Codable {
let ok: Bool
let refresh: Bool
let next: Date
}

struct GeneratedMessage: Decodable {
let message: String
let headers: SignatureComponentHeaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@
"DOMAIN_PROFILE_ACCESS_INFO_TITLE" = "Who can view your data?";
"DOMAIN_PROFILE_ACCESS_INFO_DESCRIPTION" = "Control the data displayed on your public profile and what is only visible to you and allowed apps.";
"PROFILE_SOCIALS_EMPTY_MESSAGE" = "Manage your social links on the Unstoppable Domains website.";
"PROFILE_REFRESHING_BADGES_TITLE" = "Refreshing (~15m)";
"PROFILE_REFRESHING_BADGES_TITLE" = "Refreshing";
"PROFILE_BADGES_UP_TO_DATE" = "Badges are up to date";
"PROFILE_BADGES_LEADERBOARD_RANK_MESSAGE" = "#%@ on leaderboard";
"PROFILE_BADGES_LEADERBOARD_HOLDERS_MESSAGE" = "%@ holders";
Expand Down

0 comments on commit ccfbd55

Please sign in to comment.