This is a simple iOS app to integrate HealthKit to show some info for the user
This project is using cocoapods. Please be sure to run the pod install command before running the project.
If you have any doubt about cocoapods you can check the reference here.
- No internet connection message: If the user opens the app and it doesn't download the goals list before, no internet connection message appears (the app needs the goals information in order to determinate the user progress)
- Local goals cache: The app is using a local goals cache so if the device doesn't have internet connection, the app will use the local information
- Share option: You can share a screenshot for your daily progress
References:
Basically I have a protocol file for each scene in the app. This file defines the interaction between each layer as following:
- View - Presenter: protocols to notify changes and to inject information to the UI.
- Presenter - Interactor: protocols to request / receive information to / from the interactor.
- Presenter - Router: protocol to define the transitions between scenes (I skiped this protocols for the demo because I have only a scene there).
Whith this protocols file is really easy to know how each layer notify / request / information to the other ones so we don't have any other way to communicate all the layers.
Another important point is because I'm using protocols it's really easy to define mocks views / presenters / interactors / routers for testing.
// View / Presenter
protocol GoalsViewInjection : class {
func showProgress(_ show: Bool, status: String)
func showProgress(_ show: Bool)
func showMessageWith(title: String, message: String, actionTitle: String)
func loadGoals(_ viewModels: [GoalViewModel])
}
protocol GoalsPresenterDelegate : class {
func viewDidLoad()
func refresh()
}
// Presenter / Interactor
typealias GoalsGetGoalsCompletionBlock = (_ viewModel: [GoalViewModel]?, _ success: Bool, _ error: ResultError?) -> Void
protocol GoalsInteractorDelegate : class {
func getGoalsList(completion: @escaping GoalsGetGoalsCompletionBlock)
func clear()
}
I'm using the following endpoint to get the goals list -> https://thebigachallenge.appspot.com/_ah/api/myApi/v1/goals/.
public struct GoalsResponse: Decodable {
let items: [GoalResponse]
}
public struct GoalResponse: Decodable {
let id: String
let title: String
let description: String
let type: String
let goal: Int
let reward: RewardResponse
}
public struct RewardResponse: Decodable {
let trophy: String
let points: Int
}
I'm using a Swift Standard Library decodable functionality in order to manage a type that can decode itself from an external representation (I really β€ this from Swift).
Reference: Apple documentation
This model is used for the local goals
class LocalGoal: Object {
@objc dynamic var goalId: String?
@objc dynamic var title: String = ""
@objc dynamic var goalDescription: String = ""
@objc dynamic var type: String = ""
@objc dynamic var goal: Int = 0
@objc dynamic var trophy: String = ""
@objc dynamic var points: Int = 0
override class func primaryKey() -> String? {
return "goalId"
}
}
As I'm using Realm for this it's important to define a class to manage each model in the database. In this case we only have one model (LocalGoal)
Reference: Realm
I think using managers is a good idea but be careful!. Please don't create managers as if the world were going to end tomorrow.
I'm using only 4 here:
Used to share the current progress using UIActivityViewController.
Used to manage the reachability. In this case I would like to notify a little issue related with the simulator. It seems Xcode has an issue with the simulator because if you try to turn off the wifi and turning on again, the observer for the state change is not triggering. It's working 100% fine in a real device.
Used to save the goals locally using a Realm database.
Used to manage all the connections and requests with the HealthKit framework.
- Realm migration process: It would be nice to add a process to migrate the realm database to a new model (just in case you need to add a new field into the database).
- Localizable strings from server (goals list): It would be nice to have an option to retrieve the goals list with localizable strings according with the device language.
- Logic to refresh my goals screen every xx minutes: It's a good idea to have a logic to refresh my goals screen every xx minutes.
- Swift 4.2
- Xcode 10.1
- Cocoapods 1.5.3
- Minimun iOS version: 12.1
- RealmSwift (3.7.6): A mobile database that runs directly inside phones, tablets or wearables
- SVProgressHUD (2.2.5): A clean and lightweight progress HUD for your iOS and tvOS app.
- XYPieChart (0.2): A library to create pie charts for iOS and MacOS.
You can contact me using my email: [email protected]
Follow me @rcasanovan on twitter.