Skip to content

Commit

Permalink
Refactor - CommunityListView and add test coverage (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
mormaer authored Aug 26, 2023
1 parent fb7829f commit 7379b0d
Show file tree
Hide file tree
Showing 19 changed files with 785 additions and 283 deletions.
24 changes: 24 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
503A5D752A78EF3C00488C38 /* Encodable+Export.swift in Sources */ = {isa = PBXBuildFile; fileRef = 503A5D742A78EF3C00488C38 /* Encodable+Export.swift */; };
503BA26F2A2C94540052516C /* URL+Identifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 503BA26E2A2C94540052516C /* URL+Identifiable.swift */; };
504106CD2A744D7F000AAEF8 /* CommentRepository+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 504106CC2A744D7F000AAEF8 /* CommentRepository+Dependency.swift */; };
505240E32A86916500EA4558 /* FavoriteCommunitiesTracker+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 505240E22A86916500EA4558 /* FavoriteCommunitiesTracker+Dependency.swift */; };
505240E52A86E32700EA4558 /* CommunityListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 505240E42A86E32700EA4558 /* CommunityListModel.swift */; };
505240E72A88D36D00EA4558 /* SectionIndexTitles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 505240E62A88D36D00EA4558 /* SectionIndexTitles.swift */; };
5064D03D2A6DE0AA00B22EE3 /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D03C2A6DE0AA00B22EE3 /* Notifier.swift */; };
5064D03F2A6DE0DB00B22EE3 /* Notifier+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D03E2A6DE0DB00B22EE3 /* Notifier+Dependency.swift */; };
5064D0412A6E63E000B22EE3 /* Task+Notifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5064D0402A6E63E000B22EE3 /* Task+Notifiable.swift */; };
Expand Down Expand Up @@ -63,6 +66,7 @@
50A8812C2A72D727003E3661 /* CommunityRepository+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A8812B2A72D727003E3661 /* CommunityRepository+Dependency.swift */; };
50A8812E2A72D76C003E3661 /* APIClient+Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A8812D2A72D76C003E3661 /* APIClient+Comment.swift */; };
50BC1ABB2A8D6A5A00E3C48B /* ScoringOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50BC1ABA2A8D6A5A00E3C48B /* ScoringOperation.swift */; };
50BC1AB92A89744200E3C48B /* CommunityListModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50BC1AB82A89744200E3C48B /* CommunityListModelTests.swift */; };
50C86ABC2A7E50E200277519 /* PersistenceRepositoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C86ABB2A7E50E200277519 /* PersistenceRepositoryTests.swift */; };
50C99B562A61D792005D57DD /* Dependencies in Frameworks */ = {isa = PBXBuildFile; productRef = 50C99B552A61D792005D57DD /* Dependencies */; };
50C99B592A61D889005D57DD /* APIClient+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50C99B582A61D889005D57DD /* APIClient+Dependency.swift */; };
Expand Down Expand Up @@ -438,6 +442,9 @@
503A5D742A78EF3C00488C38 /* Encodable+Export.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Encodable+Export.swift"; sourceTree = "<group>"; };
503BA26E2A2C94540052516C /* URL+Identifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Identifiable.swift"; sourceTree = "<group>"; };
504106CC2A744D7F000AAEF8 /* CommentRepository+Dependency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CommentRepository+Dependency.swift"; sourceTree = "<group>"; };
505240E22A86916500EA4558 /* FavoriteCommunitiesTracker+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FavoriteCommunitiesTracker+Dependency.swift"; sourceTree = "<group>"; };
505240E42A86E32700EA4558 /* CommunityListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommunityListModel.swift; sourceTree = "<group>"; };
505240E62A88D36D00EA4558 /* SectionIndexTitles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionIndexTitles.swift; sourceTree = "<group>"; };
5064D03C2A6DE0AA00B22EE3 /* Notifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifier.swift; sourceTree = "<group>"; };
5064D03E2A6DE0DB00B22EE3 /* Notifier+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notifier+Dependency.swift"; sourceTree = "<group>"; };
5064D0402A6E63E000B22EE3 /* Task+Notifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Task+Notifiable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -470,6 +477,7 @@
50A8812B2A72D727003E3661 /* CommunityRepository+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CommunityRepository+Dependency.swift"; sourceTree = "<group>"; };
50A8812D2A72D76C003E3661 /* APIClient+Comment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIClient+Comment.swift"; sourceTree = "<group>"; };
50BC1ABA2A8D6A5A00E3C48B /* ScoringOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScoringOperation.swift; sourceTree = "<group>"; };
50BC1AB82A89744200E3C48B /* CommunityListModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommunityListModelTests.swift; sourceTree = "<group>"; };
50C86ABB2A7E50E200277519 /* PersistenceRepositoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceRepositoryTests.swift; sourceTree = "<group>"; };
50C99B582A61D889005D57DD /* APIClient+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIClient+Dependency.swift"; sourceTree = "<group>"; };
50C99B5B2A61F5EB005D57DD /* CommentRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentRepository.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1010,6 +1018,14 @@
path = Mocks;
sourceTree = "<group>";
};
50BC1AB72A89741000E3C48B /* Community List */ = {
isa = PBXGroup;
children = (
50BC1AB82A89744200E3C48B /* CommunityListModelTests.swift */,
);
path = "Community List";
sourceTree = "<group>";
};
50C86AB82A7E507200277519 /* Persistence */ = {
isa = PBXGroup;
children = (
Expand All @@ -1027,6 +1043,7 @@
500C168D2A66FAAB006F243B /* HapticManager+Dependency.swift */,
5064D03E2A6DE0DB00B22EE3 /* Notifier+Dependency.swift */,
50785F722A98E03F00117245 /* SiteInformationTracker+Dependency.swift */,
505240E22A86916500EA4558 /* FavoriteCommunitiesTracker+Dependency.swift */,
);
path = Dependency;
sourceTree = "<group>";
Expand Down Expand Up @@ -1221,6 +1238,7 @@
isa = PBXGroup;
children = (
6D8F08FE2A4029AE003EB4FD /* Community List View.swift */,
505240E42A86E32700EA4558 /* CommunityListModel.swift */,
6D91D4532A41597B006B8F9A /* Components */,
);
path = "Community List";
Expand Down Expand Up @@ -1334,6 +1352,7 @@
6363D5D927EE196A00E34822 /* MlemTests */ = {
isa = PBXGroup;
children = (
50BC1AB72A89741000E3C48B /* Community List */,
50DBB8DE2A805770002870B1 /* Mocks */,
50C86AB82A7E507200277519 /* Persistence */,
6363D5DA27EE196A00E34822 /* MlemTests.swift */,
Expand Down Expand Up @@ -1642,6 +1661,7 @@
children = (
6D91D4542A415994006B8F9A /* CommunityListSidebarEntry.swift */,
6D91D4572A4159D8006B8F9A /* CommunityListRowViews.swift */,
505240E62A88D36D00EA4558 /* SectionIndexTitles.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -2346,6 +2366,7 @@
CD1446232A5B336900610EF1 /* LicensesView.swift in Sources */,
CDDCF6432A66343D003DA3AC /* FancyTabBar.swift in Sources */,
6318DE5627FBAE3600CC2AD6 /* Share Sheet.swift in Sources */,
505240E52A86E32700EA4558 /* CommunityListModel.swift in Sources */,
CD05E77F2A4F263B0081D102 /* Menu Function.swift in Sources */,
CDDB08782A5DF1330075BFEE /* CommentSettingsView.swift in Sources */,
6386E02C2A03D1EC006B3C1D /* App State.swift in Sources */,
Expand Down Expand Up @@ -2382,6 +2403,7 @@
CD05E7792A4E381A0081D102 /* PostSize.swift in Sources */,
6D7782362A48EED8008AC1BF /* APIPrivateMessage.swift in Sources */,
CDE3BA892A8C64BD00B972E2 /* Collapsible Text Item.swift in Sources */,
505240E72A88D36D00EA4558 /* SectionIndexTitles.swift in Sources */,
5064D0452A71549C00B22EE3 /* NotificationMessage.swift in Sources */,
63344C4D2A07ABEE001BC616 /* Community.swift in Sources */,
CDF842612A49EA3900723DA0 /* Mentions Tracker.swift in Sources */,
Expand Down Expand Up @@ -2477,6 +2499,7 @@
6372186D2A3A2AAD008C4816 /* EditComment.swift in Sources */,
6D693A4A2A51B98F009E2D76 /* APICommentReportView.swift in Sources */,
637218622A3A2AAD008C4816 /* DeletePost.swift in Sources */,
505240E32A86916500EA4558 /* FavoriteCommunitiesTracker+Dependency.swift in Sources */,
6332FDC327EFCB5F0009A98A /* Color.swift in Sources */,
637218432A3A2AAD008C4816 /* APIClient.swift in Sources */,
CD82A2572A716D7C00111034 /* PersonRepository+Dependency.swift in Sources */,
Expand Down Expand Up @@ -2613,6 +2636,7 @@
files = (
50DBB8E02A805836002870B1 /* MockErrorHandler.swift in Sources */,
6363D5DB27EE196A00E34822 /* MlemTests.swift in Sources */,
50BC1AB92A89744200E3C48B /* CommunityListModelTests.swift in Sources */,
50DBB8E22A80F9E4002870B1 /* APICommunity+Mock.swift in Sources */,
50C86ABC2A7E50E200277519 /* PersistenceRepositoryTests.swift in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions Mlem/API/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class APIClient {
// MARK: - Initialisation

init(
session: URLSession = .init(configuration: .default),
urlSession: URLSession = .init(configuration: .default),
decoder: JSONDecoder = .defaultDecoder,
transport: @escaping (URLSession, URLRequest) async throws -> (Data, URLResponse)
) {
self.urlSession = session
self.urlSession = urlSession
self.decoder = decoder
self.transport = transport
}
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Dependency/APIClient+Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Dependencies
import Foundation

extension APIClient: DependencyKey {
static let liveValue = APIClient { urlSession, urlRequest in try await urlSession.data(for: urlRequest) }
static let liveValue = APIClient(transport: { urlSession, urlRequest in try await urlSession.data(for: urlRequest) })
static let testValue = APIClient(transport: unimplemented())
}

Expand Down
21 changes: 21 additions & 0 deletions Mlem/Dependency/FavoriteCommunitiesTracker+Dependency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// FavoriteCommunitiesTracker+Dependency.swift
// Mlem
//
// Created by mormaer on 11/08/2023.
//
//

import Dependencies
import Foundation

extension FavoriteCommunitiesTracker: DependencyKey {
static let liveValue = FavoriteCommunitiesTracker()
}

extension DependencyValues {
var favoriteCommunitiesTracker: FavoriteCommunitiesTracker {
get { self[FavoriteCommunitiesTracker.self] }
set { self[FavoriteCommunitiesTracker.self] = newValue }
}
}
2 changes: 1 addition & 1 deletion Mlem/Dependency/Notifier+Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Dependencies

extension Notifier: DependencyKey {
static let liveValue = Notifier()
static let liveValue = Notifier(display: { await NotificationDisplayer.display($0) })
}

extension DependencyValues {
Expand Down
6 changes: 6 additions & 0 deletions Mlem/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ extension String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
}

extension [String] {
static var alphabet: Self {
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
}
}
5 changes: 0 additions & 5 deletions Mlem/Extensions/View - Handle Lemmy Links.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import SwiftUI
struct HandleLemmyLinksDisplay: ViewModifier {
@EnvironmentObject var appState: AppState
@EnvironmentObject var filtersTracker: FiltersTracker
@EnvironmentObject var favoriteCommunitiesTracker: FavoriteCommunitiesTracker
@EnvironmentObject var savedAccounts: SavedAccountTracker

@AppStorage("internetSpeed") var internetSpeed: InternetSpeed = .fast
Expand All @@ -26,21 +25,18 @@ struct HandleLemmyLinksDisplay: ViewModifier {
.environmentObject(appState)
.environmentObject(filtersTracker)
.environmentObject(CommunitySearchResultsTracker())
.environmentObject(favoriteCommunitiesTracker)
}
.navigationDestination(for: APICommunity.self) { community in
FeedView(community: community, feedType: .all, sortType: defaultPostSorting)
.environmentObject(appState)
.environmentObject(filtersTracker)
.environmentObject(CommunitySearchResultsTracker())
.environmentObject(favoriteCommunitiesTracker)
}
.navigationDestination(for: CommunityLinkWithContext.self) { context in
FeedView(community: context.community, feedType: context.feedType, sortType: defaultPostSorting)
.environmentObject(appState)
.environmentObject(filtersTracker)
.environmentObject(CommunitySearchResultsTracker())
.environmentObject(favoriteCommunitiesTracker)
}
.navigationDestination(for: CommunitySidebarLinkWithContext.self) { context in
CommunitySidebarView(
Expand All @@ -49,7 +45,6 @@ struct HandleLemmyLinksDisplay: ViewModifier {
)
.environmentObject(filtersTracker)
.environmentObject(CommunitySearchResultsTracker())
.environmentObject(favoriteCommunitiesTracker)
}
.navigationDestination(for: APIPostView.self) { post in
ExpandedPost(post: post)
Expand Down
11 changes: 7 additions & 4 deletions Mlem/Models/Trackers/Favorite Community Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Combine
import Dependencies
import Foundation

@MainActor
class FavoriteCommunitiesTracker: ObservableObject {
@Dependency(\.persistenceRepository) var persistenceRepository

Expand All @@ -19,9 +18,7 @@ class FavoriteCommunitiesTracker: ObservableObject {
init() {
self.favoriteCommunities = persistenceRepository.loadFavoriteCommunities()
self.updateObserver = $favoriteCommunities.sink { [weak self] value in
Task {
try await self?.persistenceRepository.saveFavoriteCommunities(value)
}
self?.save(value)
}
}

Expand All @@ -40,4 +37,10 @@ class FavoriteCommunitiesTracker: ObservableObject {
func unfavorite(_ community: APICommunity) {
favoriteCommunities.removeAll(where: { $0.community.id == community.id })
}

private func save(_ value: [FavoriteCommunity]) {
Task { [weak self] in
try await self?.persistenceRepository.saveFavoriteCommunities(value)
}
}
}
8 changes: 7 additions & 1 deletion Mlem/Notifications/Notifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation

/// An actor to queue notifications which should be presented to the user
actor Notifier {

private var display: (Notifiable) async -> Void
private var queue = [Notifiable]() {
didSet {
guard !isNotifying else { return }
Expand All @@ -19,6 +21,10 @@ actor Notifier {

private var isNotifying = false

init(display: @escaping (Notifiable) async -> Void) {
self.display = display
}

func performWithLoader(_ operation: @Sendable @escaping () async -> Void) {
queue.append(
Task(priority: .userInitiated, operation: operation)
Expand Down Expand Up @@ -51,7 +57,7 @@ actor Notifier {
}

queue.removeFirst()
await NotificationDisplayer.display(first)
await display(first)
await notify()
}
}
Loading

0 comments on commit 7379b0d

Please sign in to comment.