LMStorage 1.0.7

LMStorage 1.0.7

Maintained by John Lima.



LMStorage 1.0.7

LMStorage


๐Ÿ’พ LMStorage is a framework which reduces the complexity of managing a persistent layer.

โ—๏ธRequirements

  • iOS 12.1+
  • tvOS 9.0+
  • Swift 5.0+

โš’ Installation

Swift Package Manager

LMStorage is available through SPM. To install it, follow the steps:

Open Xcode project > File > Swift Packages > Add Package Dependecy

After that, put the url in the field: https://github.com/thejohnlima/LMStorage.git

CocoaPods

LMStorage is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'LMStorage'

and run pod install

๐ŸŽ“ How to use LMCodable

Parsing a dictionary

{
  "title": "Iron Man",
  "category": "action",
  "year": 2008,
  "rate": "94%",
  "image": "https://i.pinimg.com/564x/9d/e9/1e/9de91e58cfde7f05eb79e203301980ef.jpg"
}
import LMStorage

struct Movie: LMCodable {
  let title: String?
  let description: String?
  let category: String?
  let year: Int?
  let rate: String?
  let link: String?
  let image: String?
}

Parsing the JSON movie using a Data type returned by the request

let movie = Movie(data)

Parsing a local JSON file

let movie: Movie? = Movie.getItem(from: "file_name")

Parsing an array

[
  {
    "title": "Iron Man",
    "category": "action",
    "year": 2008,
    "rate": "94%",
    "image": "https://i.pinimg.com/564x/9d/e9/1e/9de91e58cfde7f05eb79e203301980ef.jpg"
  },
  {
    "title": "Black Panther",
    "category": "action",
    "year": 2018,
    "rate": "96%",
    "image": "https://i.pinimg.com/564x/43/cd/5b/43cd5b065b271006da5491645e0564c3.jpg"
  }
]
import LMStorage

struct Movie: LMCodable {
  let title: String?
  let description: String?
  let category: String?
  let year: Int?
  let rate: String?
  let link: String?
  let image: String?
}

Parsing the JSON movie using a Data type returned by the request

let data: Data = ...
let movies: [Movie] = data.toItems()

Parsing a local JSON file

let movies: [Movie] = Movie.getItems(from: "movies_file_name")

๐ŸŽ“ How to use LMDefaults

Import library in your file:

import LMStorage

Create a struct for your Defaults files:

struct MyDefaults: LMDefaults {
  enum Keys: String {
    case currentUser
    case accessToken
    case haveSeenOnboarding
  }
}

Than, in the view controller just save what you need:

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()

    MyDefaults.set(true, forKey: .haveSeenOnboarding)

    let haveSeenOnboarding = MyDefaults.bool(forKey: .haveSeenOnboarding)

    print("Have Seen Onboarding: \(haveSeenOnboarding)")
  }
}

๐ŸŽ“ How to use LMStorage

Import library in your file:

import LMStorage
/// Saving user example
let user = User(id: "1", name: "John", age: 30)
let storage = UserStorage()
storage.create(user)
/// Saving secure user example
let user = User(id: "2", name: "Test", age: 1130)
let secureStorage = UserSecureStorage()
secureStorage.create(user)
/// Storage Keys Example
struct Key {
  static let container = "LMStorage"
  static let user = "User"
}
/// User Example
struct User: LMCodable {
  let id: String
  let name: String
  let age: Int
}
/// User storage example
struct UserStorage: LMStorageProtocol {
  typealias T = User

  private let storage: LMAbstractStorage<T>

  init() {
    storage = LMStorage(with: Key.user)
  }

  func getFirst() -> User? {
    return storage.getFirst()
  }

  func create(_ register: User) -> Bool {
    return storage.create(register)
  }

  func update(_ register: User) -> Bool {
    return storage.update(register)
  }

  func delete() -> Bool {
    return storage.delete()
  }
}
/// User secure storage example
struct UserSecureStorage: LMStorageProtocol {
  typealias T = User

  private let storage = LMSecureStorage<T>(with: Key.container)

  init() {}

  func getFirst() -> User? {
    return storage.getFirst(key: Key.user)
  }

  func create(_ register: User) -> Bool {
    return storage.create(register, key: Key.user)
  }

  func update(_ register: User) -> Bool {
    return storage.update(register, key: Key.user)
  }

  func delete() -> Bool {
    return storage.delete(key: Key.user)
  }
}

If you need more examples, take a look at Example project.

๐Ÿ™‹๐Ÿปโ€ Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request. ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป

๐Ÿ“œ License

LMStorage is under MIT license. See the LICENSE file for more info.