Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Playlist/Playlist/PlaylistController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class PlaylistController {
let playlist = Playlist(name: name)
playlists.append(playlist)
}

// This methods returns a playlist from playlists instead of giving the ViewController direct access to private data
func playlist(at indexPath: IndexPath) -> Playlist {
return playlists[indexPath.row]
}

func delete(playlist: Playlist) {
guard let index = playlists.index(of: playlist) else { return }
Expand All @@ -33,5 +38,9 @@ class PlaylistController {

// MARK: Properties

var playlists = [Playlist]()
private var playlists = [Playlist]()
// Use a computed property to return the count. We do not want other classes to be able to access playlists directly.
var numberOfPlaylists: Int {
return playlists.count
}
}
10 changes: 6 additions & 4 deletions Playlist/Playlist/PlaylistTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class PlaylistTableViewController: UITableViewController {
// MARK: UITableViewDataSource/Delegate

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PlaylistController.shared.playlists.count
// Use the computed property instead of the now private array playlists
return PlaylistController.shared.numberOfPlaylists
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "playlistCell", for: indexPath)

let playlist = PlaylistController.shared.playlists[indexPath.row]
// Get the playlist from playlists without direct accessing the now private data.
let playlist = PlaylistController.shared.playlist(at: indexPath)
cell.textLabel?.text = playlist.name

if playlist.songs.count == 1 {
Expand All @@ -49,7 +51,7 @@ class PlaylistTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let playlist = PlaylistController.shared.playlists[indexPath.row]
let playlist = PlaylistController.shared.playlist(at: indexPath)
PlaylistController.shared.delete(playlist: playlist)
tableView.deleteRows(at: [indexPath], with: .fade)
}
Expand All @@ -60,7 +62,7 @@ class PlaylistTableViewController: UITableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPlaylistDetail",
let indexPath = tableView.indexPathForSelectedRow {
let playlist = PlaylistController.shared.playlists[indexPath.row]
let playlist = PlaylistController.shared.playlist(at: indexPath)
let songVC = segue.destination as? SongTableViewController
songVC?.playlist = playlist
}
Expand Down