smacgregor / SimpleTwitterClient

A simple twitter client leveraging the twitter API
0 stars 0 forks source link

Week 3 Project: A Twitter Client that Allows You To Interact With Your Timeline #8

Open smacgregor opened 8 years ago

smacgregor commented 8 years ago

My app is complete, please review. /cc @codepathreview @codepath

Another fun project. I was butting my head for a while trying to get the relationships set up correctly using ActiveAndroid. It was particularly challenging because of the model class hierarchies GSON is generating on my behalf. I almost got ActiveAndroid working well when offline - so close but not in time for the 10pm deadline. The work can be found here and I plan on wrapping that up tomorrow.

Interesting Notes

  1. Created a separate TwitterManager to remove code smells from the activities. The TwitterManager is responsible for interacting with the Twitter client for network requests and for parsing the responses into Java objects which are then passed back to the activity.
  2. I created a set of listener interfaces to be implemented by the activity when making requests on the TwitterManager.
    public interface OnTimelineTweetsReceivedListener {
        void onTweetsReceived(List<Tweet> tweets);
        void onTweetsFailed(int statusCode, Throwable throwable);
    }

    public interface OnCurrentUserReceivedListener {
        void onUserReceived(User user);
        void onUserFailed(int statusCode, Throwable throwable);
    }

    public interface OnNewPostReceivedListener {
        void onPostCreated(Tweet tweet);
        void onPostFailed(int statusCode, Throwable throwable);
    }

    public interface OnTweetUpdatedListener {
        void onTweetUpdated(Tweet tweet);
        void onTweetUpdateFailed(int statusCode, Throwable throwable);
    }

This got a little verbose on the activity side as every method call but I liked how it allowed the activity to deal with Tweet and User objects and not worry about whether those objects are coming from the server or from the database. Does that pattern look ok to you?

Open Questions

For ActiveAndroid - I had to write some pretty squirrely (maybe even smelly?) code to get the many to one relationships working given the GSON generated model classes. In the following example an Entities model contains an array of media associated with a tweet.

    public static class Entities extends Model {

        List<TweetMedia> media;

        public Entities() {
            super();
        }

        public List<TweetMedia> getMedia() {
            //  if we haven't saved the model yet then use media otherwise
           //  use getMany and hit the db
            if (media == null) {
               return getMany(TweetMedia.class, "Entities");
            } else {
                return media;
            }
        }

        public final Long cascadeSave(Tweet tweet) {
            long retVal = save();
            if (media != null && media.size() > 0) {
                for (TweetMedia med : media) {
                    med.setEntities(this);
                    med.cascadeSave();
                }
            }
            return retVal;
        }
    }

When saving a tweet I have a cascading save method which calls Entities.cascadeSave. In order to get the relationships set correctly - I saved the current entities object first. Then to set up the many to one relationship - enumerated over all the tweet media objects - setting the current entitites object on each tweet media model then saving the media object in the media table.

From the if I had more time department

codepathreview commented 8 years ago

:+1: nice work. A few notes after checking out the code:

Here's a detailed Project 3 Feedback Guide here which covers the most common issues with this submitted project. Read through the feedback guide point-by-point to determine how you could improve your submission.

Let us know if you have any other thoughts or questions about this assignment. Hopefully by now you feel pretty comfortable with all the major pieces to basic Android apps (Views, Controllers, ActionBar, Navigation, Models, Authentication, API Communication, Persistence, et al) and see how they all fit together. We are close now to a turning point in the course where you should be hitting a "critical mass" towards your knowledge of Android.

smacgregor commented 8 years ago

Consider including a check to see if internet is available to detect any network failures. Show an appropriate message in case of failure.

That was actually in there :).

Also here's a screen cap of offline support powered by ActiveAndroid. You can see that media was cached using Glide as well so many of the tweets have their media intact as well.

https://github.com/smacgregor/SimpleTwitterClient/tree/active_android

twitter_offline