This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Brandon T and Kyle
- Loading branch information
Showing
78 changed files
with
5,828 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2021 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
|
||
public struct FaviconAttributes: Codable { | ||
public var image: UIImage? | ||
public var backgroundColor: UIColor? | ||
public var contentMode: UIView.ContentMode = .scaleAspectFit | ||
public var includePadding: Bool = false | ||
|
||
public init( | ||
image: UIImage? = nil, | ||
backgroundColor: UIColor? = nil, | ||
contentMode: UIView.ContentMode = .scaleAspectFit, | ||
includePadding: Bool = false | ||
) { | ||
self.image = image | ||
self.backgroundColor = backgroundColor | ||
self.contentMode = contentMode | ||
self.includePadding = includePadding | ||
} | ||
|
||
// MARK: - Codable | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case image | ||
case backgroundColor | ||
case contentMode | ||
case includePadding | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(image?.sd_imageData(), forKey: .image) | ||
var rgb: Int? | ||
if let color = backgroundColor { | ||
var (r, g, b): (CGFloat, CGFloat, CGFloat) = (0.0, 0.0, 0.0) | ||
color.getRed(&r, green: &g, blue: &b, alpha: nil) | ||
rgb = (Int(r * 255.0) << 16) | (Int(g * 255.0) << 8) | Int(b * 255.0) | ||
} | ||
try container.encode(rgb, forKey: .backgroundColor) | ||
try container.encode(contentMode.rawValue, forKey: .contentMode) | ||
try container.encode(includePadding, forKey: .includePadding) | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
if let imageData = try container.decode(Data?.self, forKey: .image) { | ||
image = UIImage(data: imageData) | ||
} | ||
if let rgb = try container.decode(Int?.self, forKey: .backgroundColor) { | ||
backgroundColor = UIColor(rgb: rgb) | ||
} | ||
contentMode = UIView.ContentMode(rawValue: try container.decode(Int.self, forKey: .contentMode)) ?? .scaleAspectFit | ||
includePadding = try container.decode(Bool.self, forKey: .includePadding) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import SDWebImage | ||
import Shared | ||
import WidgetKit | ||
|
||
private let log = Logger.browserLogger | ||
|
||
public struct WidgetFavorite: Codable { | ||
public var url: URL | ||
public var favicon: FaviconAttributes? | ||
|
||
public init(url: URL, favicon: FaviconAttributes) { | ||
self.url = url | ||
self.favicon = favicon | ||
} | ||
} | ||
|
||
public class FavoritesWidgetData { | ||
private static var widgetDataRoot: URL? { | ||
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppInfo.sharedContainerIdentifier)?.appendingPathComponent("widget_data") | ||
} | ||
|
||
private static var widgetDataPath: URL? { | ||
widgetDataRoot?.appendingPathComponent("favs.json") | ||
} | ||
|
||
public static var dataExists: Bool { | ||
guard let url = widgetDataPath else { return false } | ||
return FileManager.default.fileExists(atPath: url.path) | ||
} | ||
|
||
public static func loadWidgetData() -> [WidgetFavorite]? { | ||
guard let dataPath = widgetDataPath else { return nil } | ||
do { | ||
let jsonData = try Data(contentsOf: dataPath) | ||
return try JSONDecoder().decode([WidgetFavorite].self, from: jsonData) | ||
} catch { | ||
log.error("loadWidgetData error: \(error)") | ||
return nil | ||
} | ||
} | ||
|
||
public static func updateWidgetData(_ favs: [WidgetFavorite]) { | ||
guard let rootPath = widgetDataRoot, let dataPath = widgetDataPath else { return } | ||
do { | ||
let widgetData = try JSONEncoder().encode(favs) | ||
try FileManager.default.createDirectory(atPath: rootPath.path, withIntermediateDirectories: true) | ||
try widgetData.write(to: dataPath) | ||
WidgetCenter.shared.reloadTimelines(ofKind: "FavoritesWidget") | ||
} catch { | ||
log.error("updateWidgetData error: \(error)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2021 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
extension Label where Title == Text, Icon == Image { | ||
public init<S>(_ title: S, uiImage: UIImage) where S: StringProtocol { | ||
self.init { | ||
Text(title) | ||
} icon: { | ||
Image(uiImage: uiImage) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
BraveWidgets/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0.169", | ||
"green" : "0.329", | ||
"red" : "0.984" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Oops, something went wrong.