forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Today widget to MVVM architecture : viewModel and model file…
…s added (mozilla-mobile#6864)
- Loading branch information
1 parent
2176a27
commit 74091a2
Showing
8 changed files
with
258 additions
and
28 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,56 @@ | ||
/* 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 UIKit | ||
|
||
class ButtonWithSublabel: UIButton { | ||
lazy var subtitleLabel = UILabel() | ||
lazy var label = UILabel() | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
convenience init() { | ||
self.init(frame: .zero) | ||
} | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
performLayout() | ||
} | ||
|
||
fileprivate func performLayout() { | ||
let buttonImage = self.imageView! | ||
self.titleLabel?.removeFromSuperview() | ||
addSubview(self.label) | ||
addSubview(self.subtitleLabel) | ||
|
||
buttonImage.snp.makeConstraints { make in | ||
make.centerY.left.equalTo(10) | ||
make.width.equalTo(TodayUX.copyLinkImageWidth) | ||
} | ||
|
||
self.label.snp.makeConstraints { make in | ||
make.left.equalTo(buttonImage.snp.right).offset(10) | ||
make.trailing.top.equalTo(self) | ||
make.height.greaterThanOrEqualTo(15) | ||
} | ||
self.label.numberOfLines = 1 | ||
self.label.lineBreakMode = .byWordWrapping | ||
|
||
self.subtitleLabel.lineBreakMode = .byTruncatingTail | ||
self.subtitleLabel.snp.makeConstraints { make in | ||
make.bottom.equalTo(self).inset(10) | ||
make.top.equalTo(self.label.snp.bottom).offset(3) | ||
make.leading.trailing.equalTo(self.label) | ||
make.height.greaterThanOrEqualTo(10) | ||
} | ||
} | ||
|
||
override func setTitle(_ text: String?, for state: UIControl.State) { | ||
self.label.text = text | ||
super.setTitle(text, for: state) | ||
} | ||
} |
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,47 @@ | ||
/* 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 UIKit | ||
|
||
class ImageButtonWithLabel: UIView { | ||
lazy var button = UIButton() | ||
lazy var label = UILabel() | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
performLayout() | ||
} | ||
|
||
fileprivate func performLayout() { | ||
addSubview(button) | ||
addSubview(label) | ||
button.imageView?.contentMode = .scaleAspectFit | ||
|
||
button.snp.makeConstraints { make in | ||
make.centerX.equalTo(self) | ||
make.top.equalTo(self.safeAreaLayoutGuide).offset(5) | ||
make.right.greaterThanOrEqualTo(self.safeAreaLayoutGuide).offset(40) | ||
make.left.greaterThanOrEqualTo(self.safeAreaLayoutGuide).inset(40) | ||
make.height.greaterThanOrEqualTo(60) | ||
} | ||
|
||
label.snp.makeConstraints { make in | ||
make.top.equalTo(button.snp.bottom).offset(10) | ||
make.leading.trailing.bottom.equalTo(self) | ||
make.height.greaterThanOrEqualTo(10) | ||
} | ||
|
||
label.numberOfLines = 1 | ||
label.lineBreakMode = .byWordWrapping | ||
label.textAlignment = .center | ||
} | ||
|
||
func addTarget(_ target: AnyObject?, action: Selector, forControlEvents events: UIControl.Event) { | ||
button.addTarget(target, action: action, for: events) | ||
} | ||
} |
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 @@ | ||
/* 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 | ||
|
||
struct TodayModel { | ||
static var copiedURL: URL? | ||
|
||
var scheme: String { | ||
guard let string = Bundle.main.object(forInfoDictionaryKey: "MozInternalURLScheme") as? String else { | ||
// Something went wrong/weird, but we should fallback to the public one. | ||
return "firefox" | ||
} | ||
return string | ||
} | ||
} |
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,30 @@ | ||
/* 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 UIKit | ||
|
||
struct TodayUX { | ||
static let backgroundHightlightColor = UIColor(white: 216.0/255.0, alpha: 44.0/255.0) | ||
static let linkTextSize: CGFloat = 9.0 | ||
static let labelTextSize: CGFloat = 12.0 | ||
static let imageButtonTextSize: CGFloat = 13.0 | ||
static let copyLinkImageWidth: CGFloat = 20 | ||
static let margin: CGFloat = 8 | ||
static let buttonsHorizontalMarginPercentage: CGFloat = 0.1 | ||
static let buttonStackViewSpacing: CGFloat = 20.0 | ||
static var labelColor: UIColor { | ||
if #available(iOS 13, *) { | ||
return UIColor(named: "widgetLabelColors") ?? UIColor(rgb: 0x242327) | ||
} else { | ||
return UIColor(rgb: 0x242327) | ||
} | ||
} | ||
static var subtitleLabelColor: UIColor { | ||
if #available(iOS 13, *) { | ||
return UIColor(named: "subtitleLableColor") ?? UIColor(rgb: 0x38383C) | ||
} else { | ||
return UIColor(rgb: 0x38383C) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.