-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.swift
103 lines (80 loc) · 3.21 KB
/
Question.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Question.swift
// Kapıştır
//
// Created by Evren Yortuçboylu on 13/03/16.
// Copyright © 2016 Evren Yortuçboylu. All rights reserved.
//
import Foundation
import UIKit
class Question {
var id: String
var optionA: String
var optionB: String
var optionACount: Int
var optionBCount: Int
var skipCount: Int
var askerName: String?
var askerUserId: String?
var askerProfileImage: String?
var answer: Answer?
var isAnswered: Bool = false
var isFollowed: Bool = false
var friends: [User]?
var totalAnswerCount: Int {
return optionACount + optionBCount
}
var ratioA: Double {
return totalAnswerCount > 0 ? Double(optionACount) / Double(totalAnswerCount) : 0
}
var ratioB: Double {
return totalAnswerCount > 0 ? Double(optionBCount) / Double(totalAnswerCount) : 0
}
var ratioSkip: Double {
return totalAnswerCount > 0 ? Double(skipCount) / Double(totalAnswerCount) : 0
}
init(id: String, optionA: String, optionB: String,
optionACount:Int, optionBCount:Int, skipCount: Int,
askerName: String?, askerUserId: String?, askerProfileImage: String?, friends: [User]?)
{
self.id = id
self.optionA = optionA
self.optionB = optionB
self.optionACount = optionACount
self.optionBCount = optionBCount
self.skipCount = skipCount
self.askerName = askerName
self.askerUserId = askerUserId
self.askerProfileImage = askerProfileImage
self.friends = friends
}
}
extension Question {
static func fromApiResponse(response: NSDictionary) -> Question {
// api reponse'dan gelen json ile instance oluştur
let id = response["id"] as! String
let optionA = response["option_a"] as! String
let optionB = response["option_b"] as! String
let optionACount = response["option_a_count"] as! Int
let optionBCount = response["option_b_count"] as! Int
let skipCount = response["skip_count"] as! Int
let askerName = response["asker_name"] as? String
let askerProfileImage = response["asker_profile_img"] as? String
let askerUserId = response["user_id"] as? String
var friends = [User]()
if let friendsJson = response["friends"] as? [AnyObject] {
for j in friendsJson {
let userName = j["name"] as! String
let userId = j["facebook_id"] as! String
let profileImage = j["profile_img"] as! String
let option = Answer(rawValue: j["option"] as! String)
friends.append(User(userName: userName, userId: userId, profileImageUrl: profileImage, facebookId: userId, profileImage: nil, questions: [], userVotedOption: option))
}
}
let question = Question(
id: id, optionA: optionA, optionB: optionB, optionACount: optionACount, optionBCount: optionBCount, skipCount: skipCount,
askerName: askerName, askerUserId: askerUserId, askerProfileImage: askerProfileImage, friends: friends
)
return question
}
}