Skip to content

Commit

Permalink
Merge pull request #2607 from anyproto/ios-3774-file-attachment-add-size
Browse files Browse the repository at this point in the history
iOS-3774 Chat | File attachment - add size
  • Loading branch information
joe-pusya authored Jan 9, 2025
2 parents 2b3c32c + 700201e commit f778e04
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ struct MessageLinkLocalBinaryFileView: View {

let path: String
let type: UTType
let sizeInBytes: Int?
let onTapRemove: () -> Void

private var fileName: String { URL(fileURLWithPath: path).lastPathComponent }

var body: some View {
MessageLinkObjectView(
MessageLinkObjectContainerView(
icon: .object(.file(mimeType: type.identifier, name: fileName)),
title: fileName,
description: Loc.file,
size: size(),
onTapRemove: onTapRemove
)
}

private func size() -> String? {
let sizeInBytes = Int64(sizeInBytes ?? 0)
return sizeInBytes > 0 ? ByteCountFormatter.fileFormatter.string(fromByteCount: sizeInBytes) : nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct MessageLinkedLocalFile: View {
MessageLinkLocalImageView(contentsOfFile: fileData.path, onTapRemove: onTapRemove)
.id(fileData.path)
case .file:
MessageLinkLocalBinaryFileView(path: fileData.path, type: fileData.type, onTapRemove: onTapRemove)
MessageLinkLocalBinaryFileView(path: fileData.path, type: fileData.type, sizeInBytes: fileData.sizeInBytes, onTapRemove: onTapRemove)
case .video:
MessageInputVideoView(url: URL(fileURLWithPath: fileData.path), onTapRemove: onTapRemove)
.id(fileData.path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Foundation
import SwiftUI
import Services

struct MessageLinkBookmarkContainerView: View {

let icon: Icon
let title: String
let description: String
let onTapRemove: () -> Void

var body: some View {
MessageLinkBookmarkView(
icon: icon,
title: title,
description: description
)
.messageLinkObjectStyle()
.messageLinkRemoveButton(onTapRemove: onTapRemove)
}
}

struct MessageLinkBookmarkView: View {

let icon: Icon
let title: String
let description: String

var body: some View {
VStack(alignment: .leading, spacing: 2) {
HStack(spacing: 6) {
IconView(icon: icon)
.frame(width: 14, height: 14)
Text(title)
.anytypeStyle(.caption1Regular)
.foregroundColor(.Text.secondary)
.lineLimit(1)
.padding(.bottom, 5)
Spacer()
}
Text(description)
.anytypeStyle(.uxTitle2Medium)
.foregroundColor(.Text.primary)
.lineLimit(1)
}
.padding(12)
}
}

extension MessageLinkBookmarkContainerView {
init(details: MessageAttachmentDetails, onTapRemove: @escaping () -> Void) {
self = MessageLinkBookmarkContainerView(
icon: details.objectIconImage,
title: details.source ?? details.title,
description: details.title,
onTapRemove: onTapRemove
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ import Foundation
import SwiftUI
import Services

struct MessageLinkObjectView: View {
struct MessageLinkObjectContainerView: View {

let icon: Icon
let title: String
let description: String
let size: String?
let onTapRemove: () -> Void

var body: some View {
MessageLinkObjectView(
icon: icon,
title: title,
description: description,
size: size
)
.messageLinkObjectStyle()
.messageLinkRemoveButton(onTapRemove: onTapRemove)
}
}

struct MessageLinkObjectView: View {

let icon: Icon
let title: String
let description: String
let size: String?

var body: some View {
HStack(spacing: 12) {
IconView(icon: icon)
Expand All @@ -18,29 +38,36 @@ struct MessageLinkObjectView: View {
Text(title)
.anytypeStyle(.previewTitle2Medium)
.foregroundColor(.Text.primary)
Text(description)
.anytypeStyle(.relation3Regular)
.foregroundColor(.Text.secondary)
HStack(spacing: 6) {
Text(description)
.anytypeStyle(.relation3Regular)
.foregroundColor(.Text.secondary)
if let size {
Circle()
.fill(Color.Text.secondary)
.frame(width: 3, height: 3)
Text(size)
.anytypeStyle(.relation3Regular)
.foregroundColor(.Text.secondary)
}
}
}
.lineLimit(1)
Spacer()
}
.padding(12)
.frame(width: 216, height: 72)
.background(Color.Background.secondary)
.cornerRadius(12, style: .continuous)
.outerBorder(12, color: .Shape.tertiary, lineWidth: 1)
.shadow(color: .Additional.messageInputShadow, radius: 4)
.messageLinkRemoveButton(onTapRemove: onTapRemove)
}
}

extension MessageLinkObjectView {
extension MessageLinkObjectContainerView {
init(details: MessageAttachmentDetails, onTapRemove: @escaping (MessageAttachmentDetails) -> Void) {
self = MessageLinkObjectView(
let sizeInBytes = Int64(details.sizeInBytes ?? 0)
let size = sizeInBytes > 0 ? ByteCountFormatter.fileFormatter.string(fromByteCount: sizeInBytes) : nil
self = MessageLinkObjectContainerView(
icon: details.objectIconImage,
title: details.title,
description: details.description,
size: size,
onTapRemove: { onTapRemove(details) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ struct MessageLinkedUploadedObject: View {
.onTapGesture {
onTapObject()
}
case .bookmark:
MessageLinkBookmarkContainerView(details: details, onTapRemove: onTapRemove)
.onTapGesture {
onTapObject()
}
default:
MessageLinkObjectView(details: details, onTapRemove: { _ in
MessageLinkObjectContainerView(details: details, onTapRemove: { _ in
onTapRemove()
})
.onTapGesture {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ extension View {
func messageLinkShadow() -> some View {
shadow(color: .Additional.messageInputShadow, radius: 4)
}

func messageLinkObjectStyle() -> some View {
frame(width: 216, height: 72)
.background(Color.Background.secondary)
.cornerRadius(12, style: .continuous)
.outerBorder(12, color: .Shape.tertiary, lineWidth: 1)
.shadow(color: .Additional.messageInputShadow, radius: 4)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct MessageListAttachmentsViewContainer: View {
var body: some View {
VStack(spacing: 4) {
ForEach(objects, id: \.id) { details in
MessageObjectAttachmentView(details: details)
content(for: details)
.onTapGesture {
if !details.loadingState {
onTapObject(details)
Expand All @@ -22,4 +22,14 @@ struct MessageListAttachmentsViewContainer: View {
}
}
}

@ViewBuilder
private func content(for details: MessageAttachmentDetails) -> some View {
switch details.layoutValue {
case .bookmark:
MessageObjectBookmarkView(details: details)
default:
MessageObjectAttachmentView(details: details)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@ struct MessageObjectAttachmentView: View {
let icon: Icon
let title: String
let description: String
let size: String?

var body: some View {
HStack(spacing: 12) {
IconView(icon: icon)
.frame(width: 48, height: 48)

VStack(alignment: .leading, spacing: 2) {
Text(title)
.anytypeStyle(.previewTitle2Medium)
.foregroundColor(.Text.primary)
Text(description)
.anytypeStyle(.relation3Regular)
.foregroundColor(.Text.secondary)
}
.lineLimit(1)
Spacer()
}
.padding(12)
MessageLinkObjectView(
icon: icon,
title: title,
description: description,
size: size
)
.frame(height: 64)
.frame(minWidth: 231)
.background(Color.Background.secondary)
Expand All @@ -34,10 +25,13 @@ struct MessageObjectAttachmentView: View {

extension MessageObjectAttachmentView {
init(details: MessageAttachmentDetails) {
let sizeInBytes = Int64(details.sizeInBytes ?? 0)
let size = sizeInBytes > 0 ? ByteCountFormatter.fileFormatter.string(fromByteCount: sizeInBytes) : nil
self = MessageObjectAttachmentView(
icon: details.objectIconImage,
title: details.title,
description: details.description
description: details.description,
size: size
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import SwiftUI
import Services

struct MessageObjectBookmarkView: View {

let icon: Icon
let title: String
let description: String

var body: some View {
MessageLinkBookmarkView(
icon: icon,
title: title,
description: description
)
.frame(height: 64)
.frame(minWidth: 231)
.background(Color.Background.secondary)
.cornerRadius(18, style: .continuous)
}
}

extension MessageObjectBookmarkView {
init(details: MessageAttachmentDetails) {
self = MessageObjectBookmarkView(
icon: details.objectIconImage,
title: details.source ?? details.title,
description: details.title
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ struct MessageAttachmentDetails: Equatable, Identifiable, Hashable {
let id: String
let title: String
let description: String
let sizeInBytes: Int?
let layoutValue: DetailsLayout
let objectIconImage: Icon
let source: String?
let loadingState: Bool
}

extension MessageAttachmentDetails {
init(details: ObjectDetails) {
let source = details.source?.url.host() ?? details.source?.absoluteString
self = MessageAttachmentDetails(
id: details.id,
title: details.title,
description: details.objectType.name,
sizeInBytes: details.sizeInBytes,
layoutValue: details.layoutValue,
objectIconImage: details.objectIconImage,
source: source,
loadingState: false
)
}
Expand All @@ -27,8 +32,10 @@ extension MessageAttachmentDetails {
id: tagetId,
title: "Placeholder",
description: "Placeholder",
sizeInBytes: nil,
layoutValue: .basic,
objectIconImage: .object(.empty(.page)),
source: nil,
loadingState: true
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ final class ShareOptionsInteractor: ShareOptionsInteractorProtocol {
}

private func createFileObject(url: URL, spaceId: String) async throws -> FileDetails {
let data = FileData(path: url.relativePath, type: .data, isTemporary: false)
let resources = try url.resourceValues(forKeys: [.fileSizeKey])
let data = FileData(path: url.relativePath, type: .data, sizeInBytes: resources.fileSize, isTemporary: false)
let details = try await fileService.uploadFileObject(spaceId: spaceId, data: data, origin: .sharingExtension)

AnytypeAnalytics.instance().logCreateObject(
Expand Down Expand Up @@ -169,9 +170,10 @@ final class ShareOptionsInteractor: ShareOptionsInteractorProtocol {

private func createFileBlock(fileURL: URL, addToObject: ObjectDetails, logAnalytics: Bool) async throws {
let lastBlockInDocument = try await blockService.lastBlockId(from: addToObject.id, spaceId: addToObject.spaceId)
let resources = try fileURL.resourceValues(forKeys: [.fileSizeKey])
let fileDetails = try await fileService.uploadFileObject(
spaceId: addToObject.spaceId,
data: FileData(path: fileURL.relativePath, type: .data, isTemporary: false),
data: FileData(path: fileURL.relativePath, type: .data, sizeInBytes: resources.fileSize, isTemporary: false),
origin: .sharingExtension
)
let blockInformation = BlockInformation.file(fileDetails: fileDetails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class BlockBookmarkInfoView: UIView {
descriptionView.setText(payload.subtitle)
descriptionView.isHidden = payload.subtitle.isEmpty

urlView.setText(payload.source?.absoluteString ?? "")
urlView.setText(payload.source?.url.host() ?? "")

layoutUsing.stack {
$0.edgesToSuperview()
Expand Down
Loading

0 comments on commit f778e04

Please sign in to comment.