Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nuomi1 committed Mar 14, 2021
2 parents 39578d8 + b9c1312 commit c0613e1
Show file tree
Hide file tree
Showing 18 changed files with 1,144 additions and 240 deletions.
1 change: 1 addition & 0 deletions Example/NBus/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<array>
<string>mqq</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdklaunchminiapp</string>
<string>mqqopensdkminiapp</string>
<string>mqqopensdknopasteboard</string>
<string>weixin</string>
Expand Down
174 changes: 174 additions & 0 deletions Example/NBus/View/PlatformViewController+Launch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//
// PlatformViewController+Launch.swift
// BusMock
//
// Created by nuomi1 on 2021/3/9.
// Copyright © 2021 nuomi1. All rights reserved.
//

import NBus
import RxSwift
import UIKit

extension PlatformViewController {

class LaunchView: UIView {

typealias Constant = PlatformViewController.Constant
typealias ViewModel = PlatformViewController.ViewModel

var onLaunch: (MiniProgramMessage, Platform) -> Void = { _, _ in }

private var viewModel: ViewModel?

private let disposeBag = DisposeBag()

private let launchButton = UIButton(type: Constant.buttonType)

private var allButtons: [UIButton] {
return [launchButton]
}

private var allSubviews: [UIView] {
return allButtons
}

override init(frame: CGRect) {
super.init(frame: frame)

setupSubviews()
setupBinding()
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}

extension PlatformViewController.LaunchView {

override func sizeThatFits(_ size: CGSize) -> CGSize {
reLayout()

let padding = UIEdgeInsets(
top: Constant.spacing,
left: 0,
bottom: Constant.spacing,
right: 0
)

pin
.width(size.width)
.wrapContent(.vertically, padding: padding)

return bounds.size
}

override func layoutSubviews() {
super.layoutSubviews()

reLayout()
}

private func reLayout() {
guard
let superview = superview
else { assertionFailure(); return }

var edge = self.edge.top

allSubviews.forEach { subview in
let size: CGSize = {
(allButtons as [UIView]).contains(subview)
? CGSize(
width: subview.intrinsicContentSize.width * Constant.widthMultiple,
height: subview.intrinsicContentSize.height
)
: CGSize(
width: max(0, superview.bounds.size.width - 2 * Constant.spacing),
height: 5 * Constant.spacing
)
}()

subview.pin
.size(size)
.hCenter()

let marginTop = (allButtons as [UIView]).contains(subview)
? Constant.spacing
: 2 * Constant.spacing

subview.pin
.top(to: edge)
.marginTop(marginTop)

edge = subview.edge.bottom
}
}

private func setupSubviews() {
launchButton.setTitle("\(Messages.miniProgram)", for: .normal)
setupBorder(launchButton)

allSubviews.forEach(addSubview)
}

private func setupBinding() {
launchButton.rx
.tap
.bind(onNext: didTapLaunchButton)
.disposed(by: disposeBag)
}

private func setupBorder(_ view: UIView) {
view.layer.borderColor = Constant.borderColor
view.layer.borderWidth = Constant.borderWidth
view.layer.cornerRadius = Constant.cornerRadius
}
}

extension PlatformViewController.LaunchView {

func binding(_ viewModel: ViewModel) {
self.viewModel = viewModel

viewModel.isLaunchEnabled
.bind(to: launchButton.rx.isEnabled)
.disposed(by: disposeBag)
}
}

extension PlatformViewController.LaunchView {

private func didTapLaunchButton() {
guard
let platform = viewModel?.platform.value
else { assertionFailure(); return }

let message: MessageType

switch platform {
case Platforms.wechat:
message = MediaSource.wechatMiniProgram
case Platforms.qq:
message = MediaSource.qqMiniProgram
default:
message = MediaSource.wechatMiniProgram
}

guard
let program = message as? MiniProgramMessage
else { assertionFailure(); return }

launch(program: program, with: platform)
}
}

extension PlatformViewController.LaunchView {

private func launch(program: MiniProgramMessage, with platform: Platform) {
onLaunch(program, platform)
}
}
3 changes: 3 additions & 0 deletions Example/NBus/View/PlatformViewController+ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension PlatformViewController {

let isShareEnabled: Observable<Bool>
let isOauthEnabled: Observable<Bool>
let isLaunchEnabled: Observable<Bool>

init(_ element: AppState.PlatformItem) {
title = .just("\(element.platform)")
Expand All @@ -54,6 +55,8 @@ extension PlatformViewController {
.map { $0 is ShareHandlerType }
isOauthEnabled = currentHandler
.map { $0 is OauthHandlerType }
isLaunchEnabled = currentHandler
.map { $0 is LaunchHandlerType }
}

static let defaultOauthInfo = OauthInfo(isLogin: false, parameter: "未登录")
Expand Down
50 changes: 49 additions & 1 deletion Example/NBus/View/PlatformViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ class PlatformViewController: UIViewController {

private let shareView = TitleContentView(title: "分享", contentView: ShareView())
private let oauthView = TitleContentView(title: "登录", contentView: OauthView())
private let launchView = TitleContentView(title: "启动", contentView: LaunchView())
}

extension PlatformViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .white
if #available(iOS 13.0, *) {
view.backgroundColor = .systemBackground
} else {
view.backgroundColor = .white
}

setupNavigationItem()
setupSubviews()
Expand All @@ -67,6 +72,11 @@ extension PlatformViewController {
.sizeToFit(.width)
.below(of: shareView)

launchView.pin
.horizontally()
.sizeToFit(.width)
.below(of: oauthView)

let minHeight = scrollView.bounds.size.height
- scrollView.pin.safeArea.top
- scrollView.pin.safeArea.bottom
Expand All @@ -88,6 +98,7 @@ extension PlatformViewController {

contentView.addSubview(shareView)
contentView.addSubview(oauthView)
contentView.addSubview(launchView)
}
}

Expand Down Expand Up @@ -141,6 +152,11 @@ extension PlatformViewController {
oauthView.contentView.onOauth = { [weak self] platform in
self?.oauth(with: platform)
}

launchView.contentView.binding(viewModel)
launchView.contentView.onLaunch = { [weak self] program, platform in
self?.launch(program: program, with: platform)
}
}
}

Expand Down Expand Up @@ -222,4 +238,36 @@ extension PlatformViewController {
self.present(alert, animated: true)
}
}

private func launch(program: MiniProgramMessage, with platform: Platform) {
Bus.shared.launch(program: program, with: platform) { [weak self] result in
guard let self = self else { return }

let alert: UIAlertController

switch result {
case .success:
alert = UIAlertController(
title: "Success",
message: nil,
preferredStyle: .alert
)
case let .failure(error):
alert = UIAlertController(
title: "Failure",
message: "\(error)",
preferredStyle: .alert
)
}

let okAction = UIAlertAction(
title: "OK",
style: .default
)

alert.addAction(okAction)

self.present(alert, animated: true)
}
}
}
28 changes: 14 additions & 14 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
PODS:
- NBus/BusHandlers (0.9.1):
- NBus/BusHandlers (0.11.0):
- NBus/QQHandler
- NBus/SystemHandler
- NBus/WechatHandler
- NBus/WeiboHandler
- NBus/Core (0.9.1)
- NBus/QQHandler (0.9.1):
- NBus/Core (0.11.0)
- NBus/QQHandler (0.11.0):
- NBus/Core
- NBus/QQSDK (0.9.1)
- NBus/QQSDKHandler (0.9.1):
- NBus/QQSDK (0.11.0)
- NBus/QQSDKHandler (0.11.0):
- NBus/Core
- NBus/QQSDK
- NBus/SDKHandlers (0.9.1):
- NBus/SDKHandlers (0.11.0):
- NBus/QQSDKHandler
- NBus/SystemHandler
- NBus/WechatSDKHandler
- NBus/WeiboSDKHandler
- NBus/SystemHandler (0.9.1):
- NBus/SystemHandler (0.11.0):
- NBus/Core
- NBus/WechatHandler (0.9.1):
- NBus/WechatHandler (0.11.0):
- NBus/Core
- NBus/WechatSDK (0.9.1)
- NBus/WechatSDKHandler (0.9.1):
- NBus/WechatSDK (0.11.0)
- NBus/WechatSDKHandler (0.11.0):
- NBus/Core
- NBus/WechatSDK
- NBus/WeiboHandler (0.9.1):
- NBus/WeiboHandler (0.11.0):
- NBus/Core
- NBus/WeiboSDK (0.9.1)
- NBus/WeiboSDKHandler (0.9.1):
- NBus/WeiboSDK (0.11.0)
- NBus/WeiboSDKHandler (0.11.0):
- NBus/Core
- NBus/WeiboSDK
- PinLayout (1.9.3)
Expand Down Expand Up @@ -62,7 +62,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
NBus: 8ecb3a7eb79360d65d22b5ca785164ca371f106d
NBus: 1523db5c47d0665c2dd3246e1f60495cd1b5e6e9
PinLayout: 4d8733121f8687edcc8f00c19bf1379d12808767
RxCocoa: 3f79328fafa3645b34600f37c31e64c73ae3a80e
RxRelay: 8d593be109c06ea850df027351beba614b012ffb
Expand Down
2 changes: 1 addition & 1 deletion NBus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "NBus"
s.version = "0.11.0"
s.version = "1.0.0"
s.summary = "A short description of NBus."

s.homepage = "https://github.com/nuomi1/NBus"
Expand Down
19 changes: 19 additions & 0 deletions NBus/Classes/Core/Bus+Debug.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Bus+Debug.swift
// NBus
//
// Created by nuomi1 on 2021/3/9.
// Copyright © 2021 nuomi1. All rights reserved.
//

import Foundation

func busAssertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #file,
line: UInt = #line
) {
if Bus.shared.isDebugEnabled {
assertionFailure(message(), file: file, line: line)
}
}
Loading

0 comments on commit c0613e1

Please sign in to comment.