Skip to content

Commit

Permalink
Feat: Save and load data using app group #34
Browse files Browse the repository at this point in the history
- Save and load favorites data at path using UserDefaults Domain
- Use App Group
  • Loading branch information
jinios committed Sep 17, 2018
1 parent f89b02d commit e9d0051
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 4 additions & 0 deletions MartHoliday/MartHoliday/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?
private let appGroup = UserDefaults.init(suiteName: "group.jinios.martholiday")

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Expand All @@ -38,6 +39,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

guard let loadedData = DataStorage<FavoriteList>.load() else { return true }
FavoriteList.loadSavedData(loadedData)
appGroup?.setValue(FavoriteList.shared().martList(), forKey: "favorites")
return true
}

Expand Down Expand Up @@ -72,6 +74,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

func applicationDidEnterBackground(_ application: UIApplication) {
DataStorage<FavoriteList>.save(data: FavoriteList.shared())
appGroup?.setValue(FavoriteList.shared().martList(), forKey: "favorites")
}

func applicationWillEnterForeground(_ application: UIApplication) {
Expand All @@ -89,6 +92,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

func applicationWillTerminate(_ application: UIApplication) {
DataStorage<FavoriteList>.save(data: FavoriteList.shared())
appGroup?.setValue(FavoriteList.shared().martList(), forKey: "favorites")
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
3 changes: 3 additions & 0 deletions MartHoliday/MartHoliday/Model/FavoriteList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class FavoriteList: NSObject, NSCoding {
martSet = aDecoder.decodeObject(forKey: String(describing: FavoriteList.self)) as! Set<Int>
}

private let appGroup = UserDefaults.init(suiteName: "group.jinios.martholiday")

private static var sharedFavorite = FavoriteList()

private override init() {
Expand All @@ -39,6 +41,7 @@ class FavoriteList: NSObject, NSCoding {
private var martSet: Set<Int> {
didSet {
DataStorage<FavoriteList>.save(data: self)
appGroup?.setValue(self.martList(), forKey: "favorites")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<objects>
<viewController id="M4Y-Lb-cyx" customClass="TodayViewController" customModule="MartHolidayTodayExtension" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" simulatedAppContext="notificationCenter" id="S3S-Oj-5AN">
<rect key="frame" x="0.0" y="0.0" width="320" height="110"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="150"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="2WP-eG-rcc">
<rect key="frame" x="0.0" y="0.0" width="320" height="110"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="150"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="todayExCell" id="iHc-O2-fPe" customClass="TodayExtensionTableViewCell" customModule="MartHolidayTodayExtension" customModuleProvider="target">
Expand All @@ -39,15 +39,17 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bLm-Qf-uvi">
<rect key="frame" x="15" y="13.5" width="165" height="17"/>
<fontDescription key="fontDescription" name="NanumSquareRoundOTFR" family="NanumSquareRoundOTF" pointSize="15"/>
<rect key="frame" x="15" y="15" width="165" height="14.5"/>
<fontDescription key="fontDescription" name="NanumSquareRoundOTFR" family="NanumSquareRoundOTF" pointSize="13"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rzl-2q-KzR">
<button opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rzl-2q-KzR">
<rect key="frame" x="200" y="8.5" width="105" height="27"/>
<fontDescription key="fontDescription" name="NanumSquareRoundOTFB" family="NanumSquareRoundOTF" pointSize="13"/>
<state key="normal" title="Button"/>
<state key="normal" title="Button">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
</button>
</subviews>
<constraints>
Expand Down Expand Up @@ -78,7 +80,7 @@
</view>
<extendedEdge key="edgesForExtendedLayout"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="320" height="110"/>
<size key="freeformSize" width="320" height="150"/>
<connections>
<outlet property="tableView" destination="2WP-eG-rcc" id="Gmi-QE-nOL"/>
</connections>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class TodayExtensionTableViewCell: UITableViewCell {

func setData(branch: Branch) {
setDateButton()
self.branchTitle.text = branch.martType + branch.branchName + "의 휴무일"
self.branchTitle.text = branch.martType + " " + branch.branchName + "의 휴무일"
self.dateButton.setTitle(branch.holidays[0], for: .normal)
}

func setDateButton() {
dateButton.contentEdgeInsets = UIEdgeInsets(top: 3, left: 5, bottom: 3, right: 5)
dateButton.layer.cornerRadius = 7.0
dateButton.clipsToBounds = true
dateButton.backgroundColor = UIColor.lightGray
dateButton.backgroundColor = UIColor(named: "mh-navy")
dateButton.titleLabel?.textColor = UIColor.white
dateButton.isUserInteractionEnabled = false
}
Expand Down
11 changes: 8 additions & 3 deletions MartHoliday/MartHolidayTodayExtension/TodayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDeleg

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
let expanded = (activeDisplayMode == .expanded)
print(maxSize)
preferredContentSize = expanded ? CGSize(width: maxSize.width, height: 200) : maxSize
preferredContentSize = expanded ? CGSize(width: maxSize.width, height: 150) : maxSize
}

// After updating the preferred size, you must reload the chart’s data so that it redraws based on the new layout.
Expand All @@ -49,9 +48,15 @@ class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDeleg
}

var favoriteList = BranchList()
private let appGroup = UserDefaults.init(suiteName: "group.jinios.martholiday")

func getFavorites() -> [Int] {
guard let result = appGroup?.value(forKey: "favorites") as? [Int] else { return [1] }
return result
}

func setFavoriteBranch(handler: @escaping (() -> Void)) {
let ids = [1,2,3]
let ids = getFavorites()
let idstr = ids.map{String($0)}.joined(separator: ",")
guard let baseURL = URL(string: "http://ec2-13-209-38-224.ap-northeast-2.compute.amazonaws.com/api/mart/branch") else { return }
let url = baseURL.appendingPathComponent(idstr)
Expand Down

0 comments on commit e9d0051

Please sign in to comment.