Skip to content
bishopmatthew edited this page Jan 31, 2013 · 2 revisions

Activities Overview

Activities Overview

Hacker News 2 is built from a combination of Activities and Fragments. I've used Activities where I was confident something would be stand-alone on both phones and tablets, and Fragments for ViewPagers and layouts that are combined on tablets.

Downloading and caching data

To get data from http://news.ycombinator.com, classes in the parser package use jsoup to GET the html pages and transform them into a java data structure. Then I transform it into a list of Stories or Comments, which are extensions of the SQLObject class, which is a really simple ORM I wrote that uses reflection. (By the way, if anyone wants to rewrite this with one of the (much better) ORMs now available for Android, please do!) This data is then cached, along with a Timestamp of when it was downloaded.

Loaders

Most Activities and Fragments involve loading some data either the cache database (if available) or from the network. In either case, the actual work is done on a background thread using a Loader. Loaders have the benefit (as compared to AsyncTasks) of being persistent across orientation changes.

StoryLoader uses static methods grouped in StoryParser to download and parse the html from the websites "front", "new", "ask", "best", and a user's "submissions" pages. After the data is downloaded and parsed, it is cached into a SQLite database using my really simple ORM, DatabaseUtils.

Similarly, CommentsLoader and ThreadsLoader use CommentsParser to download and parse "comments" and "threads" pages.

AboutUserLoader gets data from a user's "about" page using UserParser

SearchLoader uses the HNSearch API get data from JSON and transform it into SearchItems. These are then displayed in SearchActivity.

SubmitLoader and ReplyLoader are used to send data to the website (or will send you to the login page if you aren't logged in already).

LoginLoader attempts to login with the username and password provided to it. If it's successful, it saves the username, password, and the auth cookie to SharedPreferences.

AsyncLoginUpdater and AsyncVotingService are fire and forget background tasks for updating the user's authentication cookie and casting votes, respectively.

Clone this wiki locally