<< Go Back To MyRuns Document

Database Implementation

This section discusses the design and implementation of the database.

Data structure

ExerciseEntry is the core data structure of the app. It defines what information a workout entry should have -- we use the term workout and exercise interchangeably in this document. It can be defined as below.

id: Long //Primary Key
inputType: Int  // Manual, GPS or automatic
activityType: Int    // Running, cycling etc. 
dateTime: Calendar    // When does this entry happen
duration: Int       // Exercise duration in seconds
distance: Float      // Distance traveled. Either in meters or feet.   
avgPace: Float       // Average pace
avgSpeed: Float     // Average speed
calorie: Float        // Calories burnt
climb: Float         // Climb. Either in meters or feet.
heartRate: Int       // Heart rate
comment: String       // Comments
locationList: ArrayList<LatLng>  // Location list

You need to implement methods to set/get these attributes. For example, you need to implement methods to convert locationList to byte array to save in the database and convert byte array to array list when retrieving the location list. MyRuns3 does not require you to store the location data.

The id is the primary key. In database, the primary key uniquely identifies a record. For this course, the value of id needs to be set automatically and incrementally. The field locationList stores all the GPS coordinates. We use BLOB to save all the coordinates. A BLOB value is a blob of data, stored exactly as it was input. (see here). Hint: you need to convert the list to a byte array when you save the record, and convert the byte array to the list when you read it from the database.

Implement Database Operations

The design principle of the database operations is to hide database operation details from app's other modules. That is, other modules, e.g. history tab, do not need to operate the database directly to get data entries from or save data entries to the database. Use RoomDatabase, Dao, and repository to implement your database. When the history tab is loaded, your code should get the list of all entries. Then the list can be bound to an adapter so that the list view can show the entries. When the user selects an entry in the history tab, the app should get the entry’s details and display them either in a display activity (if it is a manual entry) or in the map (if it is not a manual entry). If an entry is generated, your app should insert it to the database.