Skip to content

Commit

Permalink
[Feat] sopt-makers#336 - MDS 토스트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
lsj8706 committed Dec 25, 2023
1 parent 3ce383c commit f36304d
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 0 deletions.
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,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "alert.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "alert@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "alert@3x.png",
"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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions SOPT-iOS/Projects/Modules/DSKit/Sources/Components/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import UIKit
import SnapKit

public extension UIViewController {
@available(*, deprecated, message: "Use MDS toast")
func showToast(message: String) {
Toast.show(message: message, view: self.view, safeAreaBottomInset: self.safeAreaBottomInset())
}

func showMDSToast(type: MDSToast.ToastType, text: String, linkButtonAction: (() -> Void)? = nil) {
Toast.showMDSToast(type: type, text: text, linkButtonAction: linkButtonAction)
}
}

public class Toast {
Expand Down Expand Up @@ -63,4 +68,32 @@ public class Toast {
})
})
}

public static func showMDSToast(type: MDSToast.ToastType, text: String, linkButtonAction: (() -> Void)? = nil) {
let toast = MDSToast(type: type, text: text, linkButtonAction: linkButtonAction)

guard let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene else { return }
guard let window = scene.windows.first(where: { $0.isKeyWindow }) else { return }

window.addSubview(toast)

let toastHeight = 48.f

let toastStartingInset = window.safeAreaInsets.top + toastHeight

toast.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview().inset(16)
make.top.equalTo(window.safeAreaLayoutGuide).inset(-toastStartingInset)
}

UIView.animate(withDuration: 0.5) {
toast.transform = CGAffineTransform(translationX: 0, y: toastStartingInset + 16.f)
} completion: { _ in
UIView.animate(withDuration: 0.5, delay: 4.0, animations: {
toast.transform = .identity
}) { _ in
toast.removeFromSuperview()
}
}
}
}
137 changes: 137 additions & 0 deletions SOPT-iOS/Projects/Modules/DSKit/Sources/MDS/MDSToast.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// MDSToast.swift
// DSKit
//
// Created by sejin on 12/25/23.
// Copyright © 2023 SOPT-iOS. All rights reserved.
//

import UIKit
import Combine

import Core

public class MDSToast: UIView {

// MARK: - Properties

private let type: ToastType
private let text: String
private var linkButtonAction: (() -> Void)?
private let cancelBag = CancelBag()

// MARK: - UI Components

private let toastIconImageView = UIImageView()

private let contentLabel: UILabel = {
let label = UILabel()
label.font = UIFont.MDS.label3
label.textColor = DSKitAsset.Colors.gray900.color
label.numberOfLines = 2
label.textAlignment = .left
return label
}()

private let linkButton: UIButton = {
var config = UIButton.Configuration.plain()
config.baseBackgroundColor = .clear
let attributes = AttributeContainer([.font: UIFont.MDS.label3,
.foregroundColor: DSKitAsset.Colors.success])

var title = AttributedString.init("보러가기", attributes: attributes)

config.attributedTitle = title
config.contentInsets = .zero
return UIButton(configuration: config)
}()

private let containerStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.spacing = 8
stackView.alignment = .center
return stackView
}()

// MARK: - initialization

init(type: ToastType, text: String, linkButtonAction: (() -> Void)? = nil) {
self.type = type
self.text = text
self.linkButtonAction = linkButtonAction
super.init(frame: .zero)
self.setUI()
self.setLayout()
self.setLinkButtonAction()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - UI & Layout

private func setUI() {
self.toastIconImageView.image = self.type.icon
self.contentLabel.text = self.text
self.backgroundColor = DSKitAsset.Colors.gray10.color
self.clipsToBounds = true
self.layer.cornerRadius = 18
self.fillStackView()
}

private func fillStackView() {
switch type {
case .default:
containerStackView.addArrangedSubviews(contentLabel)
case .actionButton:
containerStackView.addArrangedSubviews(toastIconImageView, contentLabel, linkButton)
default:
containerStackView.addArrangedSubviews(toastIconImageView, contentLabel)
}
}

private func setLayout() {
addSubview(containerStackView)

toastIconImageView.snp.makeConstraints { make in
make.width.height.equalTo(20)
}

linkButton.snp.contentHuggingHorizontalPriority = 999

containerStackView.snp.makeConstraints { make in
make.center.equalToSuperview()
make.leading.trailing.equalToSuperview().inset(16)
make.top.bottom.equalToSuperview().inset(14)
}
}

private func setLinkButtonAction() {
self.linkButton
.publisher(for: .touchUpInside)
.sink { [weak self] _ in
self?.linkButtonAction?()
}.store(in: cancelBag)
}
}

public extension MDSToast {
enum ToastType {
case `default`
case success
case alert
case error
case actionButton

var icon: UIImage {
switch self {
case .alert:
return DSKitAsset.Assets.toastAlert.image
default: // TODO: MDS 피그마 export 권한 생기면 추가 예정
return DSKitAsset.Assets.toastAlert.image
}
}
}
}

0 comments on commit f36304d

Please sign in to comment.