Skip to content

Commit

Permalink
Modified the functions and now the app works, it just needs some poli…
Browse files Browse the repository at this point in the history
…shing to move some business logic from view to presenter.
  • Loading branch information
iNoor72 committed Dec 5, 2021
1 parent 8d023b2 commit 9a013f4
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 43 deletions.
24 changes: 24 additions & 0 deletions iOS-VIPER-Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
7C74A88A2711B6AF0087F42A /* NewsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C74A8892711B6AF0087F42A /* NewsViewModel.swift */; };
7C74A88C2711B6E10087F42A /* NewsProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C74A88B2711B6E10087F42A /* NewsProtocols.swift */; };
7C74A88E2711BA450087F42A /* NetworkRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C74A88D2711BA450087F42A /* NetworkRouter.swift */; };
7C8500B7275D62CE0089397C /* Details.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7C8500B6275D62CE0089397C /* Details.storyboard */; };
7C8500BA275D63B80089397C /* NewsViewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C8500B9275D63B80089397C /* NewsViewController+Extensions.swift */; };
7C9675F127109D17005E99A9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9675F027109D17005E99A9 /* AppDelegate.swift */; };
7C9675F327109D17005E99A9 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9675F227109D17005E99A9 /* SceneDelegate.swift */; };
7C9675F527109D17005E99A9 /* NewsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C9675F427109D17005E99A9 /* NewsViewController.swift */; };
Expand All @@ -39,6 +41,8 @@
7C74A8892711B6AF0087F42A /* NewsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsViewModel.swift; sourceTree = "<group>"; };
7C74A88B2711B6E10087F42A /* NewsProtocols.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsProtocols.swift; sourceTree = "<group>"; };
7C74A88D2711BA450087F42A /* NetworkRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkRouter.swift; sourceTree = "<group>"; };
7C8500B6275D62CE0089397C /* Details.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Details.storyboard; sourceTree = "<group>"; };
7C8500B9275D63B80089397C /* NewsViewController+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NewsViewController+Extensions.swift"; sourceTree = "<group>"; };
7C9675ED27109D17005E99A9 /* iOS-VIPER-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iOS-VIPER-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7C9675F027109D17005E99A9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7C9675F227109D17005E99A9 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -93,6 +97,22 @@
path = Model;
sourceTree = "<group>";
};
7C8500B5275D62B70089397C /* NewsDetails */ = {
isa = PBXGroup;
children = (
7C8500B6275D62CE0089397C /* Details.storyboard */,
);
path = NewsDetails;
sourceTree = "<group>";
};
7C8500B8275D63A50089397C /* Extensions */ = {
isa = PBXGroup;
children = (
7C8500B9275D63B80089397C /* NewsViewController+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
7C9675E427109D17005E99A9 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -130,6 +150,7 @@
7C96760A2710A7C0005E99A9 /* Scenes */ = {
isa = PBXGroup;
children = (
7C8500B5275D62B70089397C /* NewsDetails */,
7C96760B2710A7C8005E99A9 /* News */,
);
path = Scenes;
Expand All @@ -138,6 +159,7 @@
7C96760B2710A7C8005E99A9 /* News */ = {
isa = PBXGroup;
children = (
7C8500B8275D63A50089397C /* Extensions */,
7C96760C2710A7D7005E99A9 /* NewsCell */,
7C9675F627109D17005E99A9 /* Main.storyboard */,
7C74A8852711B66F0087F42A /* NewsEntity.swift */,
Expand Down Expand Up @@ -235,6 +257,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7C8500B7275D62CE0089397C /* Details.storyboard in Resources */,
7C9676102710A7EA005E99A9 /* NewsCell.xib in Resources */,
7C9675FD27109D18005E99A9 /* LaunchScreen.storyboard in Resources */,
7C9675FA27109D18005E99A9 /* Assets.xcassets in Resources */,
Expand Down Expand Up @@ -301,6 +324,7 @@
7C74A88A2711B6AF0087F42A /* NewsViewModel.swift in Sources */,
7C74A88C2711B6E10087F42A /* NewsProtocols.swift in Sources */,
7C74A8822711B6520087F42A /* NewsPresenter.swift in Sources */,
7C8500BA275D63B80089397C /* NewsViewController+Extensions.swift in Sources */,
7C74A87D2711B48A0087F42A /* NetworkService.swift in Sources */,
7C74A8842711B65F0087F42A /* NewsRouter.swift in Sources */,
7C9675F327109D17005E99A9 /* SceneDelegate.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NewsViewController+Extensions.swift
// iOS-VIPER-Demo
//
// Created by Noor Walid on 05/12/2021.
//

import UIKit
import Kingfisher

extension NewsViewController: UITableViewDelegate, UITableViewDataSource {

//MARK: TableView Delegate & DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let count = presenter?.getNewsCount() else { return 0 }
return count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.newsCellID, for: indexPath) as! NewsCell

if let articleImage = presenter?.news?.articles[indexPath.row].image {
cell.articleImage.kf.setImage(with: URL(string: articleImage))
}
cell.articleTitle.text = presenter?.news?.articles[indexPath.row].title

return cell
}

}
4 changes: 3 additions & 1 deletion iOS-VIPER-Demo/Scenes/News/NewsCell/NewsCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import UIKit

class NewsCell: UITableViewCell {

@IBOutlet weak var articleImage: UIImageView!
@IBOutlet weak var articleTitle: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
Expand Down
36 changes: 11 additions & 25 deletions iOS-VIPER-Demo/Scenes/News/NewsCell/NewsCell.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="19162" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19144"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -23,41 +23,27 @@
<constraint firstAttribute="width" constant="72" id="tiJ-vo-jVM"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="BookName" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FsJ-TH-5DI">
<rect key="frame" x="112" y="14" width="193" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="AuthorName" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="OXH-Kh-wvX">
<rect key="frame" x="112" y="43" width="193" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="PublishDate" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="d2z-aG-2DZ">
<rect key="frame" x="112" y="72" width="193" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FsJ-TH-5DI">
<rect key="frame" x="106" y="20" width="193" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="d2z-aG-2DZ" firstAttribute="top" secondItem="OXH-Kh-wvX" secondAttribute="bottom" constant="8" id="1CQ-AG-aoO"/>
<constraint firstAttribute="trailing" secondItem="OXH-Kh-wvX" secondAttribute="trailing" id="EiQ-i9-Egw"/>
<constraint firstAttribute="trailing" secondItem="FsJ-TH-5DI" secondAttribute="trailing" id="IYz-vv-YFs"/>
<constraint firstItem="5n3-mf-4jV" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="14" id="S0F-oC-21V"/>
<constraint firstItem="FsJ-TH-5DI" firstAttribute="top" secondItem="5n3-mf-4jV" secondAttribute="top" id="XCY-o1-CPV"/>
<constraint firstItem="OXH-Kh-wvX" firstAttribute="leading" secondItem="5n3-mf-4jV" secondAttribute="trailing" constant="24" id="XIY-MO-WNU"/>
<constraint firstAttribute="trailing" secondItem="d2z-aG-2DZ" secondAttribute="trailing" id="XOf-by-Kl0"/>
<constraint firstItem="OXH-Kh-wvX" firstAttribute="top" secondItem="FsJ-TH-5DI" secondAttribute="bottom" constant="8" id="bc7-ue-ZeD"/>
<constraint firstItem="FsJ-TH-5DI" firstAttribute="leading" secondItem="5n3-mf-4jV" secondAttribute="trailing" constant="24" id="eKa-RE-Q76"/>
<constraint firstItem="d2z-aG-2DZ" firstAttribute="leading" secondItem="5n3-mf-4jV" secondAttribute="trailing" constant="24" id="fLf-XX-o4x"/>
<constraint firstItem="FsJ-TH-5DI" firstAttribute="leading" secondItem="5n3-mf-4jV" secondAttribute="trailing" constant="18" id="aMR-qf-h5a"/>
<constraint firstAttribute="trailing" secondItem="FsJ-TH-5DI" secondAttribute="trailing" constant="6" id="iVo-dN-IAe"/>
<constraint firstAttribute="bottom" secondItem="5n3-mf-4jV" secondAttribute="bottom" constant="14" id="nXE-Sf-2CM"/>
<constraint firstItem="5n3-mf-4jV" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="16" id="neT-bA-sJ3"/>
<constraint firstItem="FsJ-TH-5DI" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="20" id="vq9-SR-TRM"/>
</constraints>
</tableViewCellContentView>
<viewLayoutGuide key="safeArea" id="njF-e1-oar"/>
<connections>
<outlet property="articleImage" destination="5n3-mf-4jV" id="Bqr-6w-i6B"/>
<outlet property="articleTitle" destination="FsJ-TH-5DI" id="aGK-L1-FHb"/>
</connections>
<point key="canvasLocation" x="132.60869565217394" y="109.15178571428571"/>
</tableViewCell>
</objects>
Expand Down
8 changes: 5 additions & 3 deletions iOS-VIPER-Demo/Scenes/News/NewsInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import Foundation
import SwiftUI

class NewsInteractor: NewsInteractorInputProtocol {
var presenter: NewsPresetnerProtocol?
var presenter: NewsInteractorOutputProtocol?
var news: News?

func fetchNewsData(){
NetworkService.shared.fetchData {[weak self] (news: News?, error) in
if let error = error {
print(error.localizedDescription)
self?.presenter?.newsNotFetched(error: error)
}
self?.presenter?.news = news
self?.news = news
guard let safeNews = news else { return }
print("News from interactor. \(safeNews)")
self?.presenter?.newsFetchedSuccessfully(news: safeNews)
}
}

Expand Down
20 changes: 18 additions & 2 deletions iOS-VIPER-Demo/Scenes/News/NewsPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import Foundation

class NewsPresenter: NewsPresetnerProtocol {
class NewsPresenter: NewsPresetnerProtocol, NewsInteractorOutputProtocol {
var news : News?

weak var view: NewsViewProtocol?
private let router: NewsRouterProtocol
private let interactor: NewsInteractorInputProtocol
var newsCount: Int?

init(view: NewsViewProtocol, router: NewsRouterProtocol, interactor: NewsInteractorInputProtocol) {
self.view = view
Expand All @@ -21,9 +22,24 @@ class NewsPresenter: NewsPresetnerProtocol {
}

func fetchNewsData() {
//data is not set in the presenter
interactor.fetchNewsData()
}


func newsFetchedSuccessfully(news: News) {
self.news = news
self.newsCount = news.articles.count
view?.reloadData()
print("News from presenter. \(news)")
}

func newsNotFetched(error: Error) {
print("Error from presenter. \(error.localizedDescription)")
}

func getNewsCount() -> Int {
return newsCount ?? 0
}


}
9 changes: 6 additions & 3 deletions iOS-VIPER-Demo/Scenes/News/NewsProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ protocol NewsViewProtocol: AnyObject {
var presenter: NewsPresetnerProtocol? { get set }
func showLoadingIndicator()
func hideLoadingIndicator()
func reloadData()
}


protocol NewsPresetnerProtocol {
var view: NewsViewProtocol? { get set }
var news: News? { get set }
func fetchNewsData()
func getNewsCount() -> Int
}

protocol NewsRouterProtocol {

}

protocol NewsInteractorInputProtocol {
var presenter: NewsPresetnerProtocol? { get set }
var presenter: NewsInteractorOutputProtocol? { get set }
func fetchNewsData()
}

protocol NewsInteractorOutputProtocol {

//It's the presenter, but can be used for different output
func newsFetchedSuccessfully(news: News)
func newsNotFetched(error: Error)
}
12 changes: 3 additions & 9 deletions iOS-VIPER-Demo/Scenes/News/NewsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class NewsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NewsViewProtocol {
class NewsViewController: UIViewController, NewsViewProtocol {

var presenter: NewsPresetnerProtocol?

Expand Down Expand Up @@ -37,15 +37,9 @@ class NewsViewController: UIViewController, UITableViewDelegate, UITableViewData
loadingIndicator.stopAnimating()
loadingIndicator.isHidden = true
}

//MARK: TableView Delegate & DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return presenter?.news?.articles.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.newsCellID, for: indexPath) as! NewsCell
return cell
func reloadData() {
self.tableView.reloadData()
}
}

30 changes: 30 additions & 0 deletions iOS-VIPER-Demo/Scenes/NewsDetails/Details.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17150" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17122"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="s0d-6b-0kx">
<objects>
<viewController id="Y6W-OH-hqX" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="5EZ-qb-Rvc">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="vDu-zF-Fre"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ief-a0-LHa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>

0 comments on commit 9a013f4

Please sign in to comment.