Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
release: v2.0 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
omaralbeik authored Aug 21, 2020
1 parent 260541f commit f2233cb
Show file tree
Hide file tree
Showing 38 changed files with 2,599 additions and 490 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
run: |
brew update
brew outdated xctool || brew upgrade xctool
brew install swiftlint
- name: Test iOS
run: |
xcodebuild clean build test -project $PROJECT -scheme $SCHEME -destination "$DESTINATION" | XCPRETTY_JSON_FILE_OUTPUT="xcodebuild-ios.json" xcpretty -f `xcpretty-json-formatter`
Expand Down Expand Up @@ -57,15 +56,6 @@ jobs:
SCHEME: UserDefaultsStore
DESTINATION: name=Apple Watch Series 5 - 40mm

Swiftlint:
runs-on: [macos]
name: SwiftLint
steps:
- uses: actions/checkout@v1
- name: SwiftLint
run: |
brew install swiftlint
swiftlint
CocoaPods:
name: CocoaPods
runs-on: macos-latest
Expand Down
36 changes: 32 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swift-tools-version:5.1
//
// SingleUserDefaultsStore
//
// Copyright (c) 2018-Present Omar Albeik - https://github.com/omaralbeik
//
// 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 PackageDescription

let package = Package(
name: "UserDefaultsStore",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(name: "UserDefaultsStore", targets: ["UserDefaultsStore"])
],
dependencies: [],
targets: [
.target(name: "UserDefaultsStore", dependencies: [], path: "Sources"),
.testTarget(name: "UserDefaultsStoreTests", dependencies: ["UserDefaultsStore"], path: "Tests"),
]
.testTarget(name: "UserDefaultsStoreTests", dependencies: ["UserDefaultsStore"], path: "Tests")
],
swiftLanguageVersions: [.v5]
)
72 changes: 40 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@
# tl;dr
You love Swift's `Codable` protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve -**reasonable amount 😅**- of `Codable` objects, in a couple lines of code!

---

## Installation
## Introducing v2.0

<details>
<summary>CocoaPods (Recommended)</summary>
</br>
<p>To integrate UserDefaultsStore into your Xcode project using <a href="http://cocoapods.org">CocoaPods</a>, specify it in your <code>Podfile</code>:</p>
<pre><code class="ruby language-ruby">pod 'UserDefaultsStore'</code></pre>
</details>
- Removed the `Identifiable` protocol in favor of Swift's `Identifiable`.
- Increased deployment targets to iOS `13.0`, `tvOS 13.0`, `macOS 10.15`, and `watchOS 6.0`.
- Objects defined as non-final classes can now be used as well.
- Added new `generateSnapshot()` and `restoreSnapshot(_:)` methods to generate and restore a `Snapshot` object that can be saved (e.g. to iCloud) and restored later.
- Fixed a bug where `objectsCount` might run out of sync with the actual count of objects in store.

<details>
<summary>Carthage</summary>
</br>
<p>To integrate UserDefaultsStore into your Xcode project using <a href="https://github.com/Carthage/Carthage">Carthage</a>, specify it in your <code>Cartfile</code>:</p>
---

<pre><code class="ogdl language-ogdl">github "omaralbeik/UserDefaultsStore" ~&gt; 1.5.0
</code></pre>
</details>
## Installation

<details>
<summary>Swift Package Manager</summary>
<summary>Swift Package Manager (Recommended)</summary>
</br>
<p>You can use <a href="https://swift.org/package-manager">The Swift Package Manager</a> to install <code>UserDefaultsStore</code> by adding the proper description to your <code>Package.swift</code> file:</p>

Expand All @@ -47,7 +42,7 @@ let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.package(url: "https://github.com/omaralbeik/UserDefaultsStore.git", from: "1.5.0")
.package(url: "https://github.com/omaralbeik/UserDefaultsStore.git", from: "2.0.0")
]
)
</code></pre>
Expand All @@ -62,6 +57,23 @@ let package = Package(
<p>Then run <code>swift package update</code>.</p>
</details>


<details>
<summary>CocoaPods</summary>
</br>
<p>To integrate UserDefaultsStore into your Xcode project using <a href="http://cocoapods.org">CocoaPods</a>, specify it in your <code>Podfile</code>:</p>
<pre><code class="ruby language-ruby">pod 'UserDefaultsStore'</code></pre>
</details>

<details>
<summary>Carthage</summary>
</br>
<p>To integrate UserDefaultsStore into your Xcode project using <a href="https://github.com/Carthage/Carthage">Carthage</a>, specify it in your <code>Cartfile</code>:</p>

<pre><code class="ogdl language-ogdl">github "omaralbeik/UserDefaultsStore" ~&gt; 2.0.0
</code></pre>
</details>

<details>
<summary>Manually</summary>
</br>
Expand Down Expand Up @@ -92,39 +104,35 @@ struct Laptop: Codable {
Here is how you store them in **UserDefaultsStore**:


### 1. Conform to the `Identifiable` protocol and set the `idKey` property
### 1. Conform to the `Identifiable` protocol and set the `id` property

The `Identifiable` protocol lets UserDefaultsStore knows what is the unique id for each object.

```swift
struct User: Codable, Identifiable {
static let idKey = \User.id
...
}
```

```swift
struct Laptop: Codable, Identifiable {
static let idKey = \Laptop.model
var id: String { model }
...
}
```

> Notice how `User` uses `Int` for its id, while `Laptop` uses `String`, in fact the id can be any `Hashable` type. UserDefaults uses Swift keypaths to refer to properties without actually invoking them. Swift rocks 🤘

### 2. Create UserDefaults Stores

```swift
let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")!
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")!
let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")
```

### 3. Voilà, you're all set!

```swift
let macbook = Laptop(model: "A1278", name: "MacBook Pro")
let john = User(userId: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)
let john = User(id: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)

// Save an object to a store
try! usersStore.save(john)
Expand Down Expand Up @@ -156,21 +164,21 @@ laptops.deleteAll()
// Know how many objects are stored in a store
let usersCount = usersStore.objectsCount

```
// Create a snapshot
let snapshot = usersStore.generateSnapshot()

// Restore a pre-generated snapshot
try? usersStore.restoreSnapshot(snapshot)
```

## Looking to store a single item only?

Use [`SingleUserDefaultsStore`](https://github.com/omaralbeik/UserDefaultsStore/blob/master/Sources/SingleUserDefaultsStore.swift), it enables storing and retrieving a single value of `Int`, `Double`, `String`, or any `Codable` type.

## Note about using `class` instead of `struct`
At the moment, only `final` classes are supported, please take this into consideration before using the library.

## Requirements

- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.0+
- Swift 4.2+
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Swift 5.0+


## Thanks
Expand Down
33 changes: 0 additions & 33 deletions Sources/Identifiable.swift

This file was deleted.

Loading

0 comments on commit f2233cb

Please sign in to comment.