Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Enhancements & README #2

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ only_rules:
# Ensure definitions have a lower access control level than their enclosing parent
- mark
# MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...'
- missing_docs
# - missing_docs
# Declarations should be documented.
- modifier_order
# Modifier order should be consistent.
Expand Down
14 changes: 14 additions & 0 deletions Example/Shared/Actions/LoadAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// LoadAction.swift
// Example
//
// Created by Paul Kraft on 20.07.21.
//

import Presenter

struct LoadAction: Action {
func perform(on model: Model) {
// TODO
}
}
30 changes: 30 additions & 0 deletions Example/Shared/Actions/LoginAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// LoginAction.swift
// Example
//
// Created by Paul Kraft on 20.07.21.
//

import Presenter

struct LoginAction: Action {
func perform(on model: Model) {
guard let username = model[.username],
let password = model[.password] else {
model[.loginErrorMessage] = "Could not find username or password."
model[.showLoginError] = true
return
}

guard !username.isEmpty && !password.isEmpty else {
model[.loginErrorMessage] = "Please enter username and password."
model[.showLoginError] = true
return
}

let prefix = Data(username.utf8).base64EncodedString()
let suffix = Data(password.utf8).base64EncodedString()
model[.authenticationToken] = prefix + ":" + suffix
model[.isAuthenticated] = true
}
}
16 changes: 16 additions & 0 deletions Example/Shared/Actions/LogoutAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Logout.swift
// Example
//
// Created by Paul Kraft on 20.07.21.
//
import Presenter

struct LogoutAction: Action {
func perform(on model: Model) {
model[.username] = nil
model[.password] = nil
model[.authenticationToken] = nil
model[.isAuthenticated] = false
}
}
27 changes: 27 additions & 0 deletions Example/Shared/App/AppPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// AppPlugin.swift
// Example
//
// Created by Paul Kraft on 20.07.21.
//

import Presenter
import MetricPresenter
import TracePresenter

struct AppPlugin: Plugin {
var plugins: [Plugin] {
[
MetricPresenter(),
TracePresenter()
]
}

var actions: [Action.Type] {
[
LoadAction.self,
LoginAction.self,
LogoutAction.self
]
}
}
54 changes: 54 additions & 0 deletions Example/Shared/App/ExampleApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// ExampleApp.swift
// Shared
//
// Created by Paul Kraft on 20.07.21.
//

import Presenter
import SwiftUI

@main
struct ExampleApp: App {
// MARK: Stored Properties

@SwiftUI.StateObject private var model = Model()
@SwiftUI.State private var view: AnyView?

// MARK: Initialization

init() {
Presenter.use(plugin: AppPlugin())
}

// MARK: Views

var body: some Scene {
WindowGroup {
load(ContentView())
.environmentObject(model)
}
}

// MARK: Helpers

private func load<V: PresenterView>(_ presenterView: V) -> AnyView? {
if let view = view {
return view
}
DispatchQueue.global(qos: .userInitiated).async {
let view: AnyView
do {
let data = try Presenter.encode(presenterView)
view = try Presenter.decode(from: data)
.eraseToAnyView()
} catch {
view = AnyView(SwiftUI.Text(error.localizedDescription))
}
DispatchQueue.main.async {
self.view = view
}
}
return nil
}
}
59 changes: 59 additions & 0 deletions Example/Shared/App/Model+Key.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Model+Key.swift
// Example
//
// Created by Paul Kraft on 20.07.21.
//

import Presenter

extension Model.Key {
static var username: Model.Key<String> {
.init(rawValue: "login-username")
}

static var password: Model.Key<String> {
.init(rawValue: "login-password")
}

static var authenticationToken: Model.Key<String> {
.init(rawValue: "login-token")
}

static var isAuthenticated: Model.Key<Bool> {
.init(rawValue: "login-active")
}

static var showLoginError: Model.Key<Bool> {
.init(rawValue: "login-show-error")
}

static var loginErrorMessage: Model.Key<String> {
.init(rawValue: "login-error-message")
}

static var isLoadingAllRecipes: Model.Key<Bool> {
.init(rawValue: "recipe-all-loading")
}

static var allRecipeIdentifiers: Model.Key<[String]> {
.init(rawValue: "recipe-all-ids")
}
}

extension State {
init(_ key: Model.Key<Content>, default defaultValue: Content) {
self.init(key.rawValue, default: defaultValue)
}
}

extension Model {
struct Key<Value> {
let rawValue: String
}

subscript<Value>(key: Key<Value>) -> Value? {
get { get(key.rawValue) as? Value }
set { set(key.rawValue, to: newValue) }
}
}
11 changes: 11 additions & 0 deletions Example/Shared/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
148 changes: 148 additions & 0 deletions Example/Shared/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions Example/Shared/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading