Skip to content

Commit

Permalink
Merge pull request #33 from ayushs-2k4/Fixing-CodeForces-API-Down-Error
Browse files Browse the repository at this point in the history
Fixing code forces api down error - Fixed
  • Loading branch information
ayushs-2k4 authored Jan 12, 2024
2 parents 04b9635 + 44017a0 commit ae89599
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 33 deletions.
24 changes: 12 additions & 12 deletions Kontest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@
07ECFB942AA58259008513C6 /* CodeChefAPIModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB512AA58259008513C6 /* CodeChefAPIModel.swift */; };
07ECFB952AA58259008513C6 /* KontestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB522AA58259008513C6 /* KontestModel.swift */; };
07ECFB962AA58259008513C6 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 07ECFB542AA58259008513C6 /* Preview Assets.xcassets */; };
07ECFB972AA58259008513C6 /* CodeChefView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB562AA58259008513C6 /* CodeChefView.swift */; };
07ECFB972AA58259008513C6 /* CodeChefRatingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB562AA58259008513C6 /* CodeChefRatingsView.swift */; };
07ECFB982AA58259008513C6 /* SettingsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB572AA58259008513C6 /* SettingsScreen.swift */; };
07ECFB992AA58259008513C6 /* LeetCodeGraphQLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB582AA58259008513C6 /* LeetCodeGraphQLView.swift */; };
07ECFB992AA58259008513C6 /* LeetCodeRatingsGraphQLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB582AA58259008513C6 /* LeetCodeRatingsGraphQLView.swift */; };
07ECFB9A2AA58259008513C6 /* RatingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB592AA58259008513C6 /* RatingsView.swift */; };
07ECFB9B2AA58259008513C6 /* NoKontestsDownloadedScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5A2AA58259008513C6 /* NoKontestsDownloadedScreen.swift */; };
07ECFB9C2AA58259008513C6 /* AllKontestsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5B2AA58259008513C6 /* AllKontestsScreen.swift */; };
07ECFB9E2AA58259008513C6 /* BlinkingDotView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5D2AA58259008513C6 /* BlinkingDotView.swift */; };
07ECFB9F2AA58259008513C6 /* CodeForcesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5E2AA58259008513C6 /* CodeForcesView.swift */; };
07ECFB9F2AA58259008513C6 /* CodeForcesRatingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5E2AA58259008513C6 /* CodeForcesRatingsView.swift */; };
07ECFBA02AA58259008513C6 /* KontestDetailsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB5F2AA58259008513C6 /* KontestDetailsScreen.swift */; };
07ECFBA12AA58259008513C6 /* SingleNotificationMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB602AA58259008513C6 /* SingleNotificationMenu.swift */; };
07ECFBA22AA58259008513C6 /* SingleKontestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07ECFB612AA58259008513C6 /* SingleKontestView.swift */; };
Expand Down Expand Up @@ -271,14 +271,14 @@
07ECFB512AA58259008513C6 /* CodeChefAPIModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeChefAPIModel.swift; sourceTree = "<group>"; };
07ECFB522AA58259008513C6 /* KontestModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KontestModel.swift; sourceTree = "<group>"; };
07ECFB542AA58259008513C6 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
07ECFB562AA58259008513C6 /* CodeChefView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeChefView.swift; sourceTree = "<group>"; };
07ECFB562AA58259008513C6 /* CodeChefRatingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeChefRatingsView.swift; sourceTree = "<group>"; };
07ECFB572AA58259008513C6 /* SettingsScreen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsScreen.swift; sourceTree = "<group>"; };
07ECFB582AA58259008513C6 /* LeetCodeGraphQLView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LeetCodeGraphQLView.swift; sourceTree = "<group>"; };
07ECFB582AA58259008513C6 /* LeetCodeRatingsGraphQLView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LeetCodeRatingsGraphQLView.swift; sourceTree = "<group>"; };
07ECFB592AA58259008513C6 /* RatingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RatingsView.swift; sourceTree = "<group>"; };
07ECFB5A2AA58259008513C6 /* NoKontestsDownloadedScreen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoKontestsDownloadedScreen.swift; sourceTree = "<group>"; };
07ECFB5B2AA58259008513C6 /* AllKontestsScreen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AllKontestsScreen.swift; sourceTree = "<group>"; };
07ECFB5D2AA58259008513C6 /* BlinkingDotView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlinkingDotView.swift; sourceTree = "<group>"; };
07ECFB5E2AA58259008513C6 /* CodeForcesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeForcesView.swift; sourceTree = "<group>"; };
07ECFB5E2AA58259008513C6 /* CodeForcesRatingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeForcesRatingsView.swift; sourceTree = "<group>"; };
07ECFB5F2AA58259008513C6 /* KontestDetailsScreen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KontestDetailsScreen.swift; sourceTree = "<group>"; };
07ECFB602AA58259008513C6 /* SingleNotificationMenu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingleNotificationMenu.swift; sourceTree = "<group>"; };
07ECFB612AA58259008513C6 /* SingleKontestView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingleKontestView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -399,9 +399,9 @@
isa = PBXGroup;
children = (
07ECFB592AA58259008513C6 /* RatingsView.swift */,
07ECFB5E2AA58259008513C6 /* CodeForcesView.swift */,
07ECFB562AA58259008513C6 /* CodeChefView.swift */,
07ECFB582AA58259008513C6 /* LeetCodeGraphQLView.swift */,
07ECFB5E2AA58259008513C6 /* CodeForcesRatingsView.swift */,
07ECFB562AA58259008513C6 /* CodeChefRatingsView.swift */,
07ECFB582AA58259008513C6 /* LeetCodeRatingsGraphQLView.swift */,
);
path = RatingsScreens;
sourceTree = "<group>";
Expand Down Expand Up @@ -948,7 +948,7 @@
07D766692AC8404A000A3DFD /* Sidebar.swift in Sources */,
07ECFBA32AA58259008513C6 /* PendingNotificationsScreen.swift in Sources */,
07ECFB9C2AA58259008513C6 /* AllKontestsScreen.swift in Sources */,
07ECFB972AA58259008513C6 /* CodeChefView.swift in Sources */,
07ECFB972AA58259008513C6 /* CodeChefRatingsView.swift in Sources */,
07ECFBAD2AA58259008513C6 /* MyMenu.swift in Sources */,
07ECFB902AA58259008513C6 /* KontestStatus.swift in Sources */,
07ECFBB82AA58259008513C6 /* CodeForcesFetcher.swift in Sources */,
Expand All @@ -966,7 +966,7 @@
07ECFBC42AA583A8008513C6 /* LeetCodeUserProfileGraphQLAPIModel.swift in Sources */,
07ECFBA22AA58259008513C6 /* SingleKontestView.swift in Sources */,
07ECFBA92AA58259008513C6 /* Screen.swift in Sources */,
07ECFB9F2AA58259008513C6 /* CodeForcesView.swift in Sources */,
07ECFB9F2AA58259008513C6 /* CodeForcesRatingsView.swift in Sources */,
07ECFBAB2AA58259008513C6 /* DownloadData.swift in Sources */,
07ECFBA02AA58259008513C6 /* KontestDetailsScreen.swift in Sources */,
077E6EB12AB4B3220003E897 /* ErrorState.swift in Sources */,
Expand All @@ -981,7 +981,7 @@
07AA752E2B4FCF7A00DCD75B /* KontestNewRepository.swift in Sources */,
07ECFBB22AA58259008513C6 /* LeetCodeUserProfileGraphQLAPIDTO.swift in Sources */,
07ECFBA82AA58259008513C6 /* LocalNotificationManager.swift in Sources */,
07ECFB992AA58259008513C6 /* LeetCodeGraphQLView.swift in Sources */,
07ECFB992AA58259008513C6 /* LeetCodeRatingsGraphQLView.swift in Sources */,
07ECFB9B2AA58259008513C6 /* NoKontestsDownloadedScreen.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import OSLog

class CodeForcesAPIRepository: CodeForcesFetcher {
private let logger = Logger(subsystem: "com.ayushsinghal.Kontest", category: "CodeForcesAPIRepository")

func getUserRating(username: String) async throws -> CodeForcesUserRatingAPIDTO {
guard let url = URL(string: "https://codeforces.com/api/user.rating?handle=\(username)") else {
logger.error("Error in making CodeForces ratings url")
Expand All @@ -19,6 +19,10 @@ class CodeForcesAPIRepository: CodeForcesFetcher {

do {
let data = try await downloadDataWithAsyncAwait(url: url)
if String(decoding: data, as: UTF8.self).contains(Constants.codeforcesNotAvailableErrorResponseMessage) {
throw AppError(title: "CodeForces API not available right now", description: "")
}

let fetchedCodeForcesRatings = try JSONDecoder().decode(CodeForcesUserRatingAPIDTO.self, from: data)

return fetchedCodeForcesRatings
Expand All @@ -36,6 +40,11 @@ class CodeForcesAPIRepository: CodeForcesFetcher {

do {
let data = try await downloadDataWithAsyncAwait(url: url)

if String(decoding: data, as: UTF8.self).contains(Constants.codeforcesNotAvailableErrorResponseMessage) {
throw AppError(title: "CodeForces API not available right now", description: "")
}

let fetchedCodeForcesProfile = try JSONDecoder().decode(CodeForcesUserInfoAPIDTO.self, from: data)

return fetchedCodeForcesProfile
Expand Down
3 changes: 2 additions & 1 deletion Kontest/Screens/AllKontestsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import SwiftUI
import WidgetKit

struct AllKontestsScreen: View {
let isInDevelopmentMode = false

@ObservedObject var clockObserver = ClockObserver()

@Environment(AllKontestsViewModel.self) private var allKontestsViewModel
@Environment(NetworkMonitor.self) private var networkMonitor
@State var showRemoveAllNotificationsAlert = false
@State var showNotificationForAllKontestsAlert = false
let isInDevelopmentMode = false
@State private var isNoNotificationIconAnimating = false
let notificationsViewModel = Dependencies.instance.notificationsViewModel

Expand Down
7 changes: 7 additions & 0 deletions Kontest/Screens/GraphScreens/CodeForcesGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ struct CodeForcesGraphView: View {
Text("Please update your username in the settings")
} else if codeForcesViewModel.isLoading {
ProgressView()
} else if codeForcesViewModel.error is AppError {
let appError = codeForcesViewModel.error as! AppError

Text(appError.title)
.bold()

Text(appError.description)
} else {
if let error = codeForcesViewModel.error {
Text("Error: \(error.localizedDescription)")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// CodeChefView.swift
// CodeChefRatingsView.swift
// Kontest
//
// Created by Ayush Singhal on 27/08/23.
//

import SwiftUI

struct CodeChefView: View {
struct CodeChefRatingsView: View {
let username: String
let codeChefViewModel: CodeChefViewModel
let bgColor: Color
Expand Down Expand Up @@ -152,8 +152,8 @@ struct CodeChefProfileView: View {

#Preview {
VStack {
CodeChefView(username: "ayushs_2k4", bgColor: .brown, hoveringScaleValue: 1.05)
CodeChefView(username: "ayush_2k4", bgColor: Color(red: 90/255, green: 55/255, blue: 31/255), hoveringScaleValue: 1.05)
CodeChefRatingsView(username: "ayushs_2k4", bgColor: .brown, hoveringScaleValue: 1.05)
CodeChefRatingsView(username: "ayush_2k4", bgColor: Color(red: 90/255, green: 55/255, blue: 31/255), hoveringScaleValue: 1.05)
}
.environment(Router.instance)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//
// CodeForcesView.swift
// CodeForcesRatingsView.swift
// Kontest
//
// Created by Ayush Singhal on 16/08/23.
//

import SwiftUI

struct CodeForcesView: View {
struct CodeForcesRatingsView: View {
let username: String
let codeForcesViewModel: CodeForcesViewModel
let bgGradient: RadialGradient
let bgGradient: RadialGradient
@State var isHovering = false
let hoveringScaleValue: CGFloat

Expand Down Expand Up @@ -52,7 +52,7 @@ struct CodeForcesProfileView: View {

var body: some View {
ZStack {
bgGradient
bgGradient

if isLoading && error == nil {
ProgressView()
Expand Down Expand Up @@ -118,6 +118,14 @@ struct CodeForcesProfileView: View {
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomLeading)
}
}
else if error is AppError {
let appError = error as! AppError

Text(appError.title)
.bold()

Text(appError.description)
}
else {
Text("LastRank 0")
.padding()
Expand Down Expand Up @@ -156,10 +164,10 @@ struct CodeForcesProfileView: View {
let bgGradient: RadialGradient = .init(gradient: Gradient(stops: [.init(color: Color(red: 159/255, green: 150/255, blue: 137/255), location: 0.0), .init(color: Color(red: 209/255, green: 204/255, blue: 198/255), location: 0.5)]), center: .center, startRadius: 10, endRadius: 500)

return VStack {
CodeForcesView(username: "Fefer_Ivan", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesView(username: "ayushsinghals", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesView(username: "ayushsinghals02", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesView(username: "yermak0v", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesRatingsView(username: "Fefer_Ivan", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesRatingsView(username: "ayushsinghals", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesRatingsView(username: "ayushsinghals02", bgGradient: bgGradient, hoveringScaleValue: 1.05)
CodeForcesRatingsView(username: "yermak0v", bgGradient: bgGradient, hoveringScaleValue: 1.05)
}
.environment(Router.instance)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// LeetCodeGraphQLView.swift
// LeetCodeRatingsGraphQLView.swift
// Kontest
//
// Created by Ayush Singhal on 04/09/23.
//

import SwiftUI

struct LeetCodeGraphQLView: View {
struct LeetCodeRatingsGraphQLView: View {
let username: String
let leetCodeGraphQLViewModel: LeetCodeGraphQLViewModel
let bgColor: Color
Expand Down Expand Up @@ -141,6 +141,6 @@ struct LeetcodeProfileGraphQLView: View {
}

#Preview {
LeetCodeGraphQLView(username: "ayushs_2k4", bgColor: Color(red: 40/255, green: 40/255, blue: 40/255), hoveringScaleValue: 1.05)
LeetCodeRatingsGraphQLView(username: "ayushs_2k4", bgColor: Color(red: 40/255, green: 40/255, blue: 40/255), hoveringScaleValue: 1.05)
.environment(Router.instance)
}
6 changes: 3 additions & 3 deletions Kontest/Screens/RatingsScreens/RatingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ struct RatingsView: View {
var body: some View {
ScrollView(.horizontal) {
LazyHStack(spacing: hSpacing) {
CodeForcesView(username: codeForcesUsername, bgGradient: bgGradient, hoveringScaleValue: hoveringScaleValue)
CodeForcesRatingsView(username: codeForcesUsername, bgGradient: bgGradient, hoveringScaleValue: hoveringScaleValue)
.clipShape(.rect(cornerRadius: cornerRadius))
.aspectRatio(heroRatio, contentMode: .fit)
.containerRelativeFrame(.horizontal, count: columns, spacing: 10)

LeetCodeGraphQLView(username: leetCodeUsername, bgColor: Color(red: 34/255, green: 34/255, blue: 34/255), hoveringScaleValue: hoveringScaleValue)
LeetCodeRatingsGraphQLView(username: leetCodeUsername, bgColor: Color(red: 34/255, green: 34/255, blue: 34/255), hoveringScaleValue: hoveringScaleValue)
.clipShape(.rect(cornerRadius: cornerRadius))
.aspectRatio(heroRatio, contentMode: .fit)
.containerRelativeFrame(.horizontal, count: columns, spacing: 10)

CodeChefView(username: codeChefUsername, bgColor: .brown, hoveringScaleValue: hoveringScaleValue)
CodeChefRatingsView(username: codeChefUsername, bgColor: .brown, hoveringScaleValue: hoveringScaleValue)
.clipShape(.rect(cornerRadius: cornerRadius))
.aspectRatio(heroRatio, contentMode: .fit)
.containerRelativeFrame(.horizontal, count: columns, spacing: 10)
Expand Down
3 changes: 2 additions & 1 deletion Kontest/Utilities/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import Foundation

struct Constants {
enum Constants {
static let minutesToBeReminderBefore = 10
static let maximumDurationOfAKontestInMinutes = 360 * 60
static let minimumDurationOfAKontestInMinutes = 0
static let userDefaultsGroupID = "group.com.ayushsinghal.kontest"
static let codeforcesNotAvailableErrorResponseMessage = "Codeforces is temporarily unavailable"
}

0 comments on commit ae89599

Please sign in to comment.