public PostsAdapter()

in app/src/main/java/com/amazonaws/postsapp/PostsAdapter.java [65:231]


    public PostsAdapter(final PostsActivity display) {
        this.display = display;

        mPostIDs = new ArrayList<String>();
        allPosts = new HashMap<String, ListPostsQuery.ListPost>();

        //Create the client
        final AWSAppSyncClient client = ClientFactory.getInstance(display.getApplicationContext());


        //Setup the List Posts Query
        listPostsQuery = ListPostsQuery.builder().build();
        GraphQLCall.Callback listPostsQueryCallback = new GraphQLCall.Callback<ListPostsQuery.Data>() {
            @Override
            public void onResponse(@Nonnull final Response<ListPostsQuery.Data> response) {
                if (response == null  || response.data() == null ||
                        response.data().listPosts() == null ||
                        response.data().listPosts() == null ) {
                    Log.d(TAG, "List Posts returned with no data");
                    return;
                }

                Log.d(TAG, "listPostsQuery returned data. Iterating over the data");
                display.runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       for (ListPostsQuery.ListPost p: response.data().listPosts()) {
                           //Populate the allPosts map with the posts returned by the query.
                           //The allPosts map is used by the recycler view.

                           if ( allPosts.get(p.id()) == null )  {
                               mPostIDs.add(0, p.id());
                           }
                           allPosts.put(p.id(), new ListPostsQuery.ListPost("Post",
                                   p.id(), p.author(), p.title(), p.content(),
                                   p.url(),p.ups(),p.downs(),
                                   p.createdDate(),
                                   p.aws_ds()));
                       }
                       //Trigger the view to refresh by calling notifyDataSetChanged method
                       notifyDataSetChanged();
                   }
               });
            }

            @Override
            public void onFailure(@Nonnull ApolloException e) {
                Log.e(TAG, "ListPostsQuery failed with [" + e.getLocalizedMessage() + "]");
            }
        };
        

        //Setup Delta Post Subscription to get notified when a change (add, update, delete) happens on a Post
        OnDeltaPostSubscription onDeltaPostSubscription =
                OnDeltaPostSubscription.builder().build();
        AppSyncSubscriptionCall.Callback onDeltaPostSubscriptionCallback =
                new AppSyncSubscriptionCall.Callback<OnDeltaPostSubscription.Data>() {
            @Override
            public void onResponse(@Nonnull Response<OnDeltaPostSubscription.Data> response) {
                Log.d(TAG, "Delta on a post received via subscription.");
                if (response == null  || response.data() == null
                        || response.data().onDeltaPost() == null ) {
                    Log.d(TAG, "Delta was null!");
                    return;
                }

                Log.d(TAG, "Updating post to display");
                final OnDeltaPostSubscription.OnDeltaPost p = response.data().onDeltaPost();

                display.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Delete the Post if the aws_ds has the DELETE tag
                        if ( DeltaAction.DELETE == p.aws_ds()) {
                            mPostIDs.remove(p.id());
                            allPosts.remove(p.id());
                        }
                        // Add/Update the post otherwise
                        else {
                            if (allPosts.get(p.id()) == null) {
                                mPostIDs.add(0, p.id());
                            }
                            allPosts.put(p.id(), new ListPostsQuery.ListPost("Post",
                                    p.id(), p.author(), p.title(), p.content(),
                                    p.url(),p.ups(), p.downs(),
                                    p.createdDate(),
                                    p.aws_ds()));
                        }

                        //Update the baseQuery Cache
                        updateListPostsQueryCache();

                        //Trigger the view to refresh by calling notifyDataSetChanged method
                        notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onFailure(@Nonnull ApolloException e) {
                Log.e(TAG, "Error " + e.getLocalizedMessage());
            }
            
            @Override
            public void onCompleted() {
                Log.d(TAG, "Received onCompleted on subscription");

            }
        };


        //Setup the Delta Query to get changes to posts incrementally
        Query listPostsDeltaQuery = ListPostsDeltaQuery.builder().build();

        GraphQLCall.Callback listPostsDeltaQueryCallback = new GraphQLCall.Callback<ListPostsDeltaQuery.Data>() {
            @Override
            public void onResponse(@Nonnull final Response<ListPostsDeltaQuery.Data> response) {
                if (response == null  || response.data() == null || response.data().listPostsDelta() == null ) {
                    Log.d(TAG, "listPostsDelta returned with no data");
                    return;
                }

                //Add to the ListPostsQuery Cache
                Log.d(TAG, "listPostsDelta returned data. Iterating...");
                display.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (ListPostsDeltaQuery.ListPostsDeltum p: response.data().listPostsDelta() ) {
                            // Delete the Post if the aws_ds has the DELETE tag
                            if ( DeltaAction.DELETE == p.aws_ds()) {
                                mPostIDs.remove(p.id());
                                allPosts.remove(p.id());
                                Log.v(TAG, "Got Post [" + p.id() + "] with aws_ds set to DELETE");
                                continue;
                            }

                            Log.v(TAG, "Got Post [" + p.id() + "] with aws_ds not set to DELETE");
                            // Add or Update the post otherwise
                            if (allPosts.get(p.id()) == null ) {
                                mPostIDs.add(0, p.id());
                            }
                            allPosts.put(p.id(), new ListPostsQuery.ListPost("Post",
                                    p.id(), p.author(), p.title(), p.content(),
                                    p.url(),p.ups(), p.downs(),
                                    p.createdDate(),
                                    p.aws_ds()));
                        }

                        //Update the baseQuery Cache
                        updateListPostsQueryCache();

                        //Trigger the view to refresh by calling notifyDataSetChanged method
                        notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onFailure(@Nonnull ApolloException e) {
                Log.e(TAG, "listPostsDelta Query failed with [" + e.getLocalizedMessage() + "]");
            }
        };

        client.sync(listPostsQuery, listPostsQueryCallback,    onDeltaPostSubscription , onDeltaPostSubscriptionCallback, listPostsDeltaQuery, listPostsDeltaQueryCallback, 20 * 60 );


    }