CityWiseAQI
This app is using web-socket for realtime updation of aqi for dedicated inidan cities.
We're following MVVM architecture in this app. This app has two modules:-
- Listing of All Cities
- AQI Chart for selcted City
1. Listing of All Cities
ViewModel:-
- Here App is establishing connection with WebSocket server. For that, app is using one library(Starscream).
- Once App is connected to the server, App start getting realtime data and keep parsing the data into a desired model called AirQualityInfo.
- I have created two more models called City & AQI.
- I have created array of unique Cities(if any city already exists then keep updating the past history of aqi),
- While updating the past history of aqi in a city, we add first two values of aqi directly and then we only add aqi values after 30 seconds(because in chart we're showing aqi on 30 seconds interval).
- City updation task we're doing on an internalQueue with .barrier flag, to get better ui performance.
- ViewModel builds tableview models. Once done ViewModel updates to ViewController to update the UI.
- We also check if ViewModel has any selected city delegate, if yes then ViewModel also pass the updated aqi to the ViewModel of CityAqiDetails.
- If WebSocket occurs any error then ViewModel also updates to the ViewController for retry connection.
ViewController:-
- ViewController is listening events from ViewModel and updating the ui.
- For better outcome, app is disconnecting from web-socket when enters on background and reconnecting to the web-socket when enters on foreground.
- If WebSocket occurs any error then showing a alert to user for retry connection.
Model:-
- AirQualityInfo:- it represents data coming from web-socket server.
- City:- This is for tableview cell, it has all required info for table cell, to make table cell light weight.
- AQI:- This is for chart, on the basis of last record time of AQI model, we're checking the differnece, if it is equal to or greater than 30 seconds then only we update the chart with next AQI.
2. AQI Chart for selcted City
ViewModel:-
- This ViewModel is confirming to the delegate of city-listing-ViewModel to get the real time udpated aqi.
- We're adding first two aqi objects into records to shat chart doesn't look odd, after that we're checking the differnece of updated aqi & last recorded aqi, if it is equal to or greater than 30 seconds then only we recoed the updated one and update the chart.
- We're maintaining records of 25 aqi, if aqi records reaches to 25, we're removing the first added aqi from records.
- On the basis of aqiRecords, we're making LineChartDataSet for Charts. Once done we pass to the ViewController to update the chart.
ViewController:-
- For chart, We're using one library called Charts.
- We're using line chart here.
- once ViewController recives the updated LineChartDataSet, chart has been udpated.
We also have one enum called AQILevel, which has all the required methods like:-
- getAqiLevel():- to get to know the level of AQI.
- getDesc():- for displaying the AQILevel.
- getColor():- to get the dedicated color of an AQILevel.
- getFace():- in our UI we're shpwing few emoji, to explain the AQILevel in a better way, so this function is for this purpose.
Time Invested:-
- Socket: 4 hours
- Cities listing: 2 hours
- Charts:- 3 hours
- Project setup & Handling of use cases:- 1 hour
Overall around 10 hours have been invested on it.