Files
bundesmessenger-ios/Riot/Modules/Home/AllChats/AllChatsLayoutSettings.swift
T
2023-01-27 10:18:49 +00:00

77 lines
3.1 KiB
Swift

//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
// bwi: we use an additional section and have specific settings we want to apply on first start of the app
@objcMembers
class AllChatsLayoutSettings: NSObject, NSCoding {
fileprivate enum Constants {
static let sectionsKey = "sections"
static let filtersKey = "filters"
static let sortingKey = "sorting"
}
let sections: AllChatsLayoutSectionType
let filters: AllChatsLayoutFilterType
let sorting: AllChatsLayoutSortingType
init(sections: AllChatsLayoutSectionType = [],
filters: AllChatsLayoutFilterType = [],
sorting: AllChatsLayoutSortingType = .activity) {
if !UserDefaults.standard.bool(forKey: "FirstStartWithNewSettings") {
if PersonalNotesSettings().personalNotesVisible && BWIBuildSettings.shared.bwiPersonalNotesRoom {
self.sections = [.bwiPersonalNotes]
} else {
self.sections = []
}
self.filters = [.unreads, .favourites, .people]
self.sorting = .activity
UserDefaults.standard.set(true, forKey: "FirstStartWithNewSettings")
} else {
self.sections = sections
self.filters = filters
self.sorting = sorting
}
}
func encode(with coder: NSCoder) {
coder.encode(Int(sections.rawValue), forKey: Constants.sectionsKey)
coder.encode(Int(filters.rawValue), forKey: Constants.filtersKey)
coder.encode(Int(sorting.rawValue), forKey: Constants.sortingKey)
}
required init?(coder: NSCoder) {
// bwi: there are cases when only init with coder is called, so use our first start settings here, too
if !UserDefaults.standard.bool(forKey: "FirstStartWithNewSettings") {
if PersonalNotesSettings().personalNotesVisible && BWIBuildSettings.shared.bwiPersonalNotesRoom {
self.sections = [.bwiPersonalNotes]
} else {
self.sections = []
}
self.filters = [.unreads, .favourites, .people]
self.sorting = .activity
UserDefaults.standard.set(true, forKey: "FirstStartWithNewSettings")
} else {
self.sections = AllChatsLayoutSectionType(rawValue: UInt(coder.decodeInteger(forKey: Constants.sectionsKey)))
self.filters = AllChatsLayoutFilterType(rawValue: UInt(coder.decodeInteger(forKey: Constants.filtersKey)))
self.sorting = AllChatsLayoutSortingType(rawValue: UInt(coder.decodeInteger(forKey: Constants.sortingKey))) ?? .activity
}
}
}