Skip to content

[shared_preferences_foundation] Run swift-format on Swift files #5933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import Foundation

#if os(iOS)
import Flutter
import Flutter
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change in non-example/test files are the whitespace changes in this file. Does change actually need a pubspec version bump @stuartmorgan?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there's also removal of a semicolon, so it's not just whitespace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also unquestionably a no-op change though in terms of compile results.

I'm in favor of having no version change or CHANGELOG for this, as something that doesn't affect anything but our own development.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(But I'm also fine with leaving it as-is)

#elseif os(macOS)
import FlutterMacOS
import FlutterMacOS
#endif

public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi {
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = SharedPreferencesPlugin()
// Workaround for https://github.com/flutter/flutter/issues/118103.
#if os(iOS)
let messenger = registrar.messenger()
#else
let messenger = registrar.messenger
#endif
#if os(iOS)
let messenger = registrar.messenger()
#else
let messenger = registrar.messenger
#endif
UserDefaultsApiSetup.setUp(binaryMessenger: messenger, api: instance)
}

func getAll(prefix: String, allowList: [String]?) -> [String? : Any?] {
func getAll(prefix: String, allowList: [String]?) -> [String?: Any?] {
return getAllPrefs(prefix: prefix, allowList: allowList)
}

Expand All @@ -47,21 +47,22 @@ public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi {
for (key, _) in getAllPrefs(prefix: prefix, allowList: allowList) {
defaults.removeObject(forKey: key)
}
return true
return true
}

/// Returns all preferences stored with specified prefix.
/// If [allowList] is included, only items included will be returned.
func getAllPrefs(prefix: String, allowList: [String]?) -> [String: Any] {
var filteredPrefs: [String: Any] = [:]
var allowSet: Set<String>?;
var allowSet: Set<String>?
if let allowList = allowList {
allowSet = Set(allowList)
}
if let appDomain = Bundle.main.bundleIdentifier,
let prefs = UserDefaults.standard.persistentDomain(forName: appDomain)
{
for (key, value) in prefs where (key.hasPrefix(prefix) && (allowSet == nil || allowSet!.contains(key))) {
for (key, value) in prefs
where (key.hasPrefix(prefix) && (allowSet == nil || allowSet!.contains(key))) {
filteredPrefs[key] = value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import XCTest

@testable import shared_preferences_foundation

#if os(iOS)
import Flutter
import Flutter
#elseif os(macOS)
import FlutterMacOS
import FlutterMacOS
#endif

@testable import shared_preferences_foundation

class RunnerTests: XCTestCase {
let prefixes: [String] = ["aPrefix", ""]

Expand All @@ -30,7 +30,7 @@ class RunnerTests: XCTestCase {
XCTAssertEqual(storedValues["\(aPrefix)aDouble"] as! Double, 3.14, accuracy: 0.0001)
XCTAssertEqual(storedValues["\(aPrefix)anInt"] as? Int, 42)
XCTAssertEqual(storedValues["\(aPrefix)aString"] as? String, "hello world")
XCTAssertEqual(storedValues["\(aPrefix)aStringList"] as? Array<String>, ["hello", "world"])
XCTAssertEqual(storedValues["\(aPrefix)aStringList"] as? [String], ["hello", "world"])
}
}

Expand Down Expand Up @@ -70,40 +70,40 @@ class RunnerTests: XCTestCase {
XCTAssertNil(finalValues[testKey] as Any?)
}
}

func testClearWithNoAllowlist() throws {
for aPrefix in prefixes {
let plugin = SharedPreferencesPlugin()
let testKey = "\(aPrefix)foo"
plugin.setValue(key: testKey, value: 42)

// Make sure there is something to clear, so the test can't pass due to a set failure.
let preRemovalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(preRemovalValues[testKey] as? Int, 42)

// Then verify that clearing works.
plugin.clear(prefix: aPrefix, allowList: nil)

let finalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertNil(finalValues[testKey] as Any?)
}

func testClearWithNoAllowlist() throws {
for aPrefix in prefixes {
let plugin = SharedPreferencesPlugin()
let testKey = "\(aPrefix)foo"
plugin.setValue(key: testKey, value: 42)

// Make sure there is something to clear, so the test can't pass due to a set failure.
let preRemovalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(preRemovalValues[testKey] as? Int, 42)

// Then verify that clearing works.
plugin.clear(prefix: aPrefix, allowList: nil)

let finalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertNil(finalValues[testKey] as Any?)
}
func testClearWithAllowlist() throws {
for aPrefix in prefixes {
let plugin = SharedPreferencesPlugin()
let testKey = "\(aPrefix)foo"
plugin.setValue(key: testKey, value: 42)

// Make sure there is something to clear, so the test can't pass due to a set failure.
let preRemovalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(preRemovalValues[testKey] as? Int, 42)

plugin.clear(prefix: aPrefix, allowList: ["\(aPrefix)notfoo"])

let finalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(finalValues[testKey] as? Int, 42)
}
}

func testClearWithAllowlist() throws {
for aPrefix in prefixes {
let plugin = SharedPreferencesPlugin()
let testKey = "\(aPrefix)foo"
plugin.setValue(key: testKey, value: 42)

// Make sure there is something to clear, so the test can't pass due to a set failure.
let preRemovalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(preRemovalValues[testKey] as? Int, 42)

plugin.clear(prefix: aPrefix, allowList: ["\(aPrefix)notfoo"])

let finalValues = plugin.getAll(prefix: aPrefix, allowList: nil)
XCTAssertEqual(finalValues[testKey] as? Int, 42)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import UIKit
import Flutter
import UIKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
Expand Down