Today Widget Finished

This commit is contained in:
Chris Li 2016-07-26 14:13:08 -04:00
parent dff2471087
commit 7dc988a6d6
5 changed files with 85 additions and 29 deletions

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.7.544</string>
<string>1.7.643</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -59,6 +59,45 @@
</objects>
<point key="canvasLocation" x="1097" y="1698"/>
</scene>
<!--Table View Controller-->
<scene sceneID="Bdc-3z-wHY">
<objects>
<tableViewController id="UN7-Sp-113" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="K9Y-GV-eEk">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<sections>
<tableViewSection id="Y6v-WD-nDY">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="8kv-1M-Lve">
<rect key="frame" x="0.0" y="35" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="8kv-1M-Lve" id="Wgh-qo-MWk">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="r9e-gL-liU">
<rect key="frame" x="487" y="5" width="105" height="38.5"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="numberPad"/>
</textField>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
<connections>
<outlet property="dataSource" destination="UN7-Sp-113" id="o6W-R0-C63"/>
<outlet property="delegate" destination="UN7-Sp-113" id="Rga-Ke-onq"/>
</connections>
</tableView>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ics-PY-I9c" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1097" y="2364"/>
</scene>
<!--SettingTBVC-->
<scene sceneID="AlF-dM-FGj">
<objects>
@ -554,7 +593,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="oLJ-aA-Gy7" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1097" y="2388"/>
<point key="canvasLocation" x="1097" y="2915"/>
</scene>
</scenes>
<resources>

View File

@ -18,7 +18,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="rGU-qY-GzL">
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="rGU-qY-GzL">
<rect key="frame" x="0.0" y="0.0" width="320" height="100"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="elH-Eg-whL">

View File

@ -13,13 +13,11 @@ class TodayViewController: UIViewController, NCWidgetProviding, UICollectionView
@IBOutlet weak var collectionView: UICollectionView!
private var rowHeight: CGFloat = 110.0
private let hInset: CGFloat = 15.0
private let vInset: CGFloat = 10.0
private var itemHeight: CGFloat = 0.0
private var itemWidth: CGFloat = 0.0
private var itemSize = CGSizeZero
private var hasUpdate = false
private var hasUpdate = true
private var bookmarks = [NSDictionary]()
override func viewDidLoad() {
@ -28,15 +26,52 @@ class TodayViewController: UIViewController, NCWidgetProviding, UICollectionView
collectionView.delegate = self
updateData()
calculateItemSize(collectionViewWidth: collectionView.frame.width)
updateUI()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
updateData()
calculateItemSize(collectionViewWidth: collectionView.frame.width)
updateUI()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
calculateItemSize(collectionViewWidth: size.width)
updateUI()
}
// MARK: - Update & Calculation
func updateData() {
let defaults = NSUserDefaults(suiteName: "group.kiwix")
guard let bookmarks = defaults?.objectForKey("bookmarks") as? [NSDictionary] else {return}
hasUpdate = self.bookmarks != bookmarks
self.bookmarks = bookmarks
}
func updateUI() {
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
NCWidgetController.widgetController().setHasContent(bookmarks.count > 0, forWidgetWithBundleIdentifier: "self.Kiwix.Bookmarks")
}
func calculateItemSize(collectionViewWidth collectionViewWidth: CGFloat) {
let itemsPerRow = max(5, min(round(collectionViewWidth / 70), 10))
let itemWidth = (collectionViewWidth - (itemsPerRow + 1) * hInset) / itemsPerRow
let titles = bookmarks.flatMap({$0.objectForKey("title") as? String})
let labelHeights = titles.map({$0.heightWithConstrainedWidth(itemWidth, font: UIFont.systemFontOfSize(10.0, weight: UIFontWeightMedium))})
let labelMaxHeight = max(12.0, min((labelHeights.maxElement() ?? 12.0), 24.0))
let itemHeight = itemWidth + 2.0 + labelMaxHeight // itemHeight (1:1 ration) + label top spacing + label height
itemSize = CGSizeMake(itemWidth, itemHeight)
let rowCount = ceil(CGFloat(bookmarks.count) / CGFloat(itemsPerRow))
let collectionViewHeight = itemHeight * rowCount + hInset * rowCount
preferredContentSize = CGSizeMake(0, max(1, collectionViewHeight))
}
// MARK: - NCWidgetProviding
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
@ -45,31 +80,12 @@ class TodayViewController: UIViewController, NCWidgetProviding, UICollectionView
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
updateData()
calculateItemSize(collectionViewWidth: collectionView.frame.width)
updateUI()
completionHandler(hasUpdate ? .NewData : .NoData)
hasUpdate = false
}
func updateData() {
let defaults = NSUserDefaults(suiteName: "group.kiwix")
guard let bookmarks = defaults?.objectForKey("bookmarks") as? [NSDictionary] else {return}
hasUpdate = self.bookmarks != bookmarks
self.bookmarks = bookmarks
}
func updateUI() {
itemWidth = (collectionView.frame.width - 6 * hInset) / 5
let titles = bookmarks.flatMap({$0.objectForKey("title") as? String})
let labelHeights = titles.map({$0.heightWithConstrainedWidth(itemWidth, font: UIFont.systemFontOfSize(10.0, weight: UIFontWeightMedium))})
let labelMaxHeight = max(12.0, min((labelHeights.maxElement() ?? 12.0), 24.0))
itemHeight = itemWidth + 2.0 + labelMaxHeight // itemHeight (1:1 ration) + label top spacing + label height
let rowCount: CGFloat = ceil(CGFloat(bookmarks.count) / 5)
let collectionViewHeight = rowCount * itemHeight + vInset * (rowCount + 1)
preferredContentSize = CGSizeMake(collectionView.frame.width, collectionViewHeight)
collectionView.reloadData()
}
// MARK: - UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
@ -110,7 +126,7 @@ class TodayViewController: UIViewController, NCWidgetProviding, UICollectionView
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(itemWidth, itemHeight)
return itemSize
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {

View File

@ -8,6 +8,7 @@
import CoreData
import PSOperations
import NotificationCenter
class UpdateWidgetDataSourceOperation: Operation {
let context: NSManagedObjectContext
@ -34,7 +35,7 @@ class UpdateWidgetDataSourceOperation: Operation {
bookmarks.append(articleData)
}
defaults?.setObject(bookmarks, forKey: "bookmarks")
NCWidgetController.widgetController().setHasContent(bookmarks.count > 0, forWidgetWithBundleIdentifier: "self.Kiwix.Bookmarks")
finish()
}
}