Skip to content

Commit b790713

Browse files
committed
image publisher with images
1 parent 1c3316d commit b790713

13 files changed

Lines changed: 194 additions & 11 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// ImagePublisher.swift
3+
//
4+
//
5+
// Created by Maxim Abakumov on 2021. 06. 01.
6+
//
7+
// Copyright © 2020, Maxim Abakumov. MIT License.
8+
//
9+
10+
import UIKit
11+
12+
public protocol ImageLibrarySubscriber {
13+
14+
func receive(images: [UIImage])
15+
}
16+
17+
final class ImagePublisher {
18+
19+
private var subscribers: [ImageLibrarySubscriber] = []
20+
private var images: [UIImage] = []
21+
22+
func add(image: UIImage) {
23+
images.append(image)
24+
notifySubscribers()
25+
}
26+
27+
func remove(image: UIImage) {
28+
guard let index = images.firstIndex(where: { $0 == image }) else { return }
29+
images.remove(at: index)
30+
notifySubscribers()
31+
}
32+
33+
func removeAll() {
34+
if !images.isEmpty {
35+
images = []
36+
}
37+
notifySubscribers()
38+
}
39+
40+
func add(subscriber: ImageLibrarySubscriber) {
41+
subscribers.append(subscriber)
42+
}
43+
44+
func remove(subscriber filter: (ImageLibrarySubscriber) -> Bool) {
45+
guard let index = subscribers.firstIndex(where: filter) else { return }
46+
subscribers.remove(at: index)
47+
}
48+
49+
private func notifySubscribers() {
50+
subscribers.forEach {
51+
$0.receive(images: images)
52+
}
53+
}
54+
}
55+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Maxim Abakumov on 2021. 06. 01.
6+
//
7+
// Copyright © 2020, Maxim Abakumov. MIT License.
8+
//
9+
10+
import UIKit
11+
12+
final public class ImagePublisherFacade {
13+
14+
public typealias RepeatCount = Int
15+
16+
private let publisher = ImagePublisher()
17+
18+
// добавляет подписчика
19+
20+
public func subscribe(_ subscriber: ImageLibrarySubscriber) {
21+
publisher.add(subscriber: subscriber)
22+
}
23+
24+
// удаляет подписчика
25+
26+
public func removeSubscription(for subscriber: ImageLibrarySubscriber) {
27+
publisher.remove { _ in
28+
return true
29+
}
30+
}
31+
32+
/// По таймеру добавляет новые случайные UIImage в библиотеку ImagePublisher
33+
/// Поскольку изображений в библиотеке немного, можно добавлять свои картинки
34+
35+
@available(iOS 10.0, *)
36+
public func addImagesWithTimer(
37+
time interval: TimeInterval,
38+
repeat times: RepeatCount,
39+
userImage: UIImage? = nil
40+
) {
41+
var count = 0
42+
Timer.scheduledTimer(
43+
withTimeInterval: interval,
44+
repeats: true
45+
) { [weak self] timer in
46+
count += 1
47+
48+
if let image = userImage {
49+
self?.publisher.add(image: image)
50+
} else {
51+
guard let imageCase = Images.allCases.randomElement() else { return }
52+
let libraryImage = imageCase.image(name: imageCase)
53+
self?.publisher.add(image: libraryImage)
54+
}
55+
56+
if count == times {
57+
timer.invalidate()
58+
}
59+
}
60+
}
61+
62+
// метод обнуляет библиотеку изображений
63+
64+
public func rechargeImageLibrary() {
65+
publisher.removeAll()
66+
}
67+
}

Sources/iOSIntPackage/Images.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99

1010
import UIKit
1111

12-
public struct Images {
12+
public enum Images: String, CaseIterable {
1313

14-
public static var checkmark: UIImage {
15-
return getPdfImage(by: "checkmark")
16-
}
17-
18-
public static var sunset: UIImage {
19-
return getPdfImage(by: "sunset")
20-
}
14+
case checkmark
15+
case sunset
16+
case mountain
17+
case annecy
18+
case annecy2
19+
case skyscraper
20+
case avatar
21+
case cafe
2122

22-
public static var mountain: UIImage {
23-
return getPdfImage(by: "mountain")
23+
public func image(name: Images) -> UIImage {
24+
return getPdfImage(by: name.rawValue)
2425
}
2526

26-
private static func getPdfImage(by name: String) -> UIImage {
27+
private func getPdfImage(by name: String) -> UIImage {
2728

2829
guard let image = UIImage(
2930
named: name,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "annecy.pdf",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
250 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "annecy2.pdf",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "avatarrr.pdf",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "cafe.pdf",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}

0 commit comments

Comments
 (0)