Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
[Feature] #65 Scroll to top when Chart category changed (#67)
Browse files Browse the repository at this point in the history
<!--- 
Pull Request title must contains Category, issue number, issue
description
(e.g. "[Feature] #123 Adding new Gestures")
-->

### Describe
New feature when changing category 

### Changes Made
Scroll automatically go to top when category changed


### Issues Resolved
- #65 
<!-- 
If this pull request addresses or closes any related issues, mention
them here. Use the GitHub issue linking format
(e.g., "Feature #123"). 
-->

### Additional context
Issue related to Refresh control is found & created:
- #66
  • Loading branch information
ChoiysApple authored Jan 2, 2024
2 parents 4534b47 + dc0598a commit ce7515a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion MovieApp/MovieApp/Domain/Service/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class APIService {

let task = URLSession(configuration: .default).dataTask(with: urlString) { (data, response, error) in

print("Request \(urlString.absoluteString)")
print("[Request] \(urlString.absoluteString)")
if let error = error {
print("Error: \(error.localizedDescription)")
onComplete(.failure(error))
Expand All @@ -42,11 +42,14 @@ class APIService {
return Observable.create { emitter in

fetchRequest(url: url, retries: retries) { result in
print("---------------------------\n[Response] \(url)")
switch result {
case .success(let data):
print("Success \(String(decoding: data, as: UTF8.self))")
emitter.onNext(data)
emitter.onCompleted()
case .failure(let error):
print("Error: \(error.localizedDescription)")
emitter.onError(error)
}
}
Expand Down
3 changes: 0 additions & 3 deletions MovieApp/MovieApp/Domain/Utils/Cache/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ final class Cache<Key: Hashable, Value> {
func insertValue(_ value: Value, forKey key: Key) {
let entry = Entry(value)
data.setObject(entry, forKey: WrappedKey(key))
print("\(String(describing: Value.self)) data Cached for key: \(key)")
}

/**
Expand All @@ -34,7 +33,6 @@ final class Cache<Key: Hashable, Value> {
*/
func value(forKey key: Key) -> Value? {
let entry = data.object(forKey: WrappedKey(key))
print("Get cached data type: \(String(describing: Value.self))\n value: \(String(describing: entry?.value))")
return entry?.value
}

Expand All @@ -44,7 +42,6 @@ final class Cache<Key: Hashable, Value> {
*/
func removeValue(forKey key: Key) {
data.removeObject(forKey: WrappedKey(key))
print("\(String(describing: Value.self)) data removed for key: \(key)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ class ChartViewController: UIViewController {

self.view.backgroundColor = UIColor(named: UIColor.background)
self.view.addSubview(tableView)
self.tableView.separatorStyle = .none
self.tableView.dataSource = self
self.tableView.delegate = self

// FIXME: Refresh Control is not woking propery because of Scroll to Top action. Refactoring required
/*
let customRefreshControl = UIRefreshControl().then { $0.tintColor = .white }
tableView.refreshControl = customRefreshControl
tableView.refreshControl?.addTarget(self, action: #selector(refreshData), for: .valueChanged)
*/

// Navigation
navigationController?.navigationBar.scrollEdgeAppearance = navigationAppearance
Expand All @@ -71,9 +75,9 @@ class ChartViewController: UIViewController {

// Category Menu
let categoryMenuItem = [
UIAction(title: "Popular", image: UIImage(systemName: "flame.fill"), handler: { _ in self.viewModel.requestData(category: .popular) }),
UIAction(title: "Top Rated", image: UIImage(systemName: "star.fill"), handler: { _ in self.viewModel.requestData(category: .topRated) }),
UIAction(title: "Now Playing", image: UIImage(systemName: "theatermasks.fill"), handler: { _ in self.viewModel.requestData(category: .nowPlaying) })
UIAction(title: "Popular", image: UIImage(systemName: "flame.fill"), handler: { _ in self.onCategoryChanged(.popular) }),
UIAction(title: "Top Rated", image: UIImage(systemName: "star.fill"), handler: { _ in self.onCategoryChanged(.topRated) }),
UIAction(title: "Now Playing", image: UIImage(systemName: "theatermasks.fill"), handler: { _ in self.onCategoryChanged(.nowPlaying) })
]
let categoryMenu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: categoryMenuItem)

Expand All @@ -92,7 +96,7 @@ class ChartViewController: UIViewController {
private func bindData() {
viewModel.movieListData
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] _ in
.subscribe(onNext: { [weak self] list in
self?.tableView.reloadData()
})
.disposed(by: disposeBag)
Expand Down Expand Up @@ -137,6 +141,16 @@ extension ChartViewController: UITableViewDelegate {

}

// MARK: UIMenu Callback
extension ChartViewController {
private func onCategoryChanged(_ category: MovieListCategory) {
guard viewModel.currentCategory != category else { return }

self.viewModel.requestData(category: category)
self.tableView.setContentOffset(CGPoint.zero, animated: true)
}
}

// MARK: Scroll Features
extension ChartViewController {

Expand All @@ -153,10 +167,11 @@ extension ChartViewController {
}

if category != nil { self.viewModel.refreshData() }
self.tableView.refreshControl?.endRefreshing()

}

}

// MARK: Scroll Delegate methods
extension ChartViewController {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
Expand All @@ -165,5 +180,4 @@ extension ChartViewController {
viewModel.requestMoreData()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ extension DetailViewController {
extension DetailViewController: UIGestureRecognizerDelegate {

@objc private func backButtonAction() {
print(#function)
self.navigationController?.popViewController(animated: true)
}

Expand Down

0 comments on commit ce7515a

Please sign in to comment.