<file_sep>//
//  SDSOrientationAdaptiveStack.swift
//
//  Created by : <NAME> on 2021/06/03
//  © 2021  SmallDeskSoftware
//

import SwiftUI
import Combine
import UIKit

extension UIDevice {
    static public func stableDeviceOrientation() -> UIDeviceOrientation {
        if UIDevice.current.orientation.isValidInterfaceOrientation {
            return UIDevice.current.orientation
        } else {
            return UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height ? UIDeviceOrientation.landscapeLeft : UIDeviceOrientation.portrait
        }
    }
}

extension UIDeviceOrientation: CustomStringConvertible {
    public var description: String {
        switch self {
        case .unknown:
            return "unknown"
        case .portrait:
            return "portrait"
        case .portraitUpsideDown:
            return "portraitUpsideDown"
        case .landscapeLeft:
            return "landscapeLeft"
        case .landscapeRight:
            return "landscapeRight"
        case .faceUp:
            return "faceUp"
        case .faceDown:
            return "faceDown"
        default:
            return "unknown"
        }
    }
}

public struct SDSOrientationAdaptiveStack<Content1: View, Content2: View> : View {
    var firstContent: Content1
    var secondContent: Content2
    @State var orientation: UIDeviceOrientation = UIDevice.current.orientation
    @Binding var firstContentRatio: CGFloat
    
    public init(@ViewBuilder first: () -> Content1, @ViewBuilder second: () -> Content2, ratio: Binding<CGFloat> = .constant(0.5) ) {
        self.firstContent = first()
        self.secondContent = second()
        self._firstContentRatio = ratio
        self._orientation = State(wrappedValue: UIDevice.stableDeviceOrientation())
    }

    public var body: some View {
        ZStack {
            GeometryReader { geom in
                if orientation == .landscapeRight || orientation == .landscapeLeft {
                    HStack {
                        if firstContentRatio > 0.01 {
                            firstContent
                                .frame(width: geom.size.width * firstContentRatio)
                        } else {
                            EmptyView()
                        }
                        if firstContentRatio < 0.99 {
                            secondContent
                                .frame(width: geom.size.width * (1.0-firstContentRatio))
                        } else {
                            EmptyView()
                        }
                    }
                } else {
                    VStack {
                        if firstContentRatio > 0.01 {
                            firstContent
                                .frame(height: geom.size.height * firstContentRatio)
                        } else {
                            EmptyView()
                        }
                        if firstContentRatio < 0.99 {
                            secondContent
                                .frame(height: geom.size.height * (1.0-firstContentRatio))
                        } else {
                            EmptyView()
                        }
                    }
                }
            }
        }
        .onAppear(perform: {
            orientation = UIDevice.stableDeviceOrientation()
        })
        .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
            orientation = UIDevice.stableDeviceOrientation()
        }
    }
    
}
<file_sep># SDSOrientationAdaptiveStack


![macOS iOS](https://img.shields.io/badge/platform-iOS_macOS-lightgrey)
![SPM is supported](https://img.shields.io/badge/SPM-Supported-orange)
![license](https://img.shields.io/badge/license-MIT-lightgrey)

<!--
comment
-->

## Feature

switch HStack / VStack according to device orientation

## in 30 sec.
https://user-images.githubusercontent.com/6419800/120910504-683d5a00-c6ba-11eb-973b-a091a29f4f7b.mp4

## Code Example
```
//
//  ContentView.swift
//
//  Created by : <NAME> on 2021/06/03
//  © 2021  SmallDeskSoftware
//

import SwiftUI
import SDSOrientationAdaptiveStack

struct ContentView: View {
    var body: some View {
        SDSOrientationAdaptiveStack(first: {
            Rectangle()
                .fill(Color.red)
                .frame(width: 100, height: 100)
                .overlay(Text("First"))
        }, second: {
            Rectangle()
                .fill(Color.green)
                .frame(width: 100, height: 100)
                .overlay(Text("Second"))
        })
            .padding()
    }
}
```

## Installation
use Swift Package Manager: URL: https://github.com/tyagishi/SDSOrientationAdaptiveStack

## Requirements
none

## Note
<file_sep>    import XCTest
    @testable import SDSOrientationAdaptiveStack
    import SwiftUI
    import UIKit

    final class SDSOrientationAdaptiveStackTests: XCTestCase {
        func testLayout() throws {
            let sut = ContentView()
            XCTAssertNotNil(sut)
        }
    }

    struct ContentView: View {
        var body: some View {
            SDSOrientationAdaptiveStack(first: {
                Rectangle()
                    .fill(Color.red)
                    .frame(width: 100, height: 100)
                    .overlay(Text("First"))
                    .accessibility(identifier: "firstRect")
            }, second: {
                Rectangle()
                    .fill(Color.green)
                    .frame(width: 100, height: 100)
                    .overlay(Text("Second"))
            })
                .padding()
        }
    }
    
    
