Saturday, January 23, 2016

Understanding Architecture of Nick Butcher's Plaid App - Part-3

DataManager -  DataManager.java

Responsible for loading data from the various sources. Instantiating classes are responsible for
providing the {code onDataLoaded} method to do something with the data.
  • Important bit to note here is DataManager extends from BaseDatamanager which has an abstract method onDataLoded which will be called once the data is fetched from sources selected in the filter

  • DataManager constructor accepts filterAdapter and then loads data from all the filters in the adapter 

  • It also implements filtersChanged listener interface so that it gets notified when the filters change and then it can load resources of that filter 

Important bit to note here is DataManager extends from BaseDatamanager which has an abstract method
 
public abstract void onDataLoaded(List<? extends PlaidItem> data);

In HomeAcivity when the Datamanager is initialized this method is implemented

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
dataManager = new DataManager(this, filtersAdapter) {

            @Override

            public void onDataLoaded(List<? extends PlaidItem> data) {

                adapter.addAndResort(data);

                checkEmptyState();

            }

        };

dataManager.loadAllDataSources(); <--------------- This is onCreate () causes loading the data from all sources

Note that DataManager constructor accepts filterAdapter and then loads data from all the filters in the adapter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
 public DataManager(Context context,

                       FilterAdapter filterAdapter) {

        super(context);

        this.filterAdapter = filterAdapter;

        setupPageIndexes();

    }


    public void loadAllDataSources() {

        for (Source filter : filterAdapter.getFilters()) {

            loadSource(filter);

        }

    }


It also implements filtersChanged listener interface so that it gets notified when the filters change and then it can load resources of that filter 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 private void loadSource(Source source) {
        if (source.active) {
            loadStarted();
            int page = getNextPageIndex(source.key);
            switch (source.key) {
                case SourceManager.SOURCE_DESIGNER_NEWS_POPULAR:
                    loadDesignerNewsTopStories(page);
                    break;
                case SourceManager.SOURCE_DESIGNER_NEWS_RECENT:
                    loadDesignerNewsRecent(page);
                    break;
                case SourceManager.SOURCE_DRIBBBLE_POPULAR:
                    loadDribbblePopular(page);
                    break;
:
:
:
}


Look at how OnDataLoaded () method is invoked so that we can notify the initializer about the finish load event

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
private void loadDesignerNewsTopStories(final int page) {
        getDesignerNewsApi().getTopStories(page, new Callback<StoriesResponse>() {
            @Override
            public void success(StoriesResponse storiesResponse, Response response) {
                if (storiesResponse != null
                        && sourceIsEnabled(SourceManager.SOURCE_DESIGNER_NEWS_POPULAR)) {
                    setPage(storiesResponse.stories, page);
                    setDataSource(storiesResponse.stories,
                            SourceManager.SOURCE_DESIGNER_NEWS_POPULAR);
                    onDataLoaded(storiesResponse.stories);
                }
                loadFinished();
            }

            @Override
            public void failure(RetrofitError error) {
                loadFinished();
            }
        });
    }


 private void loadDesignerNewsRecent(final int page) {
        getDesignerNewsApi().getRecentStories(page, new Callback<StoriesResponse>() {
            @Override
            public void success(StoriesResponse storiesResponse, Response response) {
                if (storiesResponse != null
                        && sourceIsEnabled(SourceManager.SOURCE_DESIGNER_NEWS_RECENT)) {
                    setPage(storiesResponse.stories, page);
                    setDataSource(storiesResponse.stories,
                            SourceManager.SOURCE_DESIGNER_NEWS_RECENT);
                    onDataLoaded(storiesResponse.stories);
                }
                loadFinished();
            }

            @Override
            public void failure(RetrofitError error) {
                loadFinished();
            }
        });
    }

No comments:

Post a Comment