Insider's Guide To Udacity Android Developer Nanodegree Part 2 |
Written by Nikos Vaggalis | |||||
Monday, 24 April 2017 | |||||
Page 3 of 4
PreferencesThe next chapter on Preferences is about interacting with the UI and its state by making choices typically by clicking on a range of checkboxes, and persisting those choices to disk.Android has encapsulated this common functionality into the Preference Fragment, available to any application and easily tweaked to adapt to any requirements at hand.Nevertheless, we'll be exclusively using SQLite for our data persistence needs, as such this chapter can be safely left off.
SQLite is designed to cater for more complex requirements, and we use it for manipulating the list which contains our Favourite movies.A movie gets tagged as favourite by clicking on the Star present in each movie's details screen, which depending on the state it currently occupies (on or off) either saves the movie to the database as a favourite or removes it from it if it's already in. In order to use a SQLite database we first have to implement the Contract, a clause that defines the tables and the colons for each table that is included in the database. In this case we just have a single table "favouritemovies" with a single column "moviedbid" that stores the uniquely identifying movie ID, retrieved from the initital call to the MovieDB API URL.
The rest of the chapter goes through performing SQL DDL and DML, which are hidden underneath the DBhelper class, but the focus quickly shifts from direct SQL manipulation to the indirect use of Content Providers, which in essense are Data Mappers that sit in between the application and the backend database. "The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. With Data Mapper the in-memory objects needn't know even that there's a database present; they need no SQL interface code, and certainly no knowledge of the database schema. (The database schema is always ignorant of the objects that use it.) Since it's a form of Mapper (473), Data Mapper itself is even unknown to the domain layer."
The question is then, why opt for another layer of indirection than go for straight forward and hassle free direct SQL ? The answer is that Content Providers allow developers to change the underlying data source without needing to change any code in the applications that access the content provider.Then, some important classes such as Loaders and Cursor Adapters use them exclusively, therefore if you want to use a Loader you have to make your data accessible through a content provider instance.
The steps for building a Content Provider are broken down to:
At this point, in order to make the application more flexible and efficient, I've started using Lambdas to wrap the SQL operations in:
Once that is done, Android Studio correctly identifies and works with Lambdas. The downside is that for it to work it requires a minSDK version of 24, something that rules out many older devices Including a LoaderSince everything database-related goes through the Content Provider, we also need a Loader and specifically a CursorLoader to wrap all database access in, in order to retrieve our Favourite movies list upon clicking on the corresponding menu option of the MainActivity. The last requirement to fulfil is making a movie's list of trailers and reviews accessible through its Details screen (ChildActivity).For this we need to make a call to the MovieDB API to retrieve the Trailers
and another one to retrieve the Reviews http://api.themoviedb.org/3/movie/127380/reviews?api_key=1777b338c5a5ffd1f89a472ca17ecac4 { id: 127380, page: 1, results: [ { id: "5835dce5c3a3682fad019c4a", author: "Reno", content: "**Before Nemo, a long ago another child url: "https://www.themoviedb.org/review/ } ], total_pages: 1, total_results: 1 } So we need to add two extra objects ReviewData and TrailerData to hold the reviews and the trailers instances, two extra Adapters, ReviewsAdapter and TrailersAdapter, and two RecyclerViews to host those Adapters.
|
|||||
Last Updated ( Monday, 20 November 2017 ) |