Skip to content

Commit

Permalink
refactor: rename several files and update location (#62)
Browse files Browse the repository at this point in the history
* fix: fine-tune landscape view sizes

* refactor: rename Cell prefix to MessageView for message kinds

* refactor: update location of several files

* Update ChatView.swift

* Update ChatView.swift

* chore: remove ViewBuilder [skip ci]
  • Loading branch information
EnesKaraosman authored Apr 13, 2024
1 parent ccff273 commit c5b3abc
Show file tree
Hide file tree
Showing 51 changed files with 194 additions and 181 deletions.
2 changes: 1 addition & 1 deletion Example/Example/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension Color {

let futuraFont = Font.custom("Futura", size: 17)

internal extension ChatMessageCellStyle {
extension ChatMessageCellStyle {

static let basicStyle = ChatMessageCellStyle(
incomingTextStyle: .init(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//
// MessageCell.swift
// SwiftyChatbot
// ChatMessageViewContainer.swift
//
// Created by Enes Karaosman on 18.05.2020.
// Copyright © 2020 All rights reserved.
//

import SwiftUI

internal struct ChatMessageCellContainer<Message: ChatMessage>: View {
struct ChatMessageViewContainer<Message: ChatMessage>: View {

let message: Message
let size: CGSize
Expand All @@ -21,65 +20,65 @@ internal struct ChatMessageCellContainer<Message: ChatMessage>: View {
switch message.messageKind {

case .text(let text):
TextCell(
TextMessageView(
text: text,
message: message,
size: size
)

case .location(let location):
LocationCell(
LocationMessageView(
location: location,
message: message,
size: size
)

case .imageText(let imageLoadingType, let text):
ImageTextCell(
ImageTextMessageView(
message: message,
imageLoadingType: imageLoadingType,
text: text,
size: size
)

case .image(let imageLoadingType):
ImageCell(
ImageMessageView(
message: message,
imageLoadingType: imageLoadingType,
size: size
)

case .contact(let contact):
ContactCell(
ContactMessageView(
contact: contact,
message: message,
size: size,
footerSection: contactFooterSection
)

case .quickReply(let quickReplies):
QuickReplyCell(
QuickReplyMessageView(
quickReplies: quickReplies,
quickReplySelected: onQuickReplyItemSelected
)

case .carousel(let carouselItems):
CarouselCell(
CarouselMessageView(
carouselItems: carouselItems,
size: size,
message: message,
onCarouselItemAction: onCarouselItemAction
)

case .video(let videoItem):
VideoPlaceholderCell(
VideoMessageView(
media: videoItem,
message: message,
size: size
)

case .loading:
LoadingCell(message: message, size: size)
LoadingMessageView(message: message, size: size)

case .custom(let custom):
if let cell = customCell {
Expand Down
17 changes: 8 additions & 9 deletions Sources/SwiftyChat/ChatView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// ChatView.swift
// SwiftyChatbot
//
// Created by Enes Karaosman on 19.05.2020.
// Copyright © 2020 All rights reserved.
Expand Down Expand Up @@ -50,7 +49,7 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
.dismissKeyboardOnTappingOutside() // iOS only
}

@ViewBuilder private func chatView(in geometry: GeometryProxy) -> some View {
private func chatView(in geometry: GeometryProxy) -> some View {
ScrollViewReader { proxy in
ScrollView(.vertical, showsIndicators: false) {
LazyVStack {
Expand Down Expand Up @@ -80,7 +79,7 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
alignment: message.isSender ? .trailing: .leading
)
}
chatMessageCellContainer(in: geometry.size, with: message, with: shouldShowDisplayName)
chatMessageViewContainer(in: geometry.size, with: message, with: shouldShowDisplayName)
.id(message.id)
.onAppear {
if message.id == self.messages.first?.id {
Expand Down Expand Up @@ -128,14 +127,14 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {

}

internal extension ChatView {
private extension ChatView {
// MARK: - List Item
private func chatMessageCellContainer(
private func chatMessageViewContainer(
in size: CGSize,
with message: Message,
with avatarShow: Bool
) -> some View {
ChatMessageCellContainer(
ChatMessageViewContainer(
message: message,
size: size,
customCell: customCellView,
Expand All @@ -153,13 +152,13 @@ internal extension ChatView {
)
)
)
.modifier(MessageHorizontalSpaceModifier(messageKind: message.messageKind, isSender: message.isSender))
.modifier(CellEdgeInsetsModifier(isSender: message.isSender))
.modifier(MessageHorizontalAlignmentModifier(messageKind: message.messageKind, isSender: message.isSender))
.modifier(MessageViewEdgeInsetsModifier(isSender: message.isSender))
.id(message.id)
}
}

public extension ChatView {
private extension ChatView {
func shouldShowDateHeader(messages: [Message], thisMessage: Message) -> Bool {
if let messageIndex = messages.firstIndex(where: { $0.id == thisMessage.id }) {
if messageIndex == 0 { return true }
Expand Down
35 changes: 0 additions & 35 deletions Sources/SwiftyChat/Common/Modifiers/EmojiModifier.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// MessageKind.swift
// SwiftyChatbot
//
// Created by Enes Karaosman on 18.05.2020.
// Copyright © 2020 All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

internal extension Date {
extension Date {
static func - (lhs: Date, rhs: Date) -> TimeInterval {
lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

internal extension Double {
extension Double {

private static var timeHMSFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
Expand Down
36 changes: 36 additions & 0 deletions Sources/SwiftyChat/Core/Extension/String++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// String++.swift
//
// Created by Enes Karaosman on 19.05.2020.
// Copyright © 2020 All rights reserved.
//

/// Emoji helper
extension Character {
/// A simple emoji is one scalar and presented to the user as an Emoji
private var isSimpleEmoji: Bool {
guard let firstScalar = unicodeScalars.first else { return false }
return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
}

/// Checks if the scalars will be merged into an emoji
private var isCombinedIntoEmoji: Bool {
unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false
}

fileprivate var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
}

extension String {
var containsOnlyEmoji: Bool { !isEmpty && !contains { !$0.isEmoji } }

private var isSingleEmoji: Bool { count == 1 && containsEmoji }

private var containsEmoji: Bool { contains { $0.isEmoji } }

private var emojiString: String { emojis.map { String($0) }.reduce("", +) }

private var emojis: [Character] { filter { $0.isEmoji } }

private var emojiScalars: [UnicodeScalar] { filter { $0.isEmoji }.flatMap { $0.unicodeScalars } }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// UIDevice++.swift
// SwiftyChatbot
//
// Created by Enes Karaosman on 23.05.2020.
// Copyright © 2020 All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import SwiftUI

internal struct CarouselItemButtonStyle: ButtonStyle {
struct CarouselItemButtonStyle: ButtonStyle {

let backgroundColor: Color

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// ChatMessage.swift
// SwiftyChatbot
//
// Created by Enes Karaosman on 19.05.2020.
// Copyright © 2020 All rights reserved.
Expand All @@ -11,6 +10,7 @@ import Foundation
public protocol ChatMessage: Identifiable {

associatedtype User: ChatUser

/// The `User` who sent this message.
var user: User { get }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// User.swift
// SwiftyChatbot
// ChatUser.swift
//
// Created by Enes Karaosman on 19.05.2020.
// Copyright © 2020 All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Combine
import SwiftUI
import SwiftUIEKtensions

internal extension CGSize {
private extension CGSize {
var midX: CGFloat { width / 2 }
var midY: CGFloat { height / 2 }
}

internal struct PIPVideoCell<Message: ChatMessage>: View {
struct PIPVideoCell<Message: ChatMessage>: View {

@EnvironmentObject var videoManager: VideoManager<Message>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI
import Kingfisher

internal struct AvatarModifier<Message: ChatMessage>: ViewModifier {
struct AvatarModifier<Message: ChatMessage>: ViewModifier {

let message: Message
let showAvatarForMessage: Bool
Expand Down Expand Up @@ -79,10 +79,11 @@ internal struct AvatarModifier<Message: ChatMessage>: ViewModifier {
}

private var blankAvatar: some View {
return Spacer()
EmptyView()
}

@ViewBuilder private var avatarImage: some View {
@ViewBuilder
private var avatarImage: some View {
if !showAvatarForMessage {
blankAvatar
} else if let imageURL = user.avatarURL, currentStyle.imageStyle.imageSize.width > 0 {
Expand All @@ -92,7 +93,8 @@ internal struct AvatarModifier<Message: ChatMessage>: ViewModifier {
}
}

@ViewBuilder private var positionedAvatar: some View {
@ViewBuilder
private var positionedAvatar: some View {
switch currentAvatarPosition {
case .alignToMessageTop: alignToMessageTop
case .alignToMessageCenter: alignToMessageCenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import SwiftUI

internal extension View {
extension View {
func dismissKeyboardOnTappingOutside() -> some View {
return ModifiedContent(content: self, modifier: DismissKeyboardOnTappingOutside())
modifier(DismissKeyboardOnTappingOutside())
}
}

internal struct DismissKeyboardOnTappingOutside: ViewModifier {
struct DismissKeyboardOnTappingOutside: ViewModifier {
func body(content: Content) -> some View {
content
.onTapGesture {
Expand Down
32 changes: 32 additions & 0 deletions Sources/SwiftyChat/Core/ViewModifiers/EmojiModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// EmojiModifier.swift
//
// Created by Enes Karaosman on 22.05.2020.
// Copyright © 2020 All rights reserved.
//

import SwiftUI

/// Modifies text font if contains emoji
struct EmojiModifier: ViewModifier {

let text: String
let defaultFont: Font

private var font: Font {
if text.containsOnlyEmoji {
switch text.count {
case 1: return .system(size: 50)
case 2: return .system(size: 38)
case 3: return .system(size: 25)
default: return defaultFont
}
}

return defaultFont
}

func body(content: Content) -> some View {
content.font(font)
}
}
Loading

0 comments on commit c5b3abc

Please sign in to comment.