Skip to content

Instantly share code, notes, and snippets.

View azamsharp's full-sized avatar

Mohammad Azam azamsharp

View GitHub Profile
extension ArticleListViewModel {
func get(completion :@escaping (ArticleListViewModel) -> ()) {
// Use a URL builder scheme here instead of just using the string URL
let url = URL(string :"https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0cf790498275413a9247f8b94b3843fd")!
// Instead of getArticles method webservice should be generic and return the requested
// object/objects of requested type
self.webservice.getArticles(url: url) { articles in
override func viewDidLoad() {
super.viewDidLoad()
self.webservice = Webservice()
self.viewModel = ArticleListViewModel(service: self.webservice)
self.title = self.viewModel.title
loadArticles2()
}
override func viewDidLoad() {
super.viewDidLoad()
self.webservice = Webservice()
self.viewModel = ArticleListViewModel(service: self.webservice)
self.title = self.viewModel.title
loadArticles2()
}
@azamsharp
azamsharp / .py
Created November 21, 2017 19:16
Reading and Writing Files Python
import json
filename = "helloworld.txt"
# reading the whole file at once
with open(filename) as file_object:
contents = file_object.read()
print(contents)
@azamsharp
azamsharp / .swift
Created November 27, 2017 14:43
SourceListViewModel and SourceViewModel
class SourceListViewModel : NSObject {
private(set) var sourceViewModels :[SourceViewModel] = [SourceViewModel]()
private var webservice :Webservice
init(webservice :Webservice) {
self.webservice = webservice
super.init()
@azamsharp
azamsharp / .swift
Created November 27, 2017 20:10
Source Model Class
class Source {
var id :String!
var name :String!
var description :String!
init?(dictionary :JSONDictionary) {
guard let id = dictionary["id"] as? String,
let name = dictionary["name"] as? String,
@azamsharp
azamsharp / .swift
Created November 27, 2017 20:11
Webservice loadSources Function
func loadSources(completion :@escaping ([Source]) -> ()) {
URLSession.shared.dataTask(with: sourcesURL) { data, _, _ in
if let data = data {
let json = try! JSONSerialization.jsonObject(with: data, options: [])
let sourceDictionary = json as! JSONDictionary
let dictionaries = sourceDictionary["sources"] as! [JSONDictionary]
@azamsharp
azamsharp / .swift
Created November 27, 2017 20:18
SourcesTableViewController
class SourcesTableViewController : UITableViewController {
private var webservice :Webservice!
private var sourceListViewModel :SourceListViewModel!
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
@azamsharp
azamsharp / .swift
Created November 27, 2017 20:25
SourceListViewModel
func addSource(source: SourceViewModel) {
self.sourceViewModels.append(source)
}
func source(at index:Int) -> SourceViewModel {
return self.sourceViewModels[index]
}
@azamsharp
azamsharp / .swift
Created November 27, 2017 20:27
SourceViewModels Private Set
class SourceListViewModel : NSObject {
private(set) var sourceViewModels :[SourceViewModel] = [SourceViewModel]()