Skip to content

Commit 9d8c9e8

Browse files
authored
feature/update-image-string-urls (#136)
Применил ко всем ссылкам кодировку urlQueryAllowed
1 parent ecf8f1e commit 9d8c9e8

File tree

9 files changed

+30
-13
lines changed

9 files changed

+30
-13
lines changed

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/DialogResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct DialogResponse: Codable, Identifiable {
4646

4747
public extension DialogResponse {
4848
var anotherUserImageURL: URL? {
49-
.init(string: anotherUserImageStringURL.valueOrEmpty)
49+
anotherUserImageStringURL.queryAllowedURL
5050
}
5151

5252
var lastMessageFormatted: String {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/EventResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public extension EventResponse {
173173
}
174174

175175
var previewImageURL: URL? {
176-
.init(string: previewImageStringURL.valueOrEmpty)
176+
previewImageStringURL.queryAllowedURL
177177
}
178178

179179
var eventDateString: String {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/Journal/JournalEntryResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public struct JournalEntryResponse: Codable, Identifiable {
4040

4141
public extension JournalEntryResponse {
4242
var imageURL: URL? {
43-
.init(string: authorImage.valueOrEmpty)
43+
authorImage.queryAllowedURL
4444
}
4545

4646
var formattedMessage: String {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/Journal/JournalResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct JournalResponse: Codable, Identifiable, Equatable {
5454

5555
public extension JournalResponse {
5656
var imageURL: URL? {
57-
.init(string: lastMessageImage.valueOrEmpty)
57+
lastMessageImage.queryAllowedURL
5858
}
5959

6060
var title: String {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/MessageResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct MessageResponse: Codable, Identifiable, Hashable {
1616

1717
public extension MessageResponse {
1818
var imageURL: URL? {
19-
.init(string: imageStringURL.valueOrEmpty)
19+
imageStringURL.queryAllowedURL
2020
}
2121

2222
var formattedMessage: String {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/SportsGround.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class SportsGround: NSObject, Codable, MKAnnotation, Identifiable {
6666
}
6767

6868
public var previewImageURL: URL? {
69-
.init(string: preview.valueOrEmpty)
69+
preview.queryAllowedURL
7070
}
7171

7272
public enum CodingKeys: String, CodingKey {
@@ -158,7 +158,7 @@ public struct Photo: Codable, Identifiable, Equatable {
158158
public let stringURL: String?
159159

160160
public var imageURL: URL? {
161-
.init(string: stringURL.valueOrEmpty)
161+
stringURL.queryAllowedURL
162162
}
163163

164164
public enum CodingKeys: String, CodingKey {

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/UserResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public extension UserResponse {
7373
}
7474

7575
var avatarURL: URL? {
76-
.init(string: imageStringURL.valueOrEmpty)
76+
imageStringURL.queryAllowedURL
7777
}
7878

7979
var gender: String {

SwiftUI-WorkoutApp/Utils/Sources/Utils/String+.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
public extension String {
24
@available(*, deprecated, message: "Нужно корректно парсить HTML")
35
var withoutHTML: String {
@@ -19,3 +21,12 @@ public extension String {
1921
replacingOccurrences(of: " ", with: "")
2022
}
2123
}
24+
25+
public extension String? {
26+
/// `URL` без кириллицы
27+
var queryAllowedURL: URL? {
28+
guard let percentEncoded = self?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
29+
else { return nil }
30+
return .init(string: percentEncoded)
31+
}
32+
}

SwiftUI-WorkoutApp/Utils/Tests/UtilsTests/UtilsTests.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import XCTest
33

44
final class UtilsTests: XCTestCase {
5-
func testStringValueOrEmpty() throws {
5+
func testStringValueOrEmpty() {
66
let string: String? = "Test string"
77
let stringNil: String? = nil
88
XCTAssertNil(stringNil)
@@ -11,7 +11,7 @@ final class UtilsTests: XCTestCase {
1111
XCTAssertEqual(string.valueOrEmpty, "Test string")
1212
}
1313

14-
func testIntValueOrZero() throws {
14+
func testIntValueOrZero() {
1515
let one: Int? = 1
1616
let intNil: Int? = nil
1717
XCTAssertNil(intNil)
@@ -20,7 +20,7 @@ final class UtilsTests: XCTestCase {
2020
XCTAssertEqual(one.valueOrZero, 1)
2121
}
2222

23-
func testBoolIsTrue() throws {
23+
func testBoolIsTrue() {
2424
let bool: Bool? = true
2525
let boolNil: Bool? = nil
2626
XCTAssertNil(boolNil)
@@ -29,15 +29,21 @@ final class UtilsTests: XCTestCase {
2929
XCTAssertEqual(bool.isTrue, true)
3030
}
3131

32-
func testStringWithoutHTML() throws {
32+
func testStringWithoutHTML() {
3333
let htmlString = "<p>Строка с тегами html.<p>"
3434
let cleanString = htmlString.withoutHTML
3535
XCTAssertEqual(cleanString, "Строка с тегами html.")
3636
}
3737

38-
func testCapitalizingFirstLetter() throws {
38+
func testCapitalizingFirstLetter() {
3939
let string = "test string"
4040
let newString = string.capitalizingFirstLetter
4141
XCTAssertEqual(newString, "Test string")
4242
}
43+
44+
func testQueryAllowedURL() {
45+
let urlString: String? = "https://workout.su/uploads/userfiles/св3.jpg"
46+
let resultURL = urlString.queryAllowedURL
47+
XCTAssertEqual(resultURL, URL(string: "https://workout.su/uploads/userfiles/%D1%81%D0%B23.jpg"))
48+
}
4349
}

0 commit comments

Comments
 (0)