I have to create a quiz of showing questions from a JSON and post back the answers.
Here's the structure of JSON:
{
"pp": [
{
"profile_property": {
"circle_value": 4,
"created_at": null,
"id": 1,
"name": "THIS IS THE QUESTION TO DISPLAY",
"updated_at": null,
"profile_property_values": [
{
"circle_value": 1,
"created_at": null,
"description": null,
"id": 1,
"profile_property_id": 1,
"standard": true,
"updated_at": "2014-01-24T13:34:47+05:30",
"value": "OPTION 1",
"image": "imageURL",
"sel_image": "selectedImageURL"
},
{
"circle_value": 2,
"created_at": null,
"description": null,
"id": 2,
"profile_property_id": 1,
"standard": true,
"updated_at": "2014-01-24T13:34:47+05:30",
"value": "OPTION @",
"image": "imageURL",
"sel_image": "selectedImageURL"
},
{
"circle_value": 3,
"created_at": null,
"description": null,
"id": 3,
"profile_property_id": 1,
"standard": true,
"updated_at": "2014-01-24T13:34:47+05:30",
"value": "OPTION #",
"image": "imageURL",
"sel_image": "selectedImageURL"
}
]
}
},
{
"profile_property": {},
{
"profile_property": {},
{
"profile_property": {}
],
"style_profile": {
}
}
}
My Attempt
I have created a loaderVC
which loads this JSON and creates model class. Then it pushes a quizVC
which shows one question at a time.
I have created a class which contains a static array
to which I append the answers.
I know this design is very poor in many ways, so please suggest an object oriented approach to solve this.
Here are my classes:
loaderVC
class QuizLoaderViewController: UIViewController {
var quizData: Quiz? {
didSet {
showQuiz()
}
}
override func viewDidLoad() {
super.viewDidLoad()
Answer.profileValueIds.removeAll(keepCapacity: true)
self.navigationController!.navigationBarHidden = true
getQuizQuestions()
}
func getQuizQuestions() {
var urlString = Constants.baseUrl + "/quiz_questions.json"
func onSuccess(jsonData: AnyObject) {
let jsonResult = JSON_SWIFTY(jsonData)
styleQuizData = StyleQuiz(fromJson: jsonResult)
}
func onFailure(error: NSError?) {
SwiftSpinner.hide()
println(error)
}
requestData(Method.GET, urlString, onSuccess, onFailure, self)
}
func showQuiz() {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let quizVC = storyboard.instantiateViewControllerWithIdentifier("quiz") as! StyleQuizViewController
quizVC.quizData = self.quizData
styleQuizVC.index = 0
self.navigationController!.pushViewController(quizVC, animated: false)
}
}
quizVC
class QuizViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
@IBOutlet weak var collectionViewStyle: UICollectionView!
var quizData : Quiz?
var profileProperty:ProfileProperty?
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
profileProperty = quizData?.pp[index].profileProperty
topLabel.text = profileProperty?.name
submitButton.hidden = true
setupView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func closeButton(sender: UIButton) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
//MARK: Scroll View Delegate Methods
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return profileProperty?.profilePropertyValues.count ?? 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
collectionView.registerNib(UINib(nibName: "StyleQuizValueCell", bundle: nil), forCellWithReuseIdentifier: "cellIdentifier")
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) as? StyleQuizValueCell
let value = profileProperty!.profilePropertyValues[indexPath.row]
cell?.imgView.image = UIImage(named: "preloader")
let url = NSURL(string: value.image)
cell!.imgView.hnk_setImageFromURL(url!)
cell!.valueLabel.textColor = UIColor.whiteColor()
cell!.valueLabel.text = value.value
return cell!
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
Answer.profileValueIds.append(profileProperty!.profilePropertyValues[indexPath.row].id)
let cell = collectionView.cellForItemAtIndexPath(indexPath)! as! StyleQuizValueCell
let value = profileProperty!.profilePropertyValues[indexPath.row]
let url = NSURL(string: value.selImage)
cell.imgView.hnk_setImageFromURL(url!)
if index < styleQuizData!.pp.count - 1 {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let styleQuizVC = storyboard.instantiateViewControllerWithIdentifier("styleQuiz") as! StyleQuizViewController
styleQuizVC.styleQuizData = self.styleQuizData
styleQuizVC.index = self.index+1
self.navigationController!.pushViewController(styleQuizVC, animated: true)
} else {
submitButton.hidden = false
}
}
@IBAction func goBack(sender: UIButton) {
self.navigationController?.popViewControllerAnimated(true)
}
@IBAction func submitAction(sender: UIButton) {
var urlString = Constants.baseUrl + "/style_profiles.json"
let parameters = [
"profile_property_value_ids": Answer.profileValueIds,
"style_profile": ""
]
func onSuccess(jsonData: AnyObject) {
println(jsonData)
var feedVC = self.navigationController?.viewControllers.first as! FeedViewController
feedVC.url = "/home"
// self.navigationController?.popToViewController(feedVC, animated: true)
performSegueWithIdentifier("toHome", sender: self)
}
func onFailure(error: NSError?) {
println(error)
}
requestData(Method.POST, urlString, onSuccess, onFailure, self, postParams: parameters)
}
}
Answer struct
struct Answer {
static var profileValueIds = [Int](count: 10, repeatedValue : 10)
}
I am very new to object oriented programming. Please help me make a better design.
requestData()
function? \$\endgroup\$