Skip to content

Commit

Permalink
Swiftlint (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
mormaer authored Jun 27, 2023
1 parent 94dad96 commit 94bde2b
Show file tree
Hide file tree
Showing 146 changed files with 2,159 additions and 2,060 deletions.
15 changes: 15 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
disabled_rules:
- trailing_whitespace # disabling this check until swiftformat is in place, which will catch the majority.
line_length:
warning: 140
error: 160
ignores_comments: true
identifier_name:
excluded: # excluded via string array
- id
- op
- w
- h
- x
- y
allowed_symbols: ["_"] # these are allowed in type names as we use them in API body arguments
23 changes: 23 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@
buildConfigurationList = 6363D5EA27EE196A00E34822 /* Build configuration list for PBXNativeTarget "Mlem" */;
buildPhases = (
6363D5BD27EE196700E34822 /* Sources */,
50F830EB2A47CC4F00D67099 /* Swiftlint */,
6363D5BE27EE196700E34822 /* Frameworks */,
6363D5BF27EE196700E34822 /* Resources */,
);
Expand Down Expand Up @@ -1330,6 +1331,28 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
50F830EB2A47CC4F00D67099 /* Swiftlint */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = Swiftlint;
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if [[ \"$(uname -m)\" == arm64 ]]; then\n export PATH=\"/opt/homebrew/bin:$PATH\"\nfi\n\nif which swiftlint > /dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
6363D5BD27EE196700E34822 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
6 changes: 3 additions & 3 deletions Mlem/API/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class APIClient {
}

func perform<Request: APIRequest>(request: Request) async throws -> Request.Response {

let urlRequest = try urlRequest(from: request)
let (data, response) = try await execute(urlRequest)

Expand All @@ -42,7 +42,7 @@ class APIClient {
let statusCode = (response as? HTTPURLResponse)?.statusCode
throw APIClientError.response(apiError, statusCode)
}

return try decoder.decode(Request.Response.self, from: data)
}

Expand All @@ -64,7 +64,7 @@ class APIClient {
urlRequest.setValue(header.value, forHTTPHeaderField: header.key)
}

if let _ = defintion as? any APIGetRequest {
if defintion as? any APIGetRequest != nil {
urlRequest.httpMethod = "GET"
} else if let postDefinition = defintion as? any APIPostRequest {
urlRequest.httpMethod = "POST"
Expand Down
4 changes: 1 addition & 3 deletions Mlem/API/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ extension APIGetRequest {
var endpoint: URL {
instanceURL
.appending(path: path)
.appending(queryItems: queryItems.filter {
item in item.value != nil
})
.appending(queryItems: queryItems.filter { $0.value != nil })
}
}

Expand Down
32 changes: 16 additions & 16 deletions Mlem/API/Internal/HierarchicalComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
class HierarchicalComment: ObservableObject {
let commentView: APICommentView
var children: [HierarchicalComment]

init(comment: APICommentView, children: [HierarchicalComment]) {
self.commentView = comment
self.children = children
Expand All @@ -23,17 +23,17 @@ extension HierarchicalComment: Identifiable {
}

extension [HierarchicalComment] {

/// A method to insert an updated `APICommentView` into this array of `HierarchicalComment`
/// - Parameter commentView: The `APICommentView` you wish to insert
/// - Returns: An optional `HierarchicalComment` containing the updated comment and it's original chidren if found
@discardableResult mutating func update(with commentView: APICommentView) -> HierarchicalComment? {
return self.insert(commentView: commentView)
}

private mutating func insert(commentView: APICommentView) -> HierarchicalComment? {
let targetId = commentView.id

for (index, element) in self.enumerated() {
if element.id == targetId {
// we've found the comment we're targeting so re-create it and ensure we retain it's children
Expand All @@ -45,16 +45,16 @@ extension [HierarchicalComment] {
return updatedComment
}
}

return insertReply(commentView: commentView)
}

private mutating func insertReply(commentView: APICommentView) -> HierarchicalComment? {
guard let parentId = commentView.comment.parentId else {
// can't be a reply without a parent 🤷
return nil
}

for (index, element) in self.enumerated() {
if element.id == parentId {
// we've found the comment we're replying too, so re-create it and append this to it's children
Expand All @@ -68,44 +68,44 @@ extension [HierarchicalComment] {
return reply
}
}

return nil
}
}

extension [APICommentView] {

/// A representation of this array of `APICommentView` in a hierarchy that is suitable for rendering the UI with parent/child relationships
var hierarchicalRepresentation: [HierarchicalComment] {
var allComments = self

let childrenStartIndex = allComments.partition(by: { $0.comment.parentId != nil })
let children = allComments[childrenStartIndex...]

var childrenById = [APICommentView.ID: [APICommentView.ID]]()
children.forEach { child in
guard let parentId = child.comment.parentId else { return }
childrenById[parentId] = (childrenById[parentId] ?? []) + [child.id]
}

let identifiedComments = Dictionary(uniqueKeysWithValues: allComments.lazy.map { ($0.id, $0) })

/// Recursively populates child comments by looking up IDs from `childrenById`
func populateChildren(_ comment: APICommentView) -> HierarchicalComment {
guard let childIds = childrenById[comment.id] else {
return .init(comment: comment, children: [])
}

let commentWithChildren = HierarchicalComment(comment: comment, children: [])
commentWithChildren.children = childIds
.compactMap { id -> HierarchicalComment? in
guard let child = identifiedComments[id] else { return nil }
return populateChildren(child)
}

return commentWithChildren
}

let parents = allComments[..<childrenStartIndex]
let result = parents.map(populateChildren)
return result
Expand Down
2 changes: 1 addition & 1 deletion Mlem/API/Models/Community/APICommunity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension APICommunity: Equatable, Hashable {
static func == (lhs: APICommunity, rhs: APICommunity) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(self.id)
}
Expand Down
2 changes: 0 additions & 2 deletions Mlem/API/Models/Community/APICommunityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ extension APICommunityView: Hashable, Equatable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.community.id)
}


}
3 changes: 1 addition & 2 deletions Mlem/API/Models/Site/APILocalSite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct APILocalSite: Decodable {
}

// lemmy_db_schema::source::local_site::RegistrationMode
enum APIRegistrationMode: String, Codable
{
enum APIRegistrationMode: String, Codable {
case closed = "Closed"
case requireApplication = "RequireApplication"
case open = "Open"
Expand Down
1 change: 0 additions & 1 deletion Mlem/API/Models/Site/APISite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ struct APISite: Decodable {
let publicKey: String
let instanceId: Int
}

1 change: 0 additions & 1 deletion Mlem/API/Requests/Comment/DeleteComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ struct DeleteCommentRequest: APIPostRequest {
)
}
}

2 changes: 1 addition & 1 deletion Mlem/API/Requests/Comment/GetComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct GetCommentRequest: APIGetRequest {
self.queryItems = [
.init(name: "id", value: id.description),

.init(name: "auth", value: account.accessToken),
.init(name: "auth", value: account.accessToken)
]
}

Expand Down
1 change: 0 additions & 1 deletion Mlem/API/Requests/Comment/GetComments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ struct GetCommentsRequest: APIGetRequest {
.init(name: "saved_only", value: savedOnly.map(String.init))
]


}
}

Expand Down
2 changes: 1 addition & 1 deletion Mlem/API/Requests/Comment/SaveComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SaveCommentRequest: APIPutRequest {
self.body = .init(
comment_id: commentId,
save: save,

auth: account.accessToken
)
}
Expand Down
4 changes: 2 additions & 2 deletions Mlem/API/Requests/Community/ListCommunities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ListCommunitiesRequest: APIGetRequest {
.init(name: "page", value: page?.description),
.init(name: "type_", value: type.rawValue),

.init(name: "auth", value: account.accessToken),
.init(name: "auth", value: account.accessToken)
]
}

Expand All @@ -52,7 +52,7 @@ struct ListCommunitiesRequest: APIGetRequest {

.init(name: "limit", value: limit.map(String.init)),
.init(name: "page", value: page.map(String.init)),
.init(name: "type_", value: type.rawValue),
.init(name: "type_", value: type.rawValue)
]
}
}
Expand Down
5 changes: 1 addition & 4 deletions Mlem/API/Requests/Post/GetPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ struct GetPostRequest: APIGetRequest {
commentId: Int?
) {
self.instanceURL = instanceURL

var queryItems: [URLQueryItem] = [
self.queryItems = [
.init(name: "id", value: id?.description),
.init(name: "comment_id", value: commentId?.description)
]

self.queryItems = queryItems
}
}

Expand Down
5 changes: 1 addition & 4 deletions Mlem/API/Requests/Post/GetPosts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ struct GetPostsRequest: APIGetRequest {
communityName: String? = nil
) {
self.instanceURL = account.instanceLink

var queryItems: [URLQueryItem] = [
self.queryItems = [
.init(name: "auth", value: account.accessToken),
.init(name: "page", value: "\(page)"),
.init(name: "type_", value: type.rawValue),
Expand All @@ -38,8 +37,6 @@ struct GetPostsRequest: APIGetRequest {
.init(name: "limit", value: limit.map(String.init)),
.init(name: "saved_only", value: savedOnly.map(String.init))
]

self.queryItems = queryItems
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mlem/API/Requests/ResolveObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ResolveObjectRequest: APIGetRequest {
self.queryItems = [
.init(name: "q", value: query),

.init(name: "auth", value: account.accessToken),
.init(name: "auth", value: account.accessToken)
]
}
}
Expand Down
4 changes: 1 addition & 3 deletions Mlem/API/Requests/SearchRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct SearchRequest: APIGetRequest {
limit: Int? = nil
) {
self.instanceURL = account.instanceLink
let queryItems: [URLQueryItem] = [
self.queryItems = [
.init(name: "auth", value: account.accessToken),
.init(name: "type_", value: searchType.rawValue),
.init(name: "sort", value: sortOption.rawValue),
Expand All @@ -50,8 +50,6 @@ struct SearchRequest: APIGetRequest {
.init(name: "creator_id", value: creatorId.map(String.init)),
.init(name: "limit", value: limit.map(String.init))
]

self.queryItems = queryItems
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mlem/API/Requests/Site/GetSite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct GetSiteRequest: APIGetRequest {

// lemmy_api_common::site::SiteResponse
struct SiteResponse: Decodable {
let site_view: APISiteView
let siteView: APISiteView
let admins: [APIPersonView]
let version: String
let myUser: APIMyUserInfo?
Expand Down
37 changes: 28 additions & 9 deletions Mlem/App Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import Foundation
import KeychainAccess
import UIKit

struct AppConstants
{
struct AppConstants {
static let cacheSize = 500_000_000 // 500MiB in bytes
static let urlCache: URLCache = URLCache(memoryCapacity: cacheSize, diskCapacity: cacheSize)
static let webSocketSession: URLSession = URLSession(configuration: .default)
Expand All @@ -24,15 +23,35 @@ struct AppConstants
static let keychain: Keychain = Keychain(service: "com.davidbures.Mlem-keychain")

// MARK: - Files
private static let applicationSupportDirectoryPath: URL = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
static let savedAccountsFilePath: URL = applicationSupportDirectoryPath.appendingPathComponent("Saved Accounts", conformingTo: .json)
static let filteredKeywordsFilePath: URL = applicationSupportDirectoryPath.appendingPathComponent("Blocked Keywords", conformingTo: .json)
static let favoriteCommunitiesFilePath: URL = applicationSupportDirectoryPath.appendingPathComponent("Favorite Communities", conformingTo: .json)
private static let applicationSupportDirectoryPath = {
guard let path = try? FileManager.default.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
) else {
fatalError("unable to access application support path")
}

return path
}()

static let savedAccountsFilePath = { applicationSupportDirectoryPath
.appendingPathComponent("Saved Accounts", conformingTo: .json)
}()

static let filteredKeywordsFilePath = { applicationSupportDirectoryPath
.appendingPathComponent("Blocked Keywords", conformingTo: .json)
}()

static let favoriteCommunitiesFilePath = { applicationSupportDirectoryPath
.appendingPathComponent("Favorite Communities", conformingTo: .json)
}()

// MARK: - Haptics
static let hapticManager: UINotificationFeedbackGenerator = UINotificationFeedbackGenerator()

// MARK: - DragGesture thresholds
static let longSwipeDragMin: CGFloat = 150;
static let shortSwipeDragMin: CGFloat = 60;
static let longSwipeDragMin: CGFloat = 150
static let shortSwipeDragMin: CGFloat = 60
}
Loading

0 comments on commit 94bde2b

Please sign in to comment.