Skip to content

Commit

Permalink
Merge pull request #55 from charxene/use-date
Browse files Browse the repository at this point in the history
Use Decoded Dates, supports latest DiscordKit changes
  • Loading branch information
cryptoAlgorithm authored Jun 2, 2022
2 parents 7c4e390 + e8c41dc commit c3839d9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 103 deletions.
6 changes: 2 additions & 4 deletions Swiftcord.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
DA32EF3427C6861800A9ED72 /* StickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF3327C6861800A9ED72 /* StickerView.swift */; };
DA32EF3927C77E3300A9ED72 /* AttachmentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF3827C77E3300A9ED72 /* AttachmentView.swift */; };
DA32EF3F27C7C1D000A9ED72 /* MessageInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF3E27C7C1D000A9ED72 /* MessageInputView.swift */; };
DA32EF4627C8AB6200A9ED72 /* ISOTimestamp+.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF4527C8AB6200A9ED72 /* ISOTimestamp+.swift */; };
DA32EF4827C8ABFF00A9ED72 /* Date+.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF4727C8ABFF00A9ED72 /* Date+.swift */; };
DA32EF4A27C8BE8F00A9ED72 /* Color+.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF4927C8BE8F00A9ED72 /* Color+.swift */; };
DA32EF4C27C8BF5000A9ED72 /* TagCloudView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA32EF4B27C8BF5000A9ED72 /* TagCloudView.swift */; };
Expand Down Expand Up @@ -92,6 +91,7 @@
36004E1C283D63E500F0BA73 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = "<group>"; };
36367144283C19E500A5CBE6 /* AVKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVKit.framework; path = System/Library/Frameworks/AVKit.framework; sourceTree = SDKROOT; };
3684BB5A283C69C5005045AE /* Sparkle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sparkle.swift; sourceTree = "<group>"; };
D848802D28485147000D1A77 /* DiscordKit */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = DiscordKit; path = ../DiscordKit; sourceTree = "<group>"; };
DA23843827CB934D009E15E0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
DA2384A027CB9714009E15E0 /* Font+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Font+.swift"; sourceTree = "<group>"; };
DA2384B827CBC2CE009E15E0 /* GintoBold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = GintoBold.otf; sourceTree = "<group>"; };
Expand All @@ -110,7 +110,6 @@
DA32EF3327C6861800A9ED72 /* StickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerView.swift; sourceTree = "<group>"; };
DA32EF3827C77E3300A9ED72 /* AttachmentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentView.swift; sourceTree = "<group>"; };
DA32EF3E27C7C1D000A9ED72 /* MessageInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageInputView.swift; sourceTree = "<group>"; };
DA32EF4527C8AB6200A9ED72 /* ISOTimestamp+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ISOTimestamp+.swift"; sourceTree = "<group>"; };
DA32EF4727C8ABFF00A9ED72 /* Date+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+.swift"; sourceTree = "<group>"; };
DA32EF4927C8BE8F00A9ED72 /* Color+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+.swift"; sourceTree = "<group>"; };
DA32EF4B27C8BF5000A9ED72 /* TagCloudView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagCloudView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +250,7 @@
DA4A887B27C0AF3000720909 = {
isa = PBXGroup;
children = (
D848802D28485147000D1A77 /* DiscordKit */,
36004E1C283D63E500F0BA73 /* .swiftlint.yml */,
DA4A888627C0AF3000720909 /* Swiftcord */,
DA4A891727C4B0DF00720909 /* README.md */,
Expand Down Expand Up @@ -366,7 +366,6 @@
DAAFB5CB282B879200807B54 /* Double+.swift */,
DA2384A027CB9714009E15E0 /* Font+.swift */,
DA520ADC27D643CE009FD740 /* Int+.swift */,
DA32EF4527C8AB6200A9ED72 /* ISOTimestamp+.swift */,
DA28027E28095E3000B14E5C /* Logger+.swift */,
E7AF1C35282FC2E8001F78DF /* NSTextView+.swift */,
DA520ACA27D4A23A009FD740 /* String+.swift */,
Expand Down Expand Up @@ -671,7 +670,6 @@
DA32EF4827C8ABFF00A9ED72 /* Date+.swift in Sources */,
DA54D5782844DA1400B11857 /* UserSettingsProfileView.swift in Sources */,
DA4A892427C5D6C500720909 /* ServerView.swift in Sources */,
DA32EF4627C8AB6200A9ED72 /* ISOTimestamp+.swift in Sources */,
DA4A888827C0AF3000720909 /* SwiftcordApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
{
"object": {
"pins": [
{
"package": "Lottie",
"repositoryURL": "https://github.com/airbnb/lottie-ios.git",
"state": {
"branch": null,
"revision": "4a6058cbbdfe4f74aeae92c8bd51ad3b0de2a1ee",
"version": "3.3.0"
}
},
{
"package": "Reachability",
"repositoryURL": "https://github.com/ashleymills/Reachability.swift",
"state": {
"branch": "master",
"revision": "a81b7367f2c46875f29577e03a60c39cdfad0c8d",
"version": null
}
},
{
"package": "swiftui-cached-async-image",
"repositoryURL": "https://github.com/lorenzofiamingo/swiftui-cached-async-image",
"state": {
"branch": null,
"revision": "eeb1565d780d1b75d045e21b5ca2a1e3650b0fc2",
"version": "2.1.0"
}
"pins" : [
{
"identity" : "lottie-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/airbnb/lottie-ios.git",
"state" : {
"revision" : "4a6058cbbdfe4f74aeae92c8bd51ad3b0de2a1ee",
"version" : "3.3.0"
}
]
},
"version": 1
},
{
"identity" : "reachability.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ashleymills/Reachability.swift",
"state" : {
"revision" : "c01bbdf2d633cf049ae1ed1a68a2020a8bda32e2",
"version" : "5.1.0"
}
},
{
"identity" : "sparkle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sparkle-project/Sparkle",
"state" : {
"revision" : "286edd1fa22505a9e54d170e9fd07d775ea233f2",
"version" : "2.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"version" : "1.0.0"
}
},
{
"identity" : "swiftui-cached-async-image",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lorenzofiamingo/swiftui-cached-async-image",
"state" : {
"revision" : "eeb1565d780d1b75d045e21b5ca2a1e3650b0fc2",
"version" : "2.1.0"
}
}
],
"version" : 2
}
25 changes: 0 additions & 25 deletions Swiftcord/Utils/Extensions/Date+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@

import Foundation

// DateFormatters are heavy, so just cache one here
// There's probably a better way to do this lol
let commonDateFormatter = DateFormatter()

extension Date {
func toTimeString(with fmt: String = "hh:mm a") -> String {
commonDateFormatter.setLocalizedDateFormatFromTemplate(fmt)
return commonDateFormatter.string(from: self)
}

func toDateString(with fmt: String = "dd/MM/yy") -> String {
commonDateFormatter.setLocalizedDateFormatFromTemplate(fmt)
return commonDateFormatter.string(from: self)
}
func toDateString(with style: DateFormatter.Style) -> String {
commonDateFormatter.dateStyle = style
return commonDateFormatter.string(from: self)
}
}

extension Date {
static func - (lhs: Date, rhs: Date) -> TimeInterval {
return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}
}

extension Date {
func isSameDay(as date: Date) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Swiftcord/Utils/Extensions/DiscordAPI/Message+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ extension Message {
return prev.author.id == self.author.id
&& (prev.type == .defaultMsg || prev.type == .reply)
&& self.type == .defaultMsg
&& (((self.timestamp.toDate() ?? Date()) - (prev.timestamp.toDate() ?? Date())) < 400)
&& (self.timestamp.timeIntervalSince(prev.timestamp) < 400)
}
}
27 changes: 0 additions & 27 deletions Swiftcord/Utils/Extensions/ISOTimestamp+.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Swiftcord/Views/Message/EmbedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct EmbedView: View {
.font(.system(size: 12, weight: .semibold))
.textSelection(.enabled)

Text(timestamp.toDate(hasFractionalSeconds: false)?.toDateString() ?? "")
Text(timestamp, style: .date)
.font(.system(size: 12, weight: .semibold))
.textSelection(.enabled)
}
Expand Down
13 changes: 8 additions & 5 deletions Swiftcord/Views/Message/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ struct MessageView: View, Equatable {
spacing: 16
) {
// Would have loved to use switch-case but fallthrough doesn't work :(
let timestring = message.timestamp.toDate()?.toTimeString() ?? ""
if message.type == .reply || message.type == .defaultMsg {
if !shrunk {
UserAvatarView(user: message.author, guildID: serverCtx.guild!.id, webhookID: message.webhook_id, clickDisabled: false)
} else {
Text(timestring)
Text(message.timestamp, style: .time)
.font(.system(size: 8, weight: .semibold, design: .monospaced))
.frame(width: 40, height: 22, alignment: .center)
.animation(.linear(duration: 0.1), value: hovered)
Expand All @@ -86,9 +85,13 @@ struct MessageView: View, Equatable {
isWebhook: message.webhook_id != nil
)
}
Text(timestring + (message.edited_timestamp != nil ? " • Edited: \(message.edited_timestamp!.toDate()?.toTimeString() ?? "")" : ""))
.font(.system(size: 12))
.opacity(0.5)
HStack {
Text(message.timestamp, style: .time)
if let edited_timestamp = message.edited_timestamp {
Text("• Edited:")
Text(edited_timestamp, style: .time)
}
}
}
}
// For including additional message components
Expand Down
13 changes: 4 additions & 9 deletions Swiftcord/Views/Message/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,20 @@ struct MessagesViewHeader: View {
}
}

struct DayDividerView: View, Equatable {
struct DayDividerView: View {
let date: Date

var body: some View {
ZStack {
Divider()
Text(date.toDateString(with: .medium))
Text(date, style: .date)
.font(.headline)
.opacity(0.7)
.padding(.horizontal, 4)
.background(.background)
}
.padding([.top, .horizontal], 16)
}

static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.date.timeIntervalSinceReferenceDate == rhs.date.timeIntervalSinceReferenceDate
}
}

struct MessagesView: View, Equatable {
Expand Down Expand Up @@ -132,9 +128,8 @@ struct MessagesView: View, Equatable {
ForEach(Array(messages.enumerated()), id: \.1.id) { (idx, msg) in
VStack(spacing: 0) {
if (idx == messages.count - 1 && reachedTop) ||
(idx != messages.count - 1 && msg.timestamp.prefix(10) != messages[idx+1].timestamp.prefix(10)),
let newDate = msg.timestamp.toDate() {
DayDividerView(date: newDate)
(idx != messages.count - 1 && !msg.timestamp.isSameDay(as: messages[idx+1].timestamp)) {
DayDividerView(date: msg.timestamp)
}

MessageView(
Expand Down

0 comments on commit c3839d9

Please sign in to comment.