Search in sources :

Example 6 with Folder

use of io.github.ryanhoo.music.data.model.Folder in project StylishMusicPlayer by ryanhoo.

the class FileUtils method folderFromDir.

public static Folder folderFromDir(File dir) {
    Folder folder = new Folder(dir.getName(), dir.getAbsolutePath());
    List<Song> songs = musicFiles(dir);
    folder.setSongs(songs);
    folder.setNumOfSongs(songs.size());
    return folder;
}
Also used : Song(io.github.ryanhoo.music.data.model.Song) Folder(io.github.ryanhoo.music.data.model.Folder)

Example 7 with Folder

use of io.github.ryanhoo.music.data.model.Folder in project StylishMusicPlayer by ryanhoo.

the class FolderFragment method onAction.

// Adapter Callbacks
@Override
public void onAction(View actionView, final int position) {
    final Folder folder = mAdapter.getItem(position);
    PopupMenu actionMenu = new PopupMenu(getActivity(), actionView, Gravity.END | Gravity.BOTTOM);
    actionMenu.inflate(R.menu.folders_action);
    actionMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()) {
                case R.id.menu_item_add_to_play_list:
                    new AddToPlayListDialogFragment().setCallback(new AddToPlayListDialogFragment.Callback() {

                        @Override
                        public void onPlayListSelected(PlayList playList) {
                            mPresenter.addFolderToPlayList(folder, playList);
                        }
                    }).show(getFragmentManager().beginTransaction(), "AddToPlayList");
                    break;
                case R.id.menu_item_create_play_list:
                    PlayList playList = PlayList.fromFolder(folder);
                    EditPlayListDialogFragment.editPlayList(playList).setCallback(new EditPlayListDialogFragment.Callback() {

                        @Override
                        public void onCreated(PlayList playList) {
                        // Empty
                        }

                        @Override
                        public void onEdited(PlayList playList) {
                            mPresenter.createPlayList(playList);
                        }
                    }).show(getFragmentManager().beginTransaction(), "CreatePlayList");
                    break;
                case R.id.menu_item_refresh:
                    mUpdateIndex = position;
                    mPresenter.refreshFolder(folder);
                    break;
                case R.id.menu_item_delete:
                    mDeleteIndex = position;
                    mPresenter.deleteFolder(folder);
                    break;
            }
            return true;
        }
    });
    actionMenu.show();
}
Also used : AddToPlayListDialogFragment(io.github.ryanhoo.music.ui.playlist.AddToPlayListDialogFragment) PlayList(io.github.ryanhoo.music.data.model.PlayList) Folder(io.github.ryanhoo.music.data.model.Folder) EditPlayListDialogFragment(io.github.ryanhoo.music.ui.playlist.EditPlayListDialogFragment) PopupMenu(android.support.v7.widget.PopupMenu)

Example 8 with Folder

use of io.github.ryanhoo.music.data.model.Folder in project StylishMusicPlayer by ryanhoo.

the class FolderPresenter method addFolders.

@Override
public void addFolders(List<File> folders, final List<Folder> existedFolders) {
    Subscription subscription = Observable.from(folders).filter(new Func1<File, Boolean>() {

        @Override
        public Boolean call(File file) {
            for (Folder folder : existedFolders) {
                if (file.getAbsolutePath().equals(folder.getPath())) {
                    return false;
                }
            }
            return true;
        }
    }).flatMap(new Func1<File, Observable<Folder>>() {

        @Override
        public Observable<Folder> call(File file) {
            Folder folder = new Folder();
            folder.setName(file.getName());
            folder.setPath(file.getAbsolutePath());
            List<Song> musicFiles = FileUtils.musicFiles(file);
            folder.setSongs(musicFiles);
            folder.setNumOfSongs(musicFiles.size());
            return Observable.just(folder);
        }
    }).toList().flatMap(new Func1<List<Folder>, Observable<List<Folder>>>() {

        @Override
        public Observable<List<Folder>> call(List<Folder> folders) {
            return mRepository.create(folders);
        }
    }).doOnNext(new Action1<List<Folder>>() {

        @Override
        public void call(List<Folder> folders) {
            Collections.sort(folders, new Comparator<Folder>() {

                @Override
                public int compare(Folder f1, Folder f2) {
                    return f1.getName().compareToIgnoreCase(f2.getName());
                }
            });
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Folder>>() {

        @Override
        public void onStart() {
            mView.showLoading();
        }

        @Override
        public void onCompleted() {
            mView.hideLoading();
        }

        @Override
        public void onError(Throwable e) {
            mView.hideLoading();
            mView.handleError(e);
        }

        @Override
        public void onNext(List<Folder> allNewFolders) {
            mView.onFoldersAdded(allNewFolders);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : Action1(rx.functions.Action1) Folder(io.github.ryanhoo.music.data.model.Folder) Observable(rx.Observable) List(java.util.List) PlayList(io.github.ryanhoo.music.data.model.PlayList) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Func1(rx.functions.Func1) File(java.io.File)

Example 9 with Folder

use of io.github.ryanhoo.music.data.model.Folder in project StylishMusicPlayer by ryanhoo.

the class FolderPresenter method deleteFolder.

@Override
public void deleteFolder(Folder folder) {
    Subscription subscription = mRepository.delete(folder).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Folder>() {

        @Override
        public void onStart() {
            mView.showLoading();
        }

        @Override
        public void onCompleted() {
            mView.hideLoading();
        }

        @Override
        public void onError(Throwable e) {
            mView.hideLoading();
            mView.handleError(e);
        }

        @Override
        public void onNext(Folder folder) {
            mView.onFolderDeleted(folder);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Folder(io.github.ryanhoo.music.data.model.Folder)

Aggregations

Folder (io.github.ryanhoo.music.data.model.Folder)9 PlayList (io.github.ryanhoo.music.data.model.PlayList)4 Subscription (rx.Subscription)4 CompositeSubscription (rx.subscriptions.CompositeSubscription)4 File (java.io.File)3 List (java.util.List)3 Song (io.github.ryanhoo.music.data.model.Song)2 OnItemClickListener (io.github.ryanhoo.music.ui.base.adapter.OnItemClickListener)2 DefaultDividerDecoration (io.github.ryanhoo.music.ui.common.DefaultDividerDecoration)2 Func1 (rx.functions.Func1)2 PopupMenu (android.support.v7.widget.PopupMenu)1 PlayListNowEvent (io.github.ryanhoo.music.event.PlayListNowEvent)1 AddToPlayListDialogFragment (io.github.ryanhoo.music.ui.playlist.AddToPlayListDialogFragment)1 EditPlayListDialogFragment (io.github.ryanhoo.music.ui.playlist.EditPlayListDialogFragment)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Observable (rx.Observable)1 Action1 (rx.functions.Action1)1