Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanStepanok committed Jun 19, 2024
1 parent ecd4b6b commit 23394f7
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 64 deletions.
4 changes: 4 additions & 0 deletions Core/Core/View/Base/CalendarManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public protocol CalendarManagerProtocol {
func requestAccess() async -> Bool
func courseStatus(courseID: String) -> SyncStatus
func clearAllData(removeCalendar: Bool)
func isDatesChanged(courseID: String, checksum: String) -> Bool
}

#if DEBUG
public struct CalendarManagerMock: CalendarManagerProtocol {
public func createCalendarIfNeeded() {}
public func filterCoursesBySelected(fetchedCourses: [CourseForSync]) async -> [CourseForSync] {[]}
Expand All @@ -27,6 +29,8 @@ public struct CalendarManagerMock: CalendarManagerProtocol {
public func requestAccess() async -> Bool { true }
public func courseStatus(courseID: String) -> SyncStatus { .synced }
public func clearAllData(removeCalendar: Bool) {}
public func isDatesChanged(courseID: String, checksum: String) -> Bool {false}

public init() {}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public struct CourseContainerView: View {
selection: $viewModel.selection,
coordinate: $coordinate,
collapsed: $collapsed,
dateTabIndex: 1//CourseTab.dates.rawValue
dateTabIndex: CourseTab.dates.rawValue
)
.tabItem {
tab.image
Expand Down
10 changes: 1 addition & 9 deletions OpenEdX/DI/AppAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ class AppAssembly: Assembly {
connectivity: r.resolve(ConnectivityProtocol.self)!)
}).inObjectScope(.container)

container.register(CalendarManager.self) { r in
CalendarManager(
persistence: r.resolve(ProfilePersistenceProtocol.self)!,
interactor: r.resolve(ProfileInteractorProtocol.self)!,
profileStorage: r.resolve(ProfileStorage.self)!
)
}.inObjectScope(.container)

container.register(AuthorizationRouter.self) { r in
r.resolve(Router.self)!
}.inObjectScope(.container)
Expand Down Expand Up @@ -193,7 +185,7 @@ class AppAssembly: Assembly {
profileStorage: r.resolve(ProfileStorage.self)!
)
}
.inObjectScope(.weak)
.inObjectScope(.container)

container.register(DeepLinkManager.self) { r in
DeepLinkManager(
Expand Down
4 changes: 2 additions & 2 deletions OpenEdX/DI/ScreenAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ScreenAssembly: Assembly {
config: r.resolve(ConfigProtocol.self)!,
profileInteractor: r.resolve(ProfileInteractorProtocol.self)!,
appStorage: r.resolve(AppStorage.self)!,
calendarManager: r.resolve(CalendarManager.self)!,
calendarManager: r.resolve(CalendarManagerProtocol.self)!,
sourceScreen: sourceScreen
)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ class ScreenAssembly: Assembly {
interactor: r.resolve(ProfileInteractorProtocol.self)!,
profileStorage: r.resolve(ProfileStorage.self)!,
persistence: r.resolve(ProfilePersistenceProtocol.self)!,
calendarManager: r.resolve(CalendarManager.self)!,
calendarManager: r.resolve(CalendarManagerProtocol.self)!,
connectivity: r.resolve(ConnectivityProtocol.self)!
)
}
Expand Down
1 change: 0 additions & 1 deletion OpenEdX/Data/ProfilePersistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public class ProfilePersistence: ProfilePersistenceProtocol {
}

public func deleteAllCourseStatesAndEvents() {

let fetchRequestCalendarStates: NSFetchRequest<NSFetchRequestResult> = CDCourseCalendarState.fetchRequest()
let deleteRequestCalendarStates = NSBatchDeleteRequest(fetchRequest: fetchRequestCalendarStates)
let fetchRequestCalendarEvents: NSFetchRequest<NSFetchRequestResult> = CDCourseCalendarEvent.fetchRequest()
Expand Down
1 change: 0 additions & 1 deletion OpenEdX/View/MainScreenView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ struct MainScreenView: View {
await viewModel.prefetchDataForOffline()
await viewModel.loadCalendar()
}
viewModel.addShiftCourseDatesObserver()
}
.accentColor(Theme.Colors.accentXColor)
}
Expand Down
24 changes: 11 additions & 13 deletions OpenEdX/View/MainScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class MainScreenViewModel: ObservableObject {
private let profileInteractor: ProfileInteractorProtocol
var sourceScreen: LogistrationSourceScreen
private var appStorage: CoreStorage & ProfileStorage
private let calendarManager: CalendarManager
private let calendarManager: CalendarManagerProtocol
private var cancellables = Set<AnyCancellable>()

@Published var selection: MainTab = .dashboard
Expand All @@ -34,7 +34,7 @@ final class MainScreenViewModel: ObservableObject {
config: ConfigProtocol,
profileInteractor: ProfileInteractorProtocol,
appStorage: CoreStorage & ProfileStorage,
calendarManager: CalendarManager,
calendarManager: CalendarManagerProtocol,
sourceScreen: LogistrationSourceScreen = .default
) {
self.analytics = analytics
Expand All @@ -43,6 +43,15 @@ final class MainScreenViewModel: ObservableObject {
self.appStorage = appStorage
self.calendarManager = calendarManager
self.sourceScreen = sourceScreen

NotificationCenter.default.publisher(for: .shiftCourseDates, object: nil)
.sink { notification in
guard let (courseID, courseName) = notification.object as? (String, String) else { return }
Task {
await self.updateCourseDates(courseID: courseID, courseName: courseName)
}
}
.store(in: &cancellables)
}

public func select(tab: MainTab) {
Expand Down Expand Up @@ -74,17 +83,6 @@ final class MainScreenViewModel: ObservableObject {
await updateCalendarIfNeeded(for: username)
}
}

func addShiftCourseDatesObserver() {
NotificationCenter.default.publisher(for: .shiftCourseDates, object: nil)
.sink { notification in
guard let (courseID, courseName) = notification.object as? (String, String) else { return }
Task {
await self.updateCourseDates(courseID: courseID, courseName: courseName)
}
}
.store(in: &cancellables)
}
}

extension MainScreenViewModel {
Expand Down
10 changes: 5 additions & 5 deletions Profile/Profile/Data/Network/ProfileEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ enum ProfileEndpoint: EndPointType {

var path: String {
switch self {
case .getUserProfile(let username):
case let .getUserProfile(username):
return "/api/user/v1/accounts/\(username)"
case .logOut:
return "/oauth2/revoke_token/"
case let .updateUserProfile(username, _):
return "/api/user/v1/accounts/\(username)"
case let .uploadProfilePicture(username, _):
return "/api/user/v1/accounts/\(username)/image"
case .deleteProfilePicture(username: let username):
case let .deleteProfilePicture(username):
return "/api/user/v1/accounts/\(username)/image"
case .deleteAccount:
return "/api/user/v1/accounts/deactivate_logout/"
case let .enrollmentsStatus(username):
return "/api/mobile/v1/users/\(username)/enrollments_status/"
case .getCourseDates(let courseID):
case let .getCourseDates(courseID):
return "/api/course_home/v1/dates/\(courseID)"
}
}
Expand Down Expand Up @@ -88,12 +88,12 @@ enum ProfileEndpoint: EndPointType {
"username": username
]
return .requestParameters(parameters: params, encoding: JSONEncoding.default)
case .deleteAccount(password: let password):
case let .deleteAccount(password):
let params: [String: String] = [
"password": password
]
return .requestParameters(parameters: params, encoding: URLEncoding.httpBody)
case .enrollmentsStatus(username: let username):
case let .enrollmentsStatus(username):
return .requestParameters(parameters: nil, encoding: JSONEncoding.default)
case .getCourseDates:
return .requestParameters(encoding: JSONEncoding.default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public protocol ProfilePersistenceProtocol {
func getCourseCalendarEvents(for courseId: String) -> [CourseCalendarEvent]
}

#if DEBUG
public struct ProfilePersistenceMock: ProfilePersistenceProtocol {
public func getCourseState(courseID: String) -> CourseCalendarState? { nil }
public func getAllCourseStates() -> [CourseCalendarState] {[]}
Expand All @@ -31,6 +32,7 @@ public struct ProfilePersistenceMock: ProfilePersistenceProtocol {
public func removeAllCourseCalendarEvents() {}
public func getCourseCalendarEvents(for courseId: String) -> [CourseCalendarEvent] { [] }
}
#endif

public final class ProfileBundle {
private init() {}
Expand Down
2 changes: 1 addition & 1 deletion Profile/Profile/Data/ProfileRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class ProfileRepositoryMock: ProfileRepositoryProtocol {
DataLayer.EnrollmentsStatusElement(courseID: "7", courseName: "Course 7", isActive: true),
DataLayer.EnrollmentsStatusElement(courseID: "8", courseName: "Course 8", isActive: true),
DataLayer.EnrollmentsStatusElement(courseID: "9", courseName: "Course 9", isActive: true),
]
]

return result.domain
}
Expand Down
23 changes: 12 additions & 11 deletions Profile/Profile/Presentation/DatesAndCalendar/CalendarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class CalendarManager: CalendarManagerProtocol {
case ProfileLocalization.Calendar.Dropdown.icloud:
return iCloud ?? local ?? fallback
case ProfileLocalization.Calendar.Dropdown.local:
return fallback ?? local
return fallback ?? local
default:
return iCloud ?? local ?? fallback
}
Expand Down Expand Up @@ -176,7 +176,6 @@ public class CalendarManager: CalendarManagerProtocol {
let events = generateEvents(for: dateBlocks, courseName: courseName, calendar: calendar)
var saveSuccessful = true
events.forEach { event in
// if !alreadyExist(event: event) {
do {
try eventStore.save(event, span: .thisEvent)
persistence.saveCourseCalendarEvent(
Expand Down Expand Up @@ -245,7 +244,7 @@ public class CalendarManager: CalendarManagerProtocol {
private func calendarEvent(for block: CourseDateBlock, courseName: String, calendar: EKCalendar) -> EKEvent? {
guard !block.title.isEmpty else { return nil }

let title = block.title// + ": " + courseName//calendar.title
let title = block.title
let startDate = block.date.addingTimeInterval(Double(alertOffset) * 3600)
let secondAlert = startDate.addingTimeInterval(Double(alertOffset) * 86400)
let endDate = block.date
Expand All @@ -269,7 +268,7 @@ public class CalendarManager: CalendarManagerProtocol {
private func calendarEvent(for blocks: [CourseDateBlock], courseName: String, calendar: EKCalendar) -> EKEvent? {
guard let block = blocks.first, !block.title.isEmpty else { return nil }

let title = block.title// + ": " + courseName//calendar.title
let title = block.title
let startDate = block.date.addingTimeInterval(Double(alertOffset) * 3600)
let secondAlert = startDate.addingTimeInterval(Double(alertOffset) * 86400)
let endDate = block.date
Expand Down Expand Up @@ -336,13 +335,15 @@ public class CalendarManager: CalendarManagerProtocol {
return shortUrl
}

private func generateEvent(title: String,
startDate: Date,
endDate: Date,
secondAlert: Date,
notes: String,
location: String,
calendar: EKCalendar) -> EKEvent {
private func generateEvent(
title: String,
startDate: Date,
endDate: Date,
secondAlert: Date,
notes: String,
location: String,
calendar: EKCalendar
) -> EKEvent {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.location = location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class DatesAndCalendarViewModel: ObservableObject {

@Published var coursesForSync = [CourseForSync]()

var coursesForSyncBeforeChanges = [CourseForSync]()
private var coursesForSyncBeforeChanges = [CourseForSync]()

var coursesForDeleting = [CourseForSync]()
var coursesForAdding = [CourseForSync]()
private var coursesForDeleting = [CourseForSync]()
private var coursesForAdding = [CourseForSync]()

@Published var synced: Bool = true
@Published var hideInactiveCourses: Bool = false
Expand All @@ -56,7 +56,7 @@ public class DatesAndCalendarViewModel: ObservableObject {
}
}

let accounts: [DropDownPicker.DownPickerOption] = [
private let accounts: [DropDownPicker.DownPickerOption] = [
.init(title: ProfileLocalization.Calendar.Dropdown.icloud),
.init(title: ProfileLocalization.Calendar.Dropdown.local)
]
Expand Down Expand Up @@ -291,7 +291,7 @@ public class DatesAndCalendarViewModel: ObservableObject {
return syncedCourses
}

func deleteOldCalendarIfNeeded() {
func deleteOldCalendarIfNeeded() async {
guard let calSettings = profileStorage.calendarSettings else { return }
let courseCalendarStates = persistence.getAllCourseStates()
let courseCountChanges = courseCalendarStates.count != coursesForSync.count
Expand All @@ -304,9 +304,7 @@ public class DatesAndCalendarViewModel: ObservableObject {
calendarManager.removeOldCalendar()
saveCalendarOptions()
persistence.removeAllCourseCalendarEvents()
Task {
await fetchCourses()
}
await fetchCourses()
}

private func syncSelectedCourse(
Expand Down Expand Up @@ -344,7 +342,6 @@ public class DatesAndCalendarViewModel: ObservableObject {
await calendarManager.removeOutdatedEvents(courseID: course.courseID)
persistence.removeCourseState(courseID: course.courseID)
persistence.removeCourseCalendarEvents(for: course.courseID)
// Обновляем статус синхронизации курса
if let index = self.coursesForSync.firstIndex(where: { $0.courseID == course.courseID }) {
self.coursesForSync[index].synced = false
}
Expand Down Expand Up @@ -387,7 +384,6 @@ public class DatesAndCalendarViewModel: ObservableObject {
}
}
} else {
// Убираем из массивов, если состояние курса совпадает с начальным
if let index = coursesForAdding.firstIndex(where: { $0.courseID == course.courseID }) {
coursesForAdding.remove(at: index)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,30 @@ public enum DropDownColor: String {
}
}

public struct DropDownPicker: View {
struct DropDownPicker: View {

public struct DownPickerOption: Hashable {
struct DownPickerOption: Hashable {
let title: String
let color: Color?
let colorString: String?

public init(title: String) {
init(title: String) {
self.title = title
self.color = nil
self.colorString = nil
}

public init(color: DropDownColor) {
init(color: DropDownColor) {
self.title = color.title
self.color = color.color
self.colorString = color.rawValue
}

public func hash(into hasher: inout Hasher) {
func hash(into hasher: inout Hasher) {
hasher.combine(title)
}

public static func == (lhs: DownPickerOption, rhs: DownPickerOption) -> Bool {
static func == (lhs: DownPickerOption, rhs: DownPickerOption) -> Bool {
lhs.title == rhs.title
}
}
Expand All @@ -104,13 +104,13 @@ public struct DropDownPicker: View {
@State private var index = 1000.0
@State var zindex = 1000.0

public init(selection: Binding<DownPickerOption?>, state: DropDownPickerState, options: [DownPickerOption]) {
init(selection: Binding<DownPickerOption?>, state: DropDownPickerState, options: [DownPickerOption]) {
self._selection = selection
self.state = state
self.options = options
}

public var body: some View {
var body: some View {
GeometryReader {
let size = $0.size
VStack(spacing: 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by  Stepanok Ivan on 03.06.2024.
//

import UIKit
import Foundation

public struct CalendarSettings: Codable {
public var colorSelection: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public struct SyncCalendarOptionsView: View {
}

Task {
viewModel.deleteOldCalendarIfNeeded()
await viewModel.deleteOldCalendarIfNeeded()
}
},
onCloseTapped: {
Expand Down

0 comments on commit 23394f7

Please sign in to comment.