LiTranslate-iOS/LiTranslate/Application/ObservableObjects/RecentlyUsedLanguages.swift

47 lines
1.1 KiB
Swift
Raw Normal View History

2022-09-10 19:46:05 +02:00
//
// RecentlyUsedLanguages.swift
// LiTranslate
//
// Created by Viktor Kalyniuk on 02.09.2022.
//
import Foundation
import SwiftUI
class RecentlyUsedLanguages: ObservableObject {
@Published var array = [Languages]() {
didSet {
save(array: array)
}
}
init() {
array = getArray()
}
func append(language: Languages) {
if !array.contains(language) {
if array.count == Numbers.six {
array.removeLast()
}
array.insert(language, at: Numbers.zero)
}
}
func getArray() -> [Languages] {
if let savedData = UserDefaults.standard.data(forKey: Keys.recentlyUsedLanguages) {
if let decodedArray = try? JSONDecoder().decode([Languages].self, from: savedData) {
return decodedArray
}
}
return []
}
func save(array: [Languages]) {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(array) {
UserDefaults.standard.set(encoded, forKey: Keys.recentlyUsedLanguages)
}
}
}