Skip to content

Commit

Permalink
Refactor for the new ComposableRequest
Browse files Browse the repository at this point in the history
* Update default `Client` to be an iOS device

* Update README.md

* Add Authenticator app to allow anyone to rely on `SwiftagramTests`

* Update Followers example
  • Loading branch information
sbertix committed Feb 8, 2021
1 parent 212e141 commit cde5e20
Show file tree
Hide file tree
Showing 70 changed files with 3,136 additions and 2,071 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"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"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "header.png",
"idiom" : "universal"
}
],
"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.
17 changes: 17 additions & 0 deletions Examples/Authenticator/Authenticator/AuthenticatorApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AuthenticatorApp.swift
// Authenticator
//
// Created by Stefano Bertagno on 07/02/21.
//

import SwiftUI

@main
struct AuthenticatorApp: App {
var body: some Scene {
WindowGroup {
ContentView().accentColor(.pink)
}
}
}
66 changes: 66 additions & 0 deletions Examples/Authenticator/Authenticator/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ContentView.swift
// Authenticator
//
// Created by Stefano Bertagno on 07/02/21.
//

import SwiftUI

import Swiftagram

struct ContentView: View {
/// Whether it should present the login view or not.
@State var isPresentingLoginView: Bool = false
/// The current secret.
@State var secret: Secret?

/// The actual view.
var body: some View {
VStack(spacing: 40) {
// The header.
Image("header")
.resizable()
.scaledToFit()
.frame(maxWidth: .infinity)
.padding(.horizontal, 50)
// Check for token.
if let secret = secret, let token = secret.token {
Text(token)
.font(Font.headline.smallCaps())
.foregroundColor(.primary)
.lineLimit(3)
.fixedSize(horizontal: false, vertical: true)
.onTapGesture { UIPasteboard.general.string = token }
Text("(Tap to copy it in your clipboard)")
.font(.caption)
.fixedSize(horizontal: false, vertical: true)
} else {
// The disclaimer.
Text.combine(
Text("Please authenticate with your "),
Text("Instagram").bold(),
Text(" account to receive a token for "),
Text("SwiftagramTests").bold()
)
.fixedSize(horizontal: false, vertical: true)
// Login.
Button(action: { isPresentingLoginView = true }) {
Text("Authenticate").font(.headline)
}.foregroundColor(.accentColor)
}
}
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.vertical)
.padding(.horizontal, 50)
.sheet(isPresented: $isPresentingLoginView) { LoginView(secret: $secret) }
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
50 changes: 50 additions & 0 deletions Examples/Authenticator/Authenticator/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
</dict>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UILaunchScreen</key>
<dict/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
54 changes: 54 additions & 0 deletions Examples/Authenticator/Authenticator/LoginView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// LoginView.swift
// Authenticator
//
// Created by Stefano Bertagno on 07/02/21.
//

import SwiftUI
import UIKit
import WebKit

import Swiftagram

class LoginViewController: UIViewController {
/// The completion handler.
var completion: ((Secret) -> Void)?
/// The web view.
var webView: WKWebView? {
didSet {
guard let webView = webView else { return }
webView.frame = view.frame
view.addSubview(webView)
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Authenticate.
WebViewAuthenticator {
self.webView = $0
}.authenticate { [weak self] in
switch $0 {
case .failure(let error): print(error.localizedDescription)
case .success(let secret):
self?.completion?(secret)
self?.dismiss(animated: true, completion: nil)
}
}
}
}

/// A `struct` defining a `View` used for logging in.
struct LoginView: UIViewControllerRepresentable {
/// The current secret.
@Binding var secret: Secret?

func makeUIViewController(context: Context) -> LoginViewController {
let controller = LoginViewController()
controller.completion = { self.secret = $0 }
return controller
}
func updateUIViewController(_ uiViewController: LoginViewController, context: Context) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
15 changes: 15 additions & 0 deletions Examples/Authenticator/Authenticator/Secret.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Secret.swift
// Authenticator
//
// Created by Stefano Bertagno on 07/02/21.
//

import Foundation

import Swiftagram

extension Secret {
/// Compute the token.
var token: String? { try? JSONEncoder().encode(self).base64EncodedString() }
}
31 changes: 31 additions & 0 deletions Examples/Authenticator/Authenticator/Text.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Text.swift
// Authenticator
//
// Created by Stefano Bertagno on 07/02/21.
//

import Foundation

import SwiftUI

extension Text {
/// Combine a collection of texts.
///
/// - parameters: A collection of `Text`s.
/// - returns: A valid `Text`.
static func combine(_ texts: Text...) -> Text {
combine(texts)
}

/// Combine a collection of texts.
///
/// - parameters: A collection of `Text`s.
/// - returns: A valid `Text`.
static func combine(_ texts: [Text]) -> Text {
guard let first = texts.first else {
fatalError("`texts` should not be empty")
}
return texts.dropFirst().reduce(first) { $0+$1 }
}
}
Loading

0 comments on commit cde5e20

Please sign in to comment.