Skip to content

Commit

Permalink
Some minor syntax improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed May 15, 2019
1 parent 4465f72 commit 0b20eca
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
26 changes: 12 additions & 14 deletions Sources/Image/ImageProgressive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ final class ImageProgressiveProvider {
let isFastest = option.isFastestScan

func add(decode data: Data) {
queue.add(minimum: interval) { (completion) in
queue.add(minimum: interval) { completion in
guard self.isContinueClosure() else {
completion()
return
}

self.decoder.decode(data, with: callbacks) { (image) in
self.decoder.decode(data, with: callbacks) { image in
defer { completion() }
guard self.isContinueClosure() else { return }
guard self.isWait || !self.isFinishedClosure() else { return }
Expand All @@ -108,13 +108,11 @@ final class ImageProgressiveProvider {
}

if isFastest {
guard let data: Data = decoder.scanning(data) else { return }

guard let data = decoder.scanning(data) else { return }
add(decode: data)

} else {
let datas: [Data] = decoder.scanning(data)
for data in datas {
let allData: [Data] = decoder.scanning(data)
for data in allData {
add(decode: data)
}
}
Expand All @@ -134,10 +132,10 @@ final class ImageProgressiveProvider {
}
}

fileprivate final class ImageProgressiveDecoder {
private final class ImageProgressiveDecoder {

private let options: KingfisherParsedOptionsInfo
private(set) var scannedCount: Int = 0
private(set) var scannedCount = 0
private var scannedIndex = -1

init(_ options: KingfisherParsedOptionsInfo) {
Expand All @@ -148,15 +146,15 @@ fileprivate final class ImageProgressiveDecoder {
guard data.kf.contains(jpeg: .SOF2) else {
return []
}
guard (scannedIndex + 1) < data.count else {
guard scannedIndex + 1 < data.count else {
return []
}

var datas: [Data] = []
var index = scannedIndex + 1
var count = scannedCount

while index < (data.count - 1) {
while index < data.count - 1 {
scannedIndex = index
// 0xFF, 0xDA - Start Of Scan
let SOS = ImageFormat.JPEGMarker.SOS.bytes
Expand Down Expand Up @@ -184,15 +182,15 @@ fileprivate final class ImageProgressiveDecoder {
guard data.kf.contains(jpeg: .SOF2) else {
return nil
}
guard (scannedIndex + 1) < data.count else {
guard scannedIndex + 1 < data.count else {
return nil
}

var index = scannedIndex + 1
var count = scannedCount
var lastSOSIndex = 0

while index < (data.count - 1) {
while index < data.count - 1 {
scannedIndex = index
// 0xFF, 0xDA - Start Of Scan
let SOS = ImageFormat.JPEGMarker.SOS.bytes
Expand Down Expand Up @@ -263,7 +261,7 @@ fileprivate final class ImageProgressiveDecoder {
}
}

fileprivate final class ImageProgressiveSerialQueue {
private final class ImageProgressiveSerialQueue {
typealias ClosureCallback = ((@escaping () -> Void)) -> Void

private let queue: DispatchQueue
Expand Down
51 changes: 51 additions & 0 deletions Tests/KingfisherTests/DataReceivingSideEffectTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// DataReceivingSideEffectTests.swift
// Kingfisher
//
// Created by jp20028 on 2019/05/15.
//
// Copyright (c) 2019 Wei Wang <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import XCTest

class DataReceivingSideEffectTests: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}

0 comments on commit 0b20eca

Please sign in to comment.