-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui for add symbols and rearange symbols
- Loading branch information
Showing
8 changed files
with
302 additions
and
100 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 |
---|---|---|
|
@@ -12,49 +12,51 @@ import DXFeedFramework | |
class AddSymbolsViewController: UIViewController { | ||
@IBOutlet var searchBar: UISearchBar! | ||
@IBOutlet var symbolsTableView: UITableView! | ||
|
||
static let kSymbolsKey = "kSymbolsKey" | ||
var symbols: [String]? = nil | ||
@IBOutlet var titleLabel: UILabel! | ||
|
||
var symbols = [String]() | ||
var selectedSymbols = Set<String>() | ||
var dataProvider = SymbolsDataProvider() | ||
|
||
override func viewDidLoad() { | ||
reloadData() | ||
selectedSymbols = Set(dataProvider.selectedSymbols) | ||
symbols = dataProvider.allSymbols.sorted() | ||
|
||
titleLabel.textColor = .text | ||
|
||
symbolsTableView.backgroundColor = .tableBackground | ||
symbolsTableView.separatorStyle = .none | ||
|
||
searchBar.searchTextField.textColor = .text | ||
searchBar.searchTextField.attributedPlaceholder = NSAttributedString( | ||
string: "Search symbol", | ||
attributes: [.foregroundColor: UIColor.text!.withAlphaComponent(0.5)] | ||
) | ||
searchBar.barTintColor = .tableBackground | ||
// searchBar.searchTextField.textColor = .text | ||
// searchBar.searchTextField.attributedPlaceholder = NSAttributedString( | ||
// string: "Search symbol", | ||
// attributes: [.foregroundColor: UIColor.text!.withAlphaComponent(0.5)] | ||
// ) | ||
// searchBar.barTintColor = .tableBackground | ||
super.viewDidLoad() | ||
view.backgroundColor = .tableBackground | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
DispatchQueue.global(qos: .background).async { | ||
// [weak self] in | ||
self.readIpf() | ||
DispatchQueue.global(qos: .background).async { [weak self] in | ||
self?.readIpf() | ||
} | ||
} | ||
|
||
func readIpf() { | ||
let reader = DXInstrumentProfileReader() | ||
do { | ||
let result = try reader.readFromFile(address: "https://demo:[email protected]/ipf?compression=zip") | ||
let result = try reader.readFromFile(address: "https://demo:[email protected]/ipf?TYPE=FOREX,STOCK&compression=zip") | ||
guard let result = result else { | ||
return | ||
} | ||
let stocksData = result.map { ipf in | ||
return ipf.symbol | ||
} | ||
DispatchQueue.main.async { | ||
self.saveLocalSymbols(symbols: stocksData) | ||
self.reloadData() | ||
self.reloadData(symbols: stocksData) | ||
} | ||
print(stocksData) | ||
} catch { | ||
} | ||
} | ||
|
@@ -63,36 +65,30 @@ class AddSymbolsViewController: UIViewController { | |
self.navigationController?.popViewController(animated: true) | ||
} | ||
|
||
func reloadData() { | ||
symbols = loadLocalSymbols()?.sorted() | ||
symbolsTableView.reloadData() | ||
} | ||
} | ||
|
||
extension AddSymbolsViewController { | ||
func loadLocalSymbols() -> [String]? { | ||
UserDefaults.standard.value(forKey: AddSymbolsViewController.kSymbolsKey) as? [String] | ||
@IBAction func addTouchUpInside(_ sender: UIButton) { | ||
dataProvider.addSymbols(selectedSymbols) | ||
dataProvider.allSymbols = symbols | ||
self.navigationController?.popViewController(animated: true) | ||
} | ||
|
||
func saveLocalSymbols(symbols: [String]) { | ||
if symbols.count > 0 { | ||
UserDefaults.standard.setValue(symbols, forKey: AddSymbolsViewController.kSymbolsKey) | ||
} | ||
func reloadData(symbols: [String]) { | ||
self.symbols = symbols.sorted() | ||
symbolsTableView.reloadData() | ||
} | ||
} | ||
|
||
extension AddSymbolsViewController: UITableViewDelegate, UITableViewDataSource { | ||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return symbols?.count ?? 0 | ||
return symbols.count | ||
} | ||
|
||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SymbolCellId", for: indexPath) | ||
as? SymbolCell else { | ||
return UITableViewCell() | ||
} | ||
let symbol = symbols?[indexPath.row] | ||
let value = symbol ?? "" | ||
cell.update(symbol: value, check: selectedSymbols.contains(value)) | ||
let symbol = symbols[indexPath.row] | ||
cell.update(symbol: symbol, check: selectedSymbols.contains(symbol)) | ||
return cell | ||
} | ||
|
||
|
@@ -101,14 +97,12 @@ extension AddSymbolsViewController: UITableViewDelegate, UITableViewDataSource { | |
} | ||
|
||
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { | ||
|
||
let symbol = symbols?[indexPath.row] | ||
let value = symbol ?? "" | ||
let checked = selectedSymbols.contains(value) | ||
let symbol = symbols[indexPath.row] | ||
let checked = selectedSymbols.contains(symbol) | ||
if checked { | ||
selectedSymbols.remove(value) | ||
selectedSymbols.remove(symbol) | ||
} else { | ||
selectedSymbols.insert(value) | ||
selectedSymbols.insert(symbol) | ||
} | ||
tableView.reloadRows(at: [indexPath], with: .none) | ||
return indexPath | ||
|
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.