-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ddb6ed
commit 909e5e4
Showing
7 changed files
with
551 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// APIService.swift | ||
// Lunar | ||
// | ||
// Created by Mani on 05/12/2023. | ||
// | ||
|
||
import Foundation | ||
import Moya | ||
|
||
let lemmyProvider = MoyaProvider<LemmyAPI>() | ||
|
||
enum LemmyAPI { | ||
case listPosts(parameters: ListPostsParameters) | ||
case createPost(parameters: CreatePostParameters) | ||
} | ||
|
||
extension LemmyAPI: TargetType { | ||
var baseURL: URL { | ||
return URL(string: "https://lemmy.world/api/v3")! | ||
} | ||
|
||
var path: String { | ||
switch self { | ||
case .listPosts: return "/post/list" | ||
case .createPost: return "/post" | ||
} | ||
} | ||
|
||
var method: Moya.Method { | ||
switch self { | ||
case .listPosts: .get | ||
case .createPost: .post | ||
} | ||
} | ||
|
||
// var sampleData: Data { | ||
// return Data() // You can provide sample data for testing | ||
// } | ||
|
||
var task: Task { | ||
switch self { | ||
case .listPosts(let parameters): | ||
var params: [String: Any] = [:] | ||
if let type = parameters.type { params["type_"] = type } | ||
if let sort = parameters.sort { params["sort"] = sort } | ||
if let page = parameters.page { params["page"] = page } | ||
if let limit = parameters.limit { params["limit"] = limit } | ||
if let communityId = parameters.communityId { params["community_id"] = communityId } | ||
if let communityName = parameters.communityName { params["community_name"] = communityName } | ||
if let savedOnly = parameters.savedOnly { params["saved_only"] = savedOnly } | ||
if let likedOnly = parameters.likedOnly { params["liked_only"] = likedOnly } | ||
if let dislikedOnly = parameters.dislikedOnly { params["disliked_only"] = dislikedOnly } | ||
if let pageCursor = parameters.pageCursor { params["page_cursor"] = pageCursor } | ||
return .requestParameters(parameters: params, encoding: URLEncoding.default) | ||
|
||
case .createPost(parameters: let parameters): | ||
var params: [String: Any] = [ | ||
"name": parameters.name, | ||
"community_id": parameters.communityId, | ||
"language_id": parameters.languageId, | ||
"auth": parameters.auth | ||
] | ||
if let url = parameters.url { params["url"] = url } | ||
if let body = parameters.body { params["body"] = body } | ||
if let nsfw = parameters.nsfw { params["nsfw"] = nsfw } | ||
return .requestParameters(parameters: params, encoding: JSONEncoding.default) | ||
} | ||
} | ||
|
||
var headers: [String: String]? { | ||
switch self { | ||
case .createPost(let parameters): | ||
return ["Authorization": "Bearer \(parameters.auth)"] | ||
default: | ||
return ["Content-Type": "application/json"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// AppDelegate.swift | ||
// Lunar | ||
// | ||
// Created by Mani on 17/09/2023. | ||
// | ||
|
||
import Defaults | ||
import Foundation | ||
import LocalConsole | ||
import Nuke | ||
import RealmSwift | ||
import SwiftUI | ||
|
||
class AppDelegateM: UIResponder, UIApplicationDelegate { | ||
@Default(.appBundleID) var appBundleID | ||
|
||
var dataCacheHolder: DataCacheHolder? | ||
|
||
func application( | ||
_: UIApplication, | ||
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil | ||
) -> Bool { | ||
print("App Started") | ||
initialiseLocalConsole() | ||
initialiseNukeUI() | ||
initialiseRealm() | ||
PhaseChangeActions().homeScreenQuickActions() | ||
listPostsMoya(parameters: ListPostsParameters(type: "All", sort: "Active")) | ||
return true | ||
} | ||
|
||
func initialiseLocalConsole() { | ||
_ = LCManager.shared | ||
} | ||
|
||
func initialiseNukeUI() { | ||
DataLoader.sharedUrlCache.diskCapacity = 0 | ||
|
||
let pipeline = ImagePipeline { | ||
self.dataCacheHolder = DataCacheHolder(appBundleID: self.appBundleID) | ||
$0.dataCache = self.dataCacheHolder?.dataCache | ||
$0.dataCachePolicy = .storeAll | ||
$0.isProgressiveDecodingEnabled = true | ||
} | ||
|
||
ImagePipeline.shared = pipeline | ||
} | ||
|
||
func initialiseRealm() { | ||
print("Realm DB Path:") | ||
print("\(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.path)") | ||
|
||
let config = Realm.Configuration( | ||
schemaVersion: 3, | ||
migrationBlock: { _, oldSchemaVersion in | ||
if oldSchemaVersion < 1 {} | ||
}, | ||
deleteRealmIfMigrationNeeded: true | ||
) | ||
Realm.Configuration.defaultConfiguration = config | ||
} | ||
|
||
func listPostsMoya(parameters: ListPostsParameters = ListPostsParameters()) { | ||
lemmyProvider.request(.listPosts(parameters: parameters)) { result in | ||
switch result { | ||
case let .success(response): | ||
do { | ||
let decoder = JSONDecoder() | ||
let postsModel = try decoder.decode(PostModel.self, from: response.data) | ||
|
||
print(postsModel.posts[0].post.name) | ||
print(postsModel.posts[1].post.name) | ||
print(postsModel.posts[2].post.name) | ||
|
||
} catch { | ||
print("Error decoding posts: \(error)") | ||
} | ||
|
||
case let .failure(error): | ||
print("Network request failed: \(error)") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.