Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
developersancho committed Feb 26, 2022
1 parent dad2110 commit e0e6311
Show file tree
Hide file tree
Showing 30 changed files with 714 additions and 20 deletions.
226 changes: 226 additions & 0 deletions SwiftRorty.iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>SwiftRorty.iOS.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>4</integer>
</dict>
</dict>
</dict>
Expand Down
11 changes: 0 additions & 11 deletions SwiftRorty.iOS/Assets.xcassets/AccentColor.colorset/Contents.json

This file was deleted.

20 changes: 20 additions & 0 deletions SwiftRorty.iOS/Assets.xcassets/blue_primary.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0x41",
"green" : "0x29",
"red" : "0x25"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
21 changes: 21 additions & 0 deletions SwiftRorty.iOS/Assets.xcassets/ic_splash.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "ic_app_logo.jpeg",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion SwiftRorty.iOS/SwiftRorty_iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct SwiftRorty_iOSApp: App {
var body: some Scene {
WindowGroup {
ContentView()
SplashScreen()
}
}
}
38 changes: 38 additions & 0 deletions SwiftRorty.iOS/component/LottieView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// LottieView.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import SwiftUI
import Foundation
import Lottie

struct LottieView: UIViewRepresentable {
@State var name: String
var loopMode: LottieLoopMode = .playOnce
var animationView = AnimationView()

func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
let view = UIView()

animationView.animation = Animation.named(name)
animationView.contentMode = .scaleAspectFill
animationView.loopMode = .loop

animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)

NSLayoutConstraint.activate([
animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
])

return view
}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
animationView.play()
}
}
13 changes: 13 additions & 0 deletions SwiftRorty.iOS/core/network/APIClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// APIClient.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation
import Combine

protocol APIClient {
func request<T: Decodable>(_ urlRequest: URLRequest) -> AnyPublisher<T, BaseError>
}
26 changes: 26 additions & 0 deletions SwiftRorty.iOS/core/network/APIClientImpl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// APIClientImpl.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation
import Combine

class APIClientImpl: APIClient {

private let session: URLSession

init(session: URLSession = .shared) {
self.session = session
}

func request<T>(_ urlRequest: URLRequest) -> AnyPublisher<T, BaseError> where T : Decodable {
session.dataTaskPublisher(for: urlRequest)
.mapError({ .api(description: $0.localizedDescription) })
.flatMap { response in
Decoder().decode(response.data)
}.eraseToAnyPublisher()
}
}
16 changes: 16 additions & 0 deletions SwiftRorty.iOS/core/network/APIMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// APIMethod.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

enum APIMethod: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
}
13 changes: 13 additions & 0 deletions SwiftRorty.iOS/core/network/BaseError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BaseError.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

enum BaseError : Error {
case parse(description: String)
case api(description: String)
}
57 changes: 57 additions & 0 deletions SwiftRorty.iOS/core/network/URLRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// URLRequest.swift
// SwiftRorty.iOS
//
// Created by developersancho on 26.02.2022.
//

import Foundation

extension URLRequest {
private static let baseUrl = "https://rickandmortyapi.com/api/"

init(_ endpoint: APIEndpoint,
_ method: APIMethod,
_ parameters: [String: Any?]? = nil) {
let urlString = "\(URLRequest.baseUrl)\(endpoint.path())"
let url = URL(string: urlString)!
self.init(url: url)
self.httpMethod = method.rawValue
processParameters(method, parameters)
self.timeoutInterval = 30
}

private mutating func processParameters(_ method: APIMethod, _ parameters: [String: Any?]? = nil) {
switch method {
case .get:
processGetParameters(parameters)
default:
processPostParameters(parameters)
}
}

private mutating func processPostParameters(_ parameters: [String: Any?]? = nil) {
if let parameters = parameters, let jsonParameters = try? JSONSerialization.data(withJSONObject: parameters,
options: []) {
self.httpBody = jsonParameters
}
}

private mutating func processGetParameters(_ parameters: [String: Any?]? = nil) {
guard let parameters = parameters, !parameters.isEmpty else { return }
let queryParameters = parameters.reduce("?") { (result, element) -> String in
guard let value = element.value else { return result }
if result.count > 1 {
return "\(result)&\(element.key)=\(value)"
}
return "\(result)\(element.key)=\(value)"
}
var queryCharSet = NSCharacterSet.urlQueryAllowed
queryCharSet.remove(charactersIn: "+")
if let url = self.url?.absoluteString,
let urlQueryParameters = queryParameters.addingPercentEncoding(withAllowedCharacters: queryCharSet) {
let urlWithParameters = "\(url)\(urlQueryParameters)"
self.url = URL(string: urlWithParameters)!
}
}
}
29 changes: 29 additions & 0 deletions SwiftRorty.iOS/data/model/dto/CharacterDto.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// CharacterDto.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation
import UIKit

struct CharacterDto: Identifiable {
let created: String?
let episode: [String]?
let gender: String?
let id: Int?
let image: String?
let location: Location?
let name: String?
let origin: Origin?
let species: String?
let status: Status?
let type: String?
let url: String?
var isFavorite: Bool = false

static func defaultDto() -> CharacterDto {
return CharacterDto(created: nil, episode: nil, gender: nil, id: 1, image: nil, location: nil, name: "Rick Sanchez", origin: nil, species: "Human", status: Status.Alive, type: nil, url: nil)
}
}
33 changes: 33 additions & 0 deletions SwiftRorty.iOS/data/model/dto/DtoExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// DtoExtension.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation
import SwiftUI

extension CharacterResponse {
func toCharacterDtoList() -> [CharacterDto]? {
results?.map { $0.toCharacterDto() }
}
}

extension CharacterInfo {
func toCharacterDto() -> CharacterDto {
return .init(created: created, episode: episode, gender: gender, id: id, image: image, location: location, name: name, origin: origin, species: species, status: status, type: type, url: url)
}
}

extension Location {
func toLocationDto() -> LocationDto {
return .init(locationId: 0, name: name, url: url)
}
}

extension Origin {
func toLocationDto() -> LocationDto {
return .init(locationId: 0, name: name, url: url)
}
}
13 changes: 13 additions & 0 deletions SwiftRorty.iOS/data/model/dto/KeyValueModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// KeyValueModel.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

struct KeyValueModel {
let key: String?
let value: String?
}
14 changes: 14 additions & 0 deletions SwiftRorty.iOS/data/model/dto/LocationDto.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// LocationDto.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

struct LocationDto {
let locationId: Int?
let name: String?
let url: String?
}
24 changes: 24 additions & 0 deletions SwiftRorty.iOS/data/model/dto/StringExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// StringExtension.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

// command + shift + / => comment/uncomment
extension String {
// func substringWithLastInstanceOf(character: Character) -> String? {
// if let rangeOfIndex = rangeOfCharacterFromSet(NSCharacterSet(charactersInString: String(character)), options: .BackwardsSearch) {
// return self.substringToIndex(rangeOfIndex.endIndex)
// }
// return nil
// }
// func substringWithoutLastInstanceOf(character: Character) -> String? {
// if let rangeOfIndex = rangeOfCharacterFromSet(NSCharacterSet(charactersInString: String(character)), options: .BackwardsSearch) {
// return self.substringToIndex(rangeOfIndex.startIndex)
// }
// return nil
// }
}
18 changes: 18 additions & 0 deletions SwiftRorty.iOS/data/remote/APIEndpoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// APIEndpoint.swift
// SwiftRorty.iOS
//
// Created by developersancho on 25.02.2022.
//

import Foundation

enum APIEndpoint {
case characters(Int)

func path() -> String {
switch self {
case .characters(let page): return "character/?page=\(page)"
}
}
}
Loading

0 comments on commit e0e6311

Please sign in to comment.