summaryrefslogtreecommitdiff
path: root/Juick/Helpers
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/Helpers')
-rw-r--r--Juick/Helpers/Data+Hex.swift16
-rw-r--r--Juick/Helpers/LoadableState.swift25
-rw-r--r--Juick/Helpers/NSAttributedString_Entities.h19
-rw-r--r--Juick/Helpers/NSAttributedString_Entities.m90
4 files changed, 150 insertions, 0 deletions
diff --git a/Juick/Helpers/Data+Hex.swift b/Juick/Helpers/Data+Hex.swift
new file mode 100644
index 0000000..69c170d
--- /dev/null
+++ b/Juick/Helpers/Data+Hex.swift
@@ -0,0 +1,16 @@
+//
+// Data+Hex.swift
+// Juick
+//
+// Created by Vitaly Takmazov on 16.09.2020.
+// Copyright © 2020 com.juick. All rights reserved.
+//
+
+import Foundation
+
+extension Data {
+ var hexString: String {
+ let hexString = map { String(format: "%02.2hhx", $0) }.joined()
+ return hexString
+ }
+}
diff --git a/Juick/Helpers/LoadableState.swift b/Juick/Helpers/LoadableState.swift
new file mode 100644
index 0000000..a45edb2
--- /dev/null
+++ b/Juick/Helpers/LoadableState.swift
@@ -0,0 +1,25 @@
+//
+// LoadableState.swift
+// tst
+//
+// Created by Vitaly Takmazov on 10.12.2019.
+// Copyright © 2019 com.juick. All rights reserved.
+//
+
+import Foundation
+
+enum LoadableState<T> {
+ case loading
+ case fetched(Result<T, FetchError>)
+}
+
+enum FetchError: Error {
+ case error(String)
+
+ var localizedDescription: String {
+ switch self {
+ case .error(let message):
+ return message
+ }
+ }
+}
diff --git a/Juick/Helpers/NSAttributedString_Entities.h b/Juick/Helpers/NSAttributedString_Entities.h
new file mode 100644
index 0000000..691d3d4
--- /dev/null
+++ b/Juick/Helpers/NSAttributedString_Entities.h
@@ -0,0 +1,19 @@
+//
+// NSAttributedString+NSAttributedString_Entities.h
+// Juick
+//
+// Created by Vitaly Takmazov on 23.09.2020.
+// Copyright © 2020 com.juick. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface NSAttributedString (Entities)
+
++(NSAttributedString *) attributedStringFromMessage:(Message *)message;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Juick/Helpers/NSAttributedString_Entities.m b/Juick/Helpers/NSAttributedString_Entities.m
new file mode 100644
index 0000000..3f7159b
--- /dev/null
+++ b/Juick/Helpers/NSAttributedString_Entities.m
@@ -0,0 +1,90 @@
+//
+// NSAttributedString+NSAttributedString.h
+// Juick
+//
+// Created by Vitaly Takmazov on 23.09.2020.
+// Copyright © 2020 com.juick. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@implementation NSAttributedString (Entities)
+
+NSMutableParagraphStyle *quoteStyle;
+UIFont *boldFont;
+UIFont *italicFont;
+
+__attribute__((constructor))
+static void initialize_fonts() {
+ quoteStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
+ quoteStyle.firstLineHeadIndent = 12.0f;
+ quoteStyle.headIndent = 12.0f;
+ quoteStyle.paragraphSpacing = 6.0f;
+ UIFontDescriptor* fontDescriptor = [UIFontDescriptor
+ preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
+ UIFontDescriptor* boldFontDescriptor = [fontDescriptor
+ fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
+ boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size: 0.0];
+ UIFontDescriptor* italicFontDescriptor = [fontDescriptor
+ fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
+ italicFont = [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0];
+}
+
++(NSAttributedString *) attributedStringFromMessage:(Message *)msg {
+ if (msg.text) {
+ NSMutableAttributedString *txt = [[NSMutableAttributedString alloc]
+ initWithString:msg.text
+ attributes:@{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody],
+ NSForegroundColorAttributeName:[UIColor colorNamed:@"Text"]
+ }];
+ [txt beginEditing];
+ for (Entity *entity in msg.entities) {
+ NSUInteger start = entity.start ? [entity.start unsignedIntegerValue] : 0;
+ NSUInteger end = entity.end ? [entity.end unsignedIntegerValue] : 0;
+ NSString *text = entity.text ? entity.text : @"";
+ NSRange currentRange = NSMakeRange(start, end - start);
+ [txt addAttribute:@"displayText" value:text range:currentRange];
+ if ([entity.type isEqualToString:@"a"]) {
+ [txt addAttribute:NSLinkAttributeName value:entity.link range:currentRange];
+ }
+ if ([entity.type isEqualToString:@"q"]) {
+ [txt addAttribute:NSForegroundColorAttributeName value:[UIColor colorNamed:@"Muted"] range:currentRange];
+ [txt addAttribute:NSParagraphStyleAttributeName value:quoteStyle range:currentRange];
+ }
+ if ([entity.type isEqualToString:@"u"]) {
+ [txt addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:currentRange];
+ }
+
+ if ([entity.type isEqualToString:@"b"]) {
+ [txt addAttribute:NSFontAttributeName value:boldFont range:currentRange];
+ }
+ if ([entity.type isEqualToString:@"i"]) {
+ [txt addAttribute:NSFontAttributeName value:italicFont range:currentRange];
+ }
+ }
+ [txt enumerateAttribute:@"displayText" inRange:NSMakeRange(0, [txt length]) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
+ if (value) {
+ [txt replaceCharactersInRange:range withString:value];
+ }
+ }];
+ if ([msg.tags count] > 0) {
+ NSString *tagsList = [NSString stringWithFormat:@"%@\n", [msg.tags componentsJoinedByString:@", "]];
+ [txt insertAttributedString:[[NSAttributedString alloc]
+ initWithString:tagsList
+ attributes:@{
+ NSFontAttributeName:italicFont,
+ NSForegroundColorAttributeName:[UIColor colorNamed:@"Muted"]
+ }] atIndex:0];
+ }
+ [txt endEditing];
+ return txt;
+ } else {
+ return [[NSAttributedString alloc] initWithString:@""];
+ }
+}
+
+@end
+
+NS_ASSUME_NONNULL_END