Skip to content

Commit

Permalink
Implemented switch to change dark or light mode
Browse files Browse the repository at this point in the history
  • Loading branch information
All3yp committed Oct 6, 2021
1 parent fa9cb83 commit 06afd9c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Sticky Links.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
18FA0757270DED8100AEC7D8 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FA0756270DED8100AEC7D8 /* Colors.swift */; };
B26DCA1C27062C0A0088032C /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = B26DCA1B27062C0A0088032C /* README.md */; };
B290640827040F81004250FA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B290640727040F81004250FA /* AppDelegate.swift */; };
B290640A27040F81004250FA /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B290640927040F81004250FA /* SceneDelegate.swift */; };
Expand All @@ -20,6 +21,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
18FA0756270DED8100AEC7D8 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
B26DCA1B27062C0A0088032C /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
B290640427040F81004250FA /* Sticky Links.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Sticky Links.app"; sourceTree = BUILT_PRODUCTS_DIR; };
B290640727040F81004250FA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,6 +83,7 @@
isa = PBXGroup;
children = (
B290642627041685004250FA /* CategoryViewController.swift */,
18FA0756270DED8100AEC7D8 /* Colors.swift */,
B290640B27040F81004250FA /* LinksViewController.swift */,
B29064242704152A004250FA /* WebViewController.swift */,
);
Expand Down Expand Up @@ -178,6 +181,7 @@
B290641227040F81004250FA /* Sticky_Links.xcdatamodeld in Sources */,
B290640C27040F81004250FA /* LinksViewController.swift in Sources */,
B290640827040F81004250FA /* AppDelegate.swift in Sources */,
18FA0757270DED8100AEC7D8 /* Colors.swift in Sources */,
B29064252704152A004250FA /* WebViewController.swift in Sources */,
B290642727041685004250FA /* CategoryViewController.swift in Sources */,
B290640A27040F81004250FA /* SceneDelegate.swift in Sources */,
Expand Down
Binary file not shown.
21 changes: 17 additions & 4 deletions Sticky Links/Controllers/CategoryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ class CategoryViewController: UITableViewController {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let request : NSFetchRequest<Category> = Category.fetchRequest()



@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var outletSwitch: UISwitch!

@IBAction func darkAction(_ sender: Any) {
if outletSwitch.isOn {
view.window?.overrideUserInterfaceStyle = .dark
UserDefaults.standard.set(true, forKey: "DarkMode")
} else {
view.window?.overrideUserInterfaceStyle = .light
UserDefaults.standard.set(false, forKey: "DarkMode")
}

}

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(named: "PrimaryBackgroundColor")
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
loadCategory()

outletSwitch.isOn = UserDefaults.standard.value(forKey: "DarkMode") as? Bool ?? false
}
}



//MARK: Table View Methods

extension CategoryViewController{
Expand Down
14 changes: 14 additions & 0 deletions Sticky Links/Controllers/Colors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Colors.swift
// Sticky Links
//
// Created by Alley Pereira on 06/10/21.
//

import UIKit

extension UIColor {

static let primaryBackgroundColor: UIColor = UIColor(named: "PrimaryBackgroundColor")!
static let primaryTextColor: UIColor = UIColor(named: "TextColors")!
}
30 changes: 30 additions & 0 deletions Sticky Links/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }

setupUserInterfaceStyle()
}

func setupUserInterfaceStyle() {

var darkModeIsEnabled: Bool!

if let darkModeFromUserDefault = UserDefaults.standard.value(forKey: "DarkMode") as? Bool {

darkModeIsEnabled = darkModeFromUserDefault

} else {

switch UITraitCollection.current.userInterfaceStyle {
case .light:
UserDefaults.standard.set(false, forKey: "DarkMode")
darkModeIsEnabled = false
case .dark:
UserDefaults.standard.set(true, forKey: "DarkMode")
darkModeIsEnabled = true
default:
UserDefaults.standard.set(false, forKey: "DarkMode")
darkModeIsEnabled = false
}

}

window?.overrideUserInterfaceStyle = darkModeIsEnabled ? .dark : .light

}

func sceneDidDisconnect(_ scene: UIScene) {
Expand Down
18 changes: 14 additions & 4 deletions Sticky Links/View/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@
</searchBar>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="CategoryCell" id="GWR-RC-CK0">
<rect key="frame" x="0.0" y="88.666666030883789" width="414" height="43.666667938232422"/>
<rect key="frame" x="0.0" y="88.666666030883789" width="414" height="43.333332061767578"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="GWR-RC-CK0" id="ctg-Xh-Rr2">
<rect key="frame" x="0.0" y="0.0" width="384.33333333333331" height="43.666667938232422"/>
<rect key="frame" x="0.0" y="0.0" width="384.33333333333331" height="43.333332061767578"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="kmH-hp-50L" userLabel="Bookmark Category Button">
<rect key="frame" x="337.33333333333331" y="6.0000000000000018" width="41" height="31.666666666666671"/>
<rect key="frame" x="337.33333333333331" y="5.9999999999999982" width="41" height="31.333333333333329"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" image="bookmark" catalog="system"/>
<connections>
Expand All @@ -193,6 +193,15 @@
</tableView>
<toolbarItems/>
<navigationItem key="navigationItem" title="Categories" id="GEY-ir-NSG">
<barButtonItem key="leftBarButtonItem" id="VnW-Kb-Fjy">
<switch key="customView" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" id="NbN-7x-rF8">
<rect key="frame" x="20" y="6.6666666666666679" width="51" height="31.000000000000004"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<action selector="darkAction:" destination="FBQ-C6-Eyw" eventType="valueChanged" id="SXA-I6-dfM"/>
</connections>
</switch>
</barButtonItem>
<rightBarButtonItems>
<barButtonItem systemItem="add" id="0xG-VA-zJm">
<connections>
Expand All @@ -209,6 +218,7 @@
</navigationItem>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="outletSwitch" destination="NbN-7x-rF8" id="1Aa-gi-9jV"/>
<outlet property="searchBar" destination="u6J-Vx-bbb" id="2mU-4m-76N"/>
<segue destination="X4Q-zs-uTj" kind="show" identifier="CategorySegue" id="vBw-Pn-hjt"/>
</connections>
Expand All @@ -224,7 +234,7 @@
<image name="magnifyingglass" catalog="system" width="128" height="115"/>
<image name="square.and.arrow.up" catalog="system" width="115" height="128"/>
<namedColor name="PrimaryBackgroundColor">
<color red="0.98782485720000002" green="0.97735267879999999" blue="0.91371709109999999" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<color red="0.93699997663497925" green="0.94499999284744263" blue="0.95300000905990601" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</namedColor>
<namedColor name="TextColors">
<color red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
Expand Down

0 comments on commit 06afd9c

Please sign in to comment.