diff --git a/LearnEnglish/LearnEnglish/Views/CategoryListViewController.swift b/LearnEnglish/LearnEnglish/Views/CategoryListViewController.swift index fa88a5a..446564b 100644 --- a/LearnEnglish/LearnEnglish/Views/CategoryListViewController.swift +++ b/LearnEnglish/LearnEnglish/Views/CategoryListViewController.swift @@ -53,7 +53,12 @@ final class CategoryListViewController: UITableViewController { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { Haptics.selection() let category = store.categories[indexPath.row] - navigationController?.pushViewController( - WordListViewController(category: category, colorIndex: indexPath.row), animated: true) + if let subs = category.subcategories, !subs.isEmpty { + let vc = SubcategoryListViewController(category: category, colorIndex: indexPath.row) + navigationController?.pushViewController(vc, animated: true) + } else { + navigationController?.pushViewController( + WordListViewController(category: category, colorIndex: indexPath.row), animated: true) + } } } diff --git a/LearnEnglish/LearnEnglish/Views/SubcategoryListViewController.swift b/LearnEnglish/LearnEnglish/Views/SubcategoryListViewController.swift new file mode 100644 index 0000000..fde4d46 --- /dev/null +++ b/LearnEnglish/LearnEnglish/Views/SubcategoryListViewController.swift @@ -0,0 +1,50 @@ +import UIKit + +final class SubcategoryListViewController: UITableViewController { + private let category: WordCategory + private let colorIndex: Int + private let subcategories: [Subcategory] + + init(category: WordCategory, colorIndex: Int) { + self.category = category + self.colorIndex = colorIndex + self.subcategories = category.subcategories ?? [] + super.init(style: .plain) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { fatalError() } + + override func viewDidLoad() { + super.viewDidLoad() + title = category.name + navigationItem.largeTitleDisplayMode = .never + tableView.register(CategoryCardCell.self, forCellReuseIdentifier: "cell") + tableView.separatorStyle = .none + tableView.backgroundColor = .systemBackground + tableView.contentInset.top = 8 + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + subcategories.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CategoryCardCell + let sub = subcategories[indexPath.row] + cell.configure(with: makeSynthetic(sub), color: Theme.Color.category(at: colorIndex)) + return cell + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + Haptics.selection() + let sub = subcategories[indexPath.row] + navigationController?.pushViewController( + WordListViewController(category: makeSynthetic(sub), colorIndex: colorIndex), animated: true) + } + + private func makeSynthetic(_ sub: Subcategory) -> WordCategory { + WordCategory(id: sub.name, name: sub.name, icon: category.icon, + words: sub.words, subcategories: nil) + } +}