2

I use mikepenz's MaterialDrawer in my Android app. The app supports multiple logged in profiles at the same time, similar to Gmail. So while one profile is logged in, another profile can be added and then the user can switch the active profile by clicking on the icon in the header.

I use Volley to asynchronously load the profile picture from a server. This means, when I create the list of ProfileDrawerItems for the logged in profiles (when starting the app), I add a default icon. It should be updated as soon as the Volley request returns the correct icon. The same logic applies, when adding a new account.

This work partly. The correct icons are shown in the full account list (below the Account Header). But in the Account Header, I still see the default icons:

Example showing the default icon for the latest added profile. This picture shows, what the drawer looks like after adding a new profile ("Jubi").

After a successful login, I use

getAccountHeader().getProfiles().add(loadProfile(token));

to add the new profile to the Account Header. loadProfile(Token) looks like this:

   /**
     * Loads and returns the profile for the given token. If a profile already exists in the map profilesByUserId for the token's userID, that profile is updated, otherwise a new profile is created and put into that map.
     * The returned profile might not yet be ready, when this method returns. Some date is loaded asynchronously: Picture, username, user's first and last name.
     *
     * @param token the token, for which the profile should be created or updated
     * @return the profile
     */
    public ProfileDrawerItem loadProfile(Token token) {
        ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null) {
            profile = new ProfileDrawerItem();
            profilesByUserId.put(token.getUserId(), profile);
            profile.withIdentifier(token.getId());
            profile.withName(token.getUsername());
            profile.withIcon(R.drawable.default_profile_pic);
        }

        loadProfileData(token);
        loadProfilePic(token);
        return profile;
    }

loadProfileData(Token)and loadProfilePic(Token) contain the Volley requests. onSuccess(...) is called, when the request returns successfully.

loadProfileData(Token):

   /**
     * Loads the profile data (user's name and email address) for the given token and sets it in the appropriate profile. If there is no profile for the given token, nothing happens.
     *
     * @param token token, for which the profile data should be loaded
     */
    private void loadProfileData(final Token token) {

        final ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null)
            return;

        // Load name and email address
        Callback<User> callbackUser = new Callback<User>() {
            @Override
            public void onSuccess(User user) {
                profile.withName(String.format("%s %s", user.getFirstName(), user.getLastName()));
                profile.withEmail(user.getUsername());
                profile.withNameShown(true);
            }

            @Override
            public void onError(String error) {
               // not relevant
            }
        };
        loadUserById(token.getUserId(), Source.LOCAL_OVER_REMOTE, callbackUser, token);
    }

loadProfilePic(Token):

    /**
     * Loads the profile pic for the given token and sets it in the appropriate profile. If there is no profile for the given token, nothing happens.
     *
     * @param token token, for which the profile pic should be loaded
     */
    private void loadProfilePic(final Token token) {

        final ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null)
            return;

        // Load profile pic
        Callback<File> callbackPic = new Callback<File>() {
            @Override
            public void onSuccess(File profilePic) {
                Bitmap bitmap = FileUtils.createBitmap(profilePic);
                // Crop center square of the bitmap
                int dimension = Math.min(bitmap.getWidth(), bitmap.getHeight());
                bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
                profile.withIcon(bitmap);

            }

            @Override
            public void onError(String error) {
                // Not relevant
            }
        };
        loadProfilePicById(token.getUserId(), true, callbackPic, true);
    }

My expectation is that after onSuccess(File) has executed, the new profile pic should be shown in the Account Header and in the account list below the header. However, the pic, name and email in the Account Header are not updated. Only after switching profiles, which executes getAccountHeader().setActiveProfile(token.getId()), the correct data and pic are shown in the Account Header.

To me, this looks, like it was an issue with MaterialDrawer, but I'm not sure about that. And even if it were, maybe there is a workaround.

Does any one know, how I can get MaterialDrawer to refresh the Account Header, after changing a profile, or how else I can solve this issue?

0 Answers0