Thursday, January 21, 2016

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

FilterAdapter

Adapter for showing list of data sources in Drawer.




Note that there are only three items in the list that need authorization

1
2
3
4
5
6
7
8
9
private boolean isAuthorisedDribbbleSource(Source source) {

        return source.key.equals(SourceManager.SOURCE_DRIBBBLE_FOLLOWING)

                || source.key.equals(SourceManager.SOURCE_DRIBBBLE_USER_LIKES)

                || source.key.equals(SourceManager.SOURCE_DRIBBBLE_USER_SHOTS);

    }


Two important interfaces this class implements
  •   FilterAuthoriser

      The whole purpose of this authorizer callback is if you select any items in the drawer that
       require Dribble login , then this Callback would be invoked and HomeActivity will call the
       Dribble login activity 

1
2
3
 public interface FilterAuthoriser {
        void requestDribbbleAuthorisation(View sharedElement, Source forSource);
    }

 

 Constructor :


1
2
3
4
5
6
7
  public FilterAdapter(@NonNull Context context,
                         @NonNull List<Source> filters,
                         @NonNull FilterAuthoriser authoriser) {  <- This is an interface , so that the list items which                  this.context = context.getApplicationContext();   require this authorization , we can call this callback
          this.filters = filters;                                                     provided by initializer
          this.authoriser = authoriser;
          setHasStableIds(true);
    }
This is how its called from onCreate () in HomeActivity

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
   filtersAdapter = new FilterAdapter(this, SourceManager.getSources(this),
                new FilterAdapter.FilterAuthoriser() {
            @Override
            public void requestDribbbleAuthorisation(View sharedElemeent, Source forSource) {
                Intent login = new Intent(HomeActivity.this, DribbbleLogin.class);
                login.putExtra(FabDialogMorphSetup.EXTRA_SHARED_ELEMENT_START_COLOR,
                        ContextCompat.getColor(HomeActivity.this, R.color.background_dark));
                ActivityOptions options =
                        ActivityOptions.makeSceneTransitionAnimation(HomeActivity.this,
                                sharedElemeent, getString(R.string.transition_dribbble_login));
                startActivityForResult(login,
                        getAuthSourceRequestCode(forSource), options.toBundle());
            }
        });
  •  FiltersChangedListener

     Selection / Unselection of these items in the drawer are reported to HomeActivity using this
     listener.

 public interface FiltersChangedListener {
        void onFiltersChanged(Source changedFilter);
        void onFilterRemoved(Source removed);
    }

And helper method to set this listener

public void addFilterChangedListener(FiltersChangedListener listener) {
        if (listeners == null) {
            listeners = new ArrayList<>();
        }
        listeners.add(listener);
    }

Question is how is HomeActivity notified of these changes ,  

 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 filtersAdapter.addFilterChangedListener(filtersChangedListener);

  private FilterAdapter.FiltersChangedListener filtersChangedListener =
            new FilterAdapter.FiltersChangedListener() {
        @Override
        public void onFiltersChanged(Source changedFilter) {
            if (!changedFilter.active) {
                adapter.removeDataSource(changedFilter.key);
            }
            checkEmptyState();
        }

        @Override
        public void onFilterRemoved(Source removed) {
            adapter.removeDataSource(removed.key);
            checkEmptyState();
        }
    };

No comments:

Post a Comment