me.fireant.photoselect.persenter.PhotoSelectPersenterImpl.java Source code

Java tutorial

Introduction

Here is the source code for me.fireant.photoselect.persenter.PhotoSelectPersenterImpl.java

Source

/*
 * Copyright 2015 ??
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.fireant.photoselect.persenter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import me.fireant.photoselect.bean.Folder;
import me.fireant.photoselect.bean.Photo;
import me.fireant.photoselect.persenter.inter.PhotoSelectPersenter;
import me.fireant.photoselect.view.PhotoSelectView;

/**
 * ?persenter
 *
 * @author ??
 * @email fireant.me@gmail.com
 * @created 16/1/26 ?3:02
 */
public class PhotoSelectPersenterImpl extends BasePersenter<PhotoSelectView> implements PhotoSelectPersenter {

    private String[] projection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.WIDTH,
            MediaStore.Images.ImageColumns.HEIGHT };

    private LoaderManager mLoaderManager;

    public PhotoSelectPersenterImpl(Context context, PhotoSelectView mvpView, LoaderManager loaderManager) {
        super(context, mvpView);
        this.mLoaderManager = loaderManager;
    }

    @Override
    public void loadPhotos(final Folder folder) {
        if (folder == null) {
            return;
        }
        mMvpView.showPhotos(folder.getPhotos());
    }

    @Override
    public void loadFolder() {
        this.mLoaderManager.initLoader(1000, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                return new CursorLoader(mContext, uri, projection, "", null,
                        MediaStore.Images.Media.DATE_MODIFIED + " DESC");
            }

            @Override
            public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
                if (loader == null) {
                    return;
                }

                // ??
                // <??>
                LinkedHashMap<String, Folder> folders = new LinkedHashMap<>();

                Folder allPhotoFolder = new Folder("");

                folders.put(allPhotoFolder.getFolderName(), allPhotoFolder);

                while (cursor.moveToNext()) {
                    String name = cursor
                            .getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
                    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    Photo photoInfo = new Photo(path);
                    // 
                    if (!folders.containsKey(name)) {
                        Folder newFolder = new Folder(name);
                        folders.put(name, newFolder);
                    }
                    // 
                    folders.get(name).addPhoto(photoInfo);
                    // 
                    allPhotoFolder.addPhoto(photoInfo);
                }

                ArrayList<Folder> folderData = new ArrayList<>();

                for (Folder folder : folders.values()) {
                    folderData.add(folder);
                }

                if (folderData != null) {
                    mMvpView.showFolder(folderData);
                }
            }

            @Override
            public void onLoaderReset(Loader<Cursor> loader) {

            }
        });
    }
}