Skip to content

Commit

Permalink
Codestyle improvements (#76)
Browse files Browse the repository at this point in the history
* codestyle improvements

* remove unused code
  • Loading branch information
volodymyr-chekyrta authored Sep 15, 2023
1 parent 7bc93b9 commit a92a22c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 151 deletions.
2 changes: 1 addition & 1 deletion Core/Core/Extensions/CollectionExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

extension Collection {
public extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
Expand Down
8 changes: 4 additions & 4 deletions Dashboard/Dashboard/Data/Network/DashboardEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import Alamofire

enum DashboardEndpoint: EndPointType {
case getMyCourses(username: String, page: Int)

var path: String {
switch self {
case let .getMyCourses(username, _):
return "/mobile_api_extensions/v1/users/\(username)/course_enrollments"
}
}

var httpMethod: HTTPMethod {
switch self {
case .getMyCourses:
return .get
}
}

var headers: HTTPHeaders? {
nil
}

var task: HTTPTask {
switch self {
case let .getMyCourses(_, page):
Expand Down
21 changes: 9 additions & 12 deletions Discussion/Discussion/Presentation/Posts/PostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public struct PostsView: View {
@ObservedObject private var viewModel: PostsViewModel
@State private var isShowProgress: Bool = true
@State private var showingAlert = false
@State private var listAnimation: Animation?
private let router: DiscussionRouter
private let title: String
private let currentBlockID: String
Expand Down Expand Up @@ -55,7 +54,6 @@ public struct PostsView: View {
HStack {
Group {
Button(action: {
listAnimation = .easeIn
viewModel.generateButtons(type: .filter)
showingAlert = true
}, label: {
Expand All @@ -64,7 +62,6 @@ public struct PostsView: View {
})
Spacer()
Button(action: {
listAnimation = .easeIn
viewModel.generateButtons(type: .sort)
showingAlert = true
}, label: {
Expand All @@ -84,10 +81,8 @@ public struct PostsView: View {
}
.frameLimit()
RefreshableScrollViewCompat(action: {
listAnimation = nil
viewModel.resetPosts()
_ = await viewModel.getPosts(
courseID: courseID,
pageNumber: 1,
withProgress: false
)
Expand Down Expand Up @@ -134,7 +129,6 @@ public struct PostsView: View {
.onAppear {
Task {
await viewModel.getPostsPagination(
courseID: self.courseID,
index: index
)
}
Expand Down Expand Up @@ -179,7 +173,7 @@ public struct PostsView: View {
}
}
}.frameLimit()
.animation(listAnimation)
.animation(nil)
.onRightSwipeGesture {
router.back()
}
Expand All @@ -197,7 +191,10 @@ public struct PostsView: View {
}
.onFirstAppear {
Task {
await viewModel.getPosts(courseID: courseID, pageNumber: 1, withProgress: true)
await viewModel.getPosts(
pageNumber: 1,
withProgress: true
)
}
}
.navigationBarHidden(!showTopMenu)
Expand All @@ -216,11 +213,11 @@ public struct PostsView: View {
@MainActor
private func reloadPage(onSuccess: @escaping () -> Void) {
Task {
listAnimation = nil
viewModel.resetPosts()
_ = await viewModel.getPosts(courseID: courseID,
pageNumber: 1,
withProgress: false)
_ = await viewModel.getPosts(
pageNumber: 1,
withProgress: false
)
onSuccess()
}
}
Expand Down
152 changes: 31 additions & 121 deletions Discussion/Discussion/Presentation/Posts/PostsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class PostsViewModel: ObservableObject {

public var nextPage = 1
public var totalPages = 1
public private(set) var fetchInProgress = false
@Published public private(set) var fetchInProgress = false

public enum ButtonType {
case sort
Expand All @@ -43,7 +43,7 @@ public class PostsViewModel: ObservableObject {
if let courseID {
resetPosts()
Task {
_ = await getPosts(courseID: courseID, pageNumber: 1)
_ = await getPosts(pageNumber: 1)
}
}
}
Expand All @@ -53,7 +53,7 @@ public class PostsViewModel: ObservableObject {
if let courseID {
resetPosts()
Task {
_ = await getPosts(courseID: courseID, pageNumber: 1)
_ = await getPosts(pageNumber: 1)
}
}
}
Expand Down Expand Up @@ -109,9 +109,6 @@ public class PostsViewModel: ObservableObject {
}

public func resetPosts() {
filteredPosts = []
discussionPosts = []
threads.threads = []
nextPage = 1
totalPages = 1
}
Expand Down Expand Up @@ -170,134 +167,36 @@ public class PostsViewModel: ObservableObject {
}

@MainActor
func getPostsPagination(courseID: String, index: Int, withProgress: Bool = true) async {
if !fetchInProgress {
if totalPages > 1 {
if index == filteredPosts.count - 3 {
if totalPages != 1 {
if nextPage <= totalPages {
_ = await getPosts(
courseID: courseID,
pageNumber: self.nextPage,
withProgress: withProgress
)
}
}
}
}
func getPostsPagination(index: Int, withProgress: Bool = true) async {
guard !fetchInProgress else { return }
if totalPages > 1, index >= filteredPosts.count - 3, nextPage <= totalPages {
_ = await getPosts(
pageNumber: self.nextPage,
withProgress: withProgress
)
}
}

// swiftlint:disable function_body_length
@MainActor
public func getPosts(courseID: String, pageNumber: Int, withProgress: Bool = true) async -> Bool {
public func getPosts(pageNumber: Int, withProgress: Bool = true) async -> Bool {
fetchInProgress = true
isShowProgress = withProgress
do {
if pageNumber == 1 {
switch type {
case .allPosts:
threads.threads = try await interactor
.getThreadsList(courseID: courseID,
type: .allPosts,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage = 2
}
case .followingPosts:
threads.threads = try await interactor
.getThreadsList(courseID: courseID,
type: .followingPosts,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage = 2
}
case .nonCourseTopics:
threads.threads = try await interactor
.getThreadsList(courseID: courseID,
type: .nonCourseTopics,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage = 2
}
case .courseTopics(topicID: let topicID):
threads.threads = try await interactor
.getThreadsList(courseID: courseID,
type: .courseTopics(topicID: topicID),
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage = 2
}
case .none:
isShowProgress = false
return false
threads.threads = try await getThreadsList(type: type, page: pageNumber)
if threads.threads.indices.contains(0) {
totalPages = threads.threads[0].numPages
nextPage = 2
}
} else {
switch type {
case .allPosts:
threads.threads += try await interactor
.getThreadsList(courseID: courseID,
type: .allPosts,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage += 1
}
case .followingPosts:
threads.threads += try await interactor
.getThreadsList(courseID: courseID,
type: .followingPosts,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage += 1
}
case .nonCourseTopics:
threads.threads += try await interactor
.getThreadsList(courseID: courseID,
type: .nonCourseTopics,
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage += 1
}
case .courseTopics(topicID: let topicID):
threads.threads += try await interactor
.getThreadsList(courseID: courseID,
type: .courseTopics(topicID: topicID),
sort: sortTitle,
filter: filterTitle,
page: pageNumber).threads
if threads.threads.indices.contains(0) {
self.totalPages = threads.threads[0].numPages
self.nextPage += 1
}
case .none:
isShowProgress = false
return false
threads.threads += try await getThreadsList(type: type, page: pageNumber)
if threads.threads.indices.contains(0) {
totalPages = threads.threads[0].numPages
nextPage += 1
}
}
discussionPosts = generatePosts(threads: threads)
filteredPosts = discussionPosts
self.filteredPosts = self.discussionPosts
isShowProgress = false
fetchInProgress = false
return true
Expand All @@ -312,7 +211,18 @@ public class PostsViewModel: ObservableObject {
return false
}
}
// swiftlint:enable function_body_length

@MainActor
private func getThreadsList(type: ThreadType, page: Int) async throws -> [UserThread] {
guard let courseID else { return [] }
return try await interactor.getThreadsList(
courseID: courseID,
type: type,
sort: sortTitle,
filter: filterTitle,
page: page
).threads
}

private func updateUnreadCommentsCount(id: String) {
var threads = threads.threads
Expand Down
Loading

0 comments on commit a92a22c

Please sign in to comment.