-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented choosing a station from the list
- Loading branch information
Showing
7 changed files
with
225 additions
and
12 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
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
59 changes: 59 additions & 0 deletions
59
SmogWatch WatchKit Extension/SelectionListController.swift
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 @@ | ||
// | ||
// SelectionListController.swift | ||
// SmogWatch WatchKit Extension | ||
// | ||
// Created by Kuba Suder on 13.06.2020. | ||
// Copyright © 2020 Kuba Suder. Licensed under WTFPL license. | ||
// | ||
|
||
import WatchKit | ||
|
||
struct SelectionListContext { | ||
let items: [Station] | ||
let selectedId: Int? | ||
let onSelect: ((Station) -> ()) | ||
} | ||
|
||
class SelectionListController: WKInterfaceController { | ||
@IBOutlet weak var table: WKInterfaceTable! | ||
|
||
var selectedRowIndex: Int? = nil | ||
var items: [Station] = [] | ||
var selectionHandler: ((Station) -> ())? | ||
|
||
override func awake(withContext context: Any?) { | ||
let context = context as! SelectionListContext | ||
|
||
items = context.items | ||
selectionHandler = context.onSelect | ||
|
||
table.setNumberOfRows(items.count, withRowType: "SelectionListRow") | ||
|
||
for i in 0..<items.count { | ||
let row = table.rowController(at: i) as! SelectionListRow | ||
row.setTitle(items[i].name) | ||
} | ||
|
||
if context.selectedId != nil { | ||
if let index = items.firstIndex(where: { $0.channelId == context.selectedId }) { | ||
selectedRowIndex = index | ||
listRowController(at: index).setCheckmarkVisible(true) | ||
} | ||
} | ||
} | ||
|
||
override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) { | ||
if let previous = selectedRowIndex, previous != rowIndex { | ||
listRowController(at: previous).setCheckmarkVisible(false) | ||
} | ||
|
||
listRowController(at: rowIndex).setCheckmarkVisible(true) | ||
selectedRowIndex = rowIndex | ||
|
||
selectionHandler?(items[rowIndex]) | ||
} | ||
|
||
func listRowController(at index: Int) -> SelectionListRow { | ||
return table.rowController(at: index) as! SelectionListRow | ||
} | ||
} |
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,22 @@ | ||
// | ||
// SelectionListRow.swift | ||
// SmogWatch WatchKit Extension | ||
// | ||
// Created by Kuba Suder on 13.06.2020. | ||
// Copyright © 2020 Kuba Suder. Licensed under WTFPL license. | ||
// | ||
|
||
import WatchKit | ||
|
||
class SelectionListRow: NSObject { | ||
@IBOutlet weak var titleLabel: WKInterfaceLabel! | ||
@IBOutlet weak var checkmark: WKInterfaceLabel! | ||
|
||
func setTitle(_ title: String) { | ||
titleLabel.setText(title) | ||
} | ||
|
||
func setCheckmarkVisible(_ visible: Bool) { | ||
checkmark.setHidden(!visible) | ||
} | ||
} |
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