Skip to content

Commit

Permalink
refactor: move mock related files to newly created SwiftyChatMock pac…
Browse files Browse the repository at this point in the history
…kage (#63)

* refactor: move mock related files into SwiftyChatMock target

* Update README.md

* Update Lorem.swift

* Update MessageMocker.swift

* Update AdvancedExampleView.swift
  • Loading branch information
EnesKaraosman authored Apr 13, 2024
1 parent c5b3abc commit f9fced1
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 186 deletions.
20 changes: 14 additions & 6 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
94410B752BC6B4D400C37BDB /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94410B742BC6B4D400C37BDB /* ExampleApp.swift */; };
94410B792BC6B4D700C37BDB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 94410B782BC6B4D700C37BDB /* Assets.xcassets */; };
94410B7C2BC6B4D700C37BDB /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 94410B7B2BC6B4D700C37BDB /* Preview Assets.xcassets */; };
94410B842BC6B50700C37BDB /* SwiftyChat in Frameworks */ = {isa = PBXBuildFile; productRef = 94410B832BC6B50700C37BDB /* SwiftyChat */; };
94410B862BC6B53900C37BDB /* ChatListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94410B852BC6B53900C37BDB /* ChatListView.swift */; };
94410B882BC6B53E00C37BDB /* AdvancedExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94410B872BC6B53E00C37BDB /* AdvancedExampleView.swift */; };
94410B8A2BC6B54100C37BDB /* BasicExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94410B892BC6B54100C37BDB /* BasicExampleView.swift */; };
94410B8C2BC6B54900C37BDB /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94410B8B2BC6B54900C37BDB /* Extensions.swift */; };
9498B4232BCAADBB0021FB93 /* SwiftyChat in Frameworks */ = {isa = PBXBuildFile; productRef = 9498B4222BCAADBB0021FB93 /* SwiftyChat */; };
9498B4252BCAADBB0021FB93 /* SwiftyChatMock in Frameworks */ = {isa = PBXBuildFile; productRef = 9498B4242BCAADBB0021FB93 /* SwiftyChatMock */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -34,7 +35,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
94410B842BC6B50700C37BDB /* SwiftyChat in Frameworks */,
9498B4252BCAADBB0021FB93 /* SwiftyChatMock in Frameworks */,
9498B4232BCAADBB0021FB93 /* SwiftyChat in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -46,6 +48,7 @@
children = (
94410B732BC6B4D400C37BDB /* Example */,
94410B722BC6B4D400C37BDB /* Products */,
9498B4192BC982FF0021FB93 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -97,7 +100,8 @@
);
name = Example;
packageProductDependencies = (
94410B832BC6B50700C37BDB /* SwiftyChat */,
9498B4222BCAADBB0021FB93 /* SwiftyChat */,
9498B4242BCAADBB0021FB93 /* SwiftyChatMock */,
);
productName = Example;
productReference = 94410B712BC6B4D400C37BDB /* Example.app */;
Expand Down Expand Up @@ -128,7 +132,7 @@
);
mainGroup = 94410B682BC6B4D400C37BDB;
packageReferences = (
94410B822BC6B50700C37BDB /* XCLocalSwiftPackageReference ".." */,
9498B4212BCAADBB0021FB93 /* XCLocalSwiftPackageReference ".." */,
);
productRefGroup = 94410B722BC6B4D400C37BDB /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -374,17 +378,21 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
94410B822BC6B50700C37BDB /* XCLocalSwiftPackageReference ".." */ = {
9498B4212BCAADBB0021FB93 /* XCLocalSwiftPackageReference ".." */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ..;
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
94410B832BC6B50700C37BDB /* SwiftyChat */ = {
9498B4222BCAADBB0021FB93 /* SwiftyChat */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftyChat;
};
9498B4242BCAADBB0021FB93 /* SwiftyChatMock */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftyChatMock;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 94410B692BC6B4D400C37BDB /* Project object */;
Expand Down
18 changes: 9 additions & 9 deletions Example/Example/AdvancedExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import SwiftUI
import SwiftyChat
import SwiftyChatMock

struct AdvancedExampleView: View {

@State var messages: [MockMessages.ChatMessageItem] = []
@State var messages: [MessageMocker.ChatMessageItem] = []
@State private var scrollToBottom = false

@State private var message = ""
Expand All @@ -20,13 +21,13 @@ struct AdvancedExampleView: View {
}

private var chatView: some View {
ChatView<MockMessages.ChatMessageItem, MockMessages.ChatUserItem>(messages: $messages) {
ChatView<MessageMocker.ChatMessageItem, MessageMocker.ChatUserItem>(messages: $messages, scrollToBottom: $scrollToBottom) {
BasicInputView(
message: $message,
placeholder: "Type something",
onCommit: { messageKind in
self.messages.append(
.init(user: MockMessages.sender, messageKind: messageKind, isSender: true)
.init(user: MessageMocker.sender, messageKind: messageKind, isSender: true)
)
}
)
Expand Down Expand Up @@ -59,8 +60,8 @@ struct AdvancedExampleView: View {
// ▼ Implement in case ChatMessageKind.quickReply
.onQuickReplyItemSelected { (quickReply) in
self.messages.append(
MockMessages.ChatMessageItem(
user: MockMessages.sender,
MessageMocker.ChatMessageItem(
user: MessageMocker.sender,
messageKind: .text(quickReply.title),
isSender: true
)
Expand All @@ -83,20 +84,19 @@ struct AdvancedExampleView: View {
#if os(iOS)
.navigationBarTitle("Advanced")
#endif
.listStyle(PlainListStyle())
.task {
if let portraitUrl = URL(string: "https://picsum.photos/id/\(Int.random(in: 1...100))/400/600") {
self.messages.append(.init(user: MockMessages.chatbot, messageKind: .image(.remote(portraitUrl))))
self.messages.append(.init(user: MessageMocker.chatbot, messageKind: .image(.remote(portraitUrl))))
}

self.messages.append(
.init(
user: MockMessages.chatbot,
user: MessageMocker.chatbot,
messageKind: .text("https://github.com/EnesKaraosman/SwiftyChat and here is his phone +90 537 844 11-41, & mail: [email protected] Today is 27 May 2020")
)
)

self.messages.append(contentsOf: MockMessages.generatedMessages(count: 53))
self.messages.append(contentsOf: MessageMocker.generate(count: 53))
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions Example/Example/BasicExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import SwiftUI
import SwiftyChat
import SwiftyChatMock

struct BasicExampleView: View {

@State var messages: [MockMessages.ChatMessageItem] = MockMessages.generateMessage(kind: .Text, count: 20)
@State var messages: [MessageMocker.ChatMessageItem] = MessageMocker.generate(kind: .text, count: 20)

@State private var message = ""

Expand All @@ -19,14 +20,14 @@ struct BasicExampleView: View {
}

private var chatView: some View {
ChatView<MockMessages.ChatMessageItem, MockMessages.ChatUserItem>(messages: $messages) {
ChatView<MessageMocker.ChatMessageItem, MessageMocker.ChatUserItem>(messages: $messages) {

BasicInputView(
message: $message,
placeholder: "Type something",
onCommit: { messageKind in
self.messages.append(
.init(user: MockMessages.sender, messageKind: messageKind, isSender: true)
.init(user: MessageMocker.sender, messageKind: messageKind, isSender: true)
)
}
)
Expand Down
16 changes: 12 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ let package = Package(
products: [
.library(
name: "SwiftyChat",
targets: ["SwiftyChat"]),
targets: ["SwiftyChat"]
),
.library(
name: "SwiftyChatMock",
targets: ["SwiftyChatMock"]
),
],
dependencies: [
// Image downloading library
.package(url: "https://github.com/onevcat/Kingfisher.git", from: "7.11.0"),
.package(url: "https://github.com/EnesKaraosman/SwiftUIEKtensions.git", from: "0.4.0"),
.package(url: "https://github.com/dkk/WrappingHStack.git", from: "2.2.11")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftyChat",
dependencies: [
Expand All @@ -32,6 +34,12 @@ let package = Package(

],
exclude: ["Demo/Preview"]
),
.target(
name: "SwiftyChatMock",
dependencies: [
"SwiftyChat"
]
)
],
swiftLanguageVersions: [.v5]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Here below is minimum code required to get started (see up & running)<br>
For detail, visit example project [here](../master/SwiftyChatExample/Example)

```swift
import SwiftyChat
import SwiftyChatMock

@State private var scrollToBottom = false
@State private var messages: [MockMessages.ChatMessageItem] = [] // for quick test assign MockMessages.generatedMessages()

Expand Down
9 changes: 0 additions & 9 deletions Sources/SwiftyChat/MessageViews/LoadingMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,3 @@ struct LoadingMessageView<Message: ChatMessage>: View {
)
}
}

#if DEBUG
struct Loadingcell_Previews: PreviewProvider {
static var previews: some View {
LoadingMessageView(message: MockMessages.generateMessage(kind: .Text), size: .zero)
.environmentObject(ChatMessageCellStyle.init())
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import Foundation

// MARK: - Lorem

public class Lorem {
class Lorem {
// MARK: Public

/// Return a random word.
///
/// - returns: Returns a random word.
public class func word() -> String {
class func word() -> String {
wordList.randomElement()!
}

Expand All @@ -41,7 +41,7 @@ public class Lorem {
/// - parameter count: The number of words to return.
///
/// - returns: Returns an array of `count` words.
public class func words(nbWords: Int = 3) -> [String] {
class func words(nbWords: Int = 3) -> [String] {
(1...nbWords).map { _ in
word()
}
Expand All @@ -52,7 +52,7 @@ public class Lorem {
/// - parameter count: The number of words the string should contain.
///
/// - returns: Returns a string of `count` words.
public class func words(nbWords: Int = 3) -> String {
class func words(nbWords: Int = 3) -> String {
words(nbWords: nbWords).joined(separator: " ")
}

Expand All @@ -61,7 +61,7 @@ public class Lorem {
/// - parameter variable: If `true`, the number of words will vary between
/// +/- 40% of `nbWords`.
/// - returns:
public class func sentence(nbWords: Int = 6, variable: Bool = true) -> String {
class func sentence(nbWords: Int = 6, variable: Bool = true) -> String {
if nbWords <= 0 {
return ""
}
Expand All @@ -75,7 +75,7 @@ public class Lorem {
/// - parameter nbSentences: The number of sentences to generate.
///
/// - returns: Returns an array of random sentences.
public class func sentences(nbSentences: Int = 3) -> [String] {
class func sentences(nbSentences: Int = 3) -> [String] {
(0 ..< nbSentences).map { _ in sentence() }
}

Expand All @@ -85,7 +85,7 @@ public class Lorem {
/// - parameter variable: If `true`, the number of sentences will vary
/// between +/- 40% of `nbSentences`.
/// - returns: Returns a paragraph with `nbSentences` random sentences.
public class func paragraph(nbSentences: Int = 3, variable: Bool = true) -> String {
class func paragraph(nbSentences: Int = 3, variable: Bool = true) -> String {
if nbSentences <= 0 {
return ""
}
Expand All @@ -97,22 +97,22 @@ public class Lorem {
/// Generate an array of random paragraphs.
/// - parameter nbParagraphs: The number of paragraphs to generate.
/// - returns: Returns an array of `nbParagraphs` paragraphs.
public class func paragraphs(nbParagraphs: Int = 3) -> [String] {
class func paragraphs(nbParagraphs: Int = 3) -> [String] {
(0 ..< nbParagraphs).map { _ in paragraph() }
}

/// Generate a string of random paragraphs.
/// - parameter nbParagraphs: The number of paragraphs to generate.
/// - returns: Returns a string of random paragraphs.
public class func paragraphs(nbParagraphs: Int = 3) -> String {
class func paragraphs(nbParagraphs: Int = 3) -> String {
paragraphs(nbParagraphs: nbParagraphs).joined(separator: "\n\n")
}

/// Generate a string of at most `maxNbChars` characters.
/// - parameter maxNbChars: The maximum number of characters the string
/// should contain.
/// - returns: Returns a string of at most `maxNbChars` characters.
public class func text(maxNbChars: Int = 200) -> String {
class func text(maxNbChars: Int = 200) -> String {
var result: [String] = []

if maxNbChars < 5 {
Expand Down Expand Up @@ -201,7 +201,7 @@ public class Lorem {
]
}

extension String {
private extension String {
var firstCapitalized: String {
var string = self
string.replaceSubrange(
Expand All @@ -213,8 +213,8 @@ extension String {
}
}

extension Int {
public func randomize(variation: Int) -> Int {
private extension Int {
func randomize(variation: Int) -> Int {
let randomInt = Int.random(in: (100 - variation)..<(100+variation))
let multiplier = Double(randomInt) / 100
let randomized = Double(self) * multiplier
Expand Down
Loading

0 comments on commit f9fced1

Please sign in to comment.