com.afollestad.async.Action Java Examples

The following examples show how to use com.afollestad.async.Action. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlbumFragment.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
private void modifyMenu(final Menu menu) {
    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isAlbumFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            return pasta.isFavorite(data);
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "favorite album action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #2
Source File: ArtistFragment.java    From Pasta-for-Spotify with Apache License 2.0 5 votes vote down vote up
private void modifyMenu(final Menu menu) {
    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isArtistFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            try {
                return pasta.isFavorite(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "artist favorite action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #3
Source File: AlbumFragment.java    From Pasta-for-Spotify with Apache License 2.0 5 votes vote down vote up
private void modifyMenu(final Menu menu) {
    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isAlbumFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            return pasta.isFavorite(data);
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "favorite album action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #4
Source File: PlaylistFragment.java    From Pasta-for-Spotify with Apache License 2.0 5 votes vote down vote up
public void modifyMenu(final Menu menu) {
    if (data.editable) {
        menu.findItem(R.id.action_edit).setVisible(true);
    }

    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isPlaylistFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            try {
                return pasta.isFavorite(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "playlist favorite action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #5
Source File: ArtistFragment.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
private void modifyMenu(final Menu menu) {
    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isArtistFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            try {
                return pasta.isFavorite(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "artist favorite action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #6
Source File: PlaylistFragment.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
public void modifyMenu(final Menu menu) {
    menu.findItem(R.id.action_edit).setVisible(true);

    new Action<Boolean>() {
        @NonNull
        @Override
        public String id() {
            return "isPlaylistFav";
        }

        @Nullable
        @Override
        protected Boolean run() throws InterruptedException {
            try {
                return pasta.isFavorite(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void done(@Nullable Boolean result) {
            if (result == null) {
                pasta.onError(getActivity(), "playlist favorite action");
                return;
            }
            if (result) {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_fav);
            } else {
                menu.findItem(R.id.action_fav).setIcon(R.drawable.ic_unfav);
            }
        }

    }.execute();
}
 
Example #7
Source File: ArtistFragment.java    From Pasta-Music with Apache License 2.0 4 votes vote down vote up
private void onMenuClick(final MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().onBackPressed();
            break;
        case R.id.action_fav:
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "favArtist";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    if (!pasta.toggleFavorite(data)) {
                        return null;
                    } else return pasta.isFavorite(data);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) {
                        pasta.onError(getActivity(), "artist favorite menu action");
                        return;
                    }
                    if (result) {
                        item.setIcon(R.drawable.ic_fav);
                    } else {
                        item.setIcon(R.drawable.ic_unfav);
                    }
                }

            }.execute();
            break;
        case R.id.action_share:
            Intent s = new Intent(android.content.Intent.ACTION_SEND);
            s.setType("text/plain");
            s.putExtra(Intent.EXTRA_SUBJECT, data.artistName);
            s.putExtra(Intent.EXTRA_TEXT, StaticUtils.getArtistUrl(data.artistId));
            startActivity(Intent.createChooser(s, data.artistName));
            break;
        case R.id.action_web:
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(StaticUtils.getArtistUrl(data.artistId))));
            break;
    }
}
 
Example #8
Source File: CategoryFragment.java    From Pasta-Music with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = DataBindingUtil.inflate(inflater, R.layout.fragment_category, container, false).getRoot();
    ButterKnife.bind(this, rootView);

    pasta = (Pasta) getContext().getApplicationContext();
    data = getArguments().getParcelable("category");
    limit = (PreferenceUtils.getLimit(getContext()) + 1) * 10;

    toolbar.setTitle(data.categoryName);
    toolbar.setNavigationIcon(R.drawable.ic_back);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().onBackPressed();
        }
    });

    setData(data.categoryName, PreferenceUtils.getPrimaryColor(getContext()), PreferenceUtils.getPrimaryColor(getContext()));

    spinner.setVisibility(View.VISIBLE);

    manager = new GridLayoutManager(getContext(), PreferenceUtils.getColumnNumber(getContext(), false));

    if (PreferenceUtils.isCards(getContext())) {
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return manager.getSpanCount();
            }
        });
    }

    recycler.setLayoutManager(manager);
    adapter = new OmniAdapter((AppCompatActivity) getActivity(), null, false);
    recycler.setAdapter(adapter);
    recycler.setHasFixedSize(true);

    action = new Action<List<PlaylistListData>>() {
        @NonNull
        @Override
        public String id() {
            return "getCategoryPlaylists";
        }

        @Nullable
        @Override
        protected List<PlaylistListData> run() throws InterruptedException {
            return pasta.getPlaylists(data);
        }

        @Override
        protected void done(@Nullable List<PlaylistListData> result) {
            if (spinner != null) spinner.setVisibility(View.GONE);
            if (result == null) {
                pasta.onCriticalError(getActivity(), "category playlists action");
                return;
            }
            adapter.swapData(result);
        }
    };
    action.execute();

    return rootView;
}
 
Example #9
Source File: CategoriesFragment.java    From Pasta-Music with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_recycler, container, false);
    ButterKnife.bind(this, rootView);

    pasta = (Pasta) getContext().getApplicationContext();

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    recycler.setLayoutManager(new GridLayoutManager(getContext(), PreferenceUtils.getColumnNumber(getContext(), metrics.widthPixels > metrics.heightPixels)));
    adapter = new CategoryAdapter((AppCompatActivity) getActivity(), null);
    recycler.setAdapter(adapter);
    recycler.setHasFixedSize(true);

    action = new Action<List<CategoryListData>>() {
        @NonNull
        @Override
        public String id() {
            return "getCategories";
        }

        @Nullable
        @Override
        protected List<CategoryListData> run() throws InterruptedException {
            return pasta.getCategories();
        }

        @Override
        protected void done(@Nullable List<CategoryListData> result) {
            if (spinner != null) spinner.setVisibility(View.GONE);
            if (result == null) {
                pasta.onCriticalError(getActivity(), "categories action");
                return;
            }
            adapter.swapData(result);
        }
    };
    action.execute();

    return rootView;
}
 
Example #10
Source File: CategoriesFragment.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_recycler, container, false);
    unbinder = ButterKnife.bind(this, rootView);

    pasta = (Pasta) getContext().getApplicationContext();

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    recycler.setLayoutManager(new GridLayoutManager(getContext(), PreferenceUtils.getColumnNumber(getContext(), metrics.widthPixels > metrics.heightPixels)));
    adapter = new ListAdapter(new ArrayList<ListData>());
    recycler.setAdapter(adapter);
    recycler.setHasFixedSize(true);

    action = new Action<List<ListData>>() {
        @NonNull
        @Override
        public String id() {
            return "getCategories";
        }

        @Nullable
        @Override
        protected List<ListData> run() throws InterruptedException {
            CategoriesPager categories = null;
            for (int i = 0; categories == null && i < PreferenceUtils.getRetryCount(getContext()); i++) {
                try {
                    categories = pasta.spotifyService.getCategories(null);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (StaticUtils.shouldResendRequest(e)) Thread.sleep(200);
                    else break;
                }
            }
            if (categories == null) return null;

            List<ListData> list = new ArrayList<>();
            for (Category category : categories.categories.items) {
                list.add(new CategoryListData(category));
            }
            return list;
        }

        @Override
        protected void done(@Nullable List<ListData> result) {
            if (spinner != null) spinner.setVisibility(View.GONE);
            if (result == null) {
                pasta.onCriticalError(getActivity(), "categories action");
                return;
            }
            adapter.setList(result);
        }
    };
    action.execute();

    return rootView;
}
 
Example #11
Source File: ArtistFragment.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
private void onMenuClick(final MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().onBackPressed();
            break;
        case R.id.action_fav:
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "favArtist";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    if (!pasta.toggleFavorite(data)) {
                        return null;
                    } else return pasta.isFavorite(data);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) {
                        pasta.onError(getActivity(), "artist favorite menu action");
                        return;
                    }
                    if (result) {
                        item.setIcon(R.drawable.ic_fav);
                    } else {
                        item.setIcon(R.drawable.ic_unfav);
                    }
                }

            }.execute();
            break;
        case R.id.action_share:
            Intent s = new Intent(android.content.Intent.ACTION_SEND);
            s.setType("text/plain");
            s.putExtra(Intent.EXTRA_SUBJECT, data.artistName);
            s.putExtra(Intent.EXTRA_TEXT, StaticUtils.getArtistUrl(data.artistId));
            startActivity(Intent.createChooser(s, data.artistName));
            break;
        case R.id.action_web:
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(StaticUtils.getArtistUrl(data.artistId))));
            break;
    }
}
 
Example #12
Source File: CategoryFragment.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_category, container, false);
    unbinder = ButterKnife.bind(this, rootView);

    pasta = (Pasta) getContext().getApplicationContext();
    data = getArguments().getParcelable("category");
    limitMap = new HashMap<>();
    limitMap.put(SpotifyService.LIMIT, (PreferenceUtils.getLimit(getContext()) + 1) * 10);

    toolbar.setTitle(data.categoryName);
    toolbar.setNavigationIcon(R.drawable.ic_back);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().onBackPressed();
        }
    });

    int color = ContextCompat.getColor(getContext(), R.color.colorPrimary);
    setData(data.categoryName, color, color);

    spinner.setVisibility(View.VISIBLE);

    manager = new GridLayoutManager(getContext(), 1);

    recycler.setLayoutManager(manager);
    adapter = new ListAdapter(new ArrayList<ListData>());
    recycler.setAdapter(adapter);
    recycler.setHasFixedSize(true);

    action = new Action<ArrayList<PlaylistListData>>() {
        @NonNull
        @Override
        public String id() {
            return "getCategoryPlaylists";
        }

        @Nullable
        @Override
        protected ArrayList<PlaylistListData> run() throws InterruptedException {
            PlaylistsPager pager = null;
            for (int i = 0; pager == null && i < PreferenceUtils.getRetryCount(getContext()); i++) {
                try {
                    pager = pasta.spotifyService.getPlaylistsForCategory(data.categoryId, limitMap);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (StaticUtils.shouldResendRequest(e)) Thread.sleep(200);
                    else break;
                }
            }
            if (pager == null) return null;

            ArrayList<PlaylistListData> list = new ArrayList<>();
            for (PlaylistSimple playlist : pager.playlists.items) {
                list.add(new PlaylistListData(playlist, pasta.me));
            }
            return list;
        }

        @Override
        protected void done(@Nullable ArrayList<PlaylistListData> result) {
            if (spinner != null) spinner.setVisibility(View.GONE);
            if (result == null) {
                pasta.onCriticalError(getActivity(), "category playlists action");
                return;
            }
            adapter.setList(result);
        }
    };
    action.execute();

    return rootView;
}
 
Example #13
Source File: ArtistListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.menu:
            PopupMenu popup = new PopupMenu(v.getContext(), v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_basic, popup.getMenu());

            final MenuItem fav = popup.getMenu().findItem(R.id.action_fav);
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "isTrackFav";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) return;
                    if (result) {
                        fav.setTitle(R.string.unfav);
                    } else {
                        fav.setTitle(R.string.fav);
                    }
                }

            }.execute();

            popup.setOnMenuItemClickListener(this);
            popup.show();
            break;
        default:
            Bundle args = new Bundle();
            args.putParcelable("artist", listData);

            Fragment f = new ArtistFragment();
            f.setArguments(args);

            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, f).addToBackStack(null).commit();
            break;
    }
}
 
Example #14
Source File: ArtistListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onMenuItemClick(final MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_fav:
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "favArtist";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    if (!pasta.toggleFavorite(listData)) {
                        return null;
                    } else
                        return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) {
                        pasta.onError(activity, "favorite artist menu action");
                        return;
                    }
                    if (result)
                        item.setTitle(R.string.unfav);
                    else item.setTitle(R.string.fav);
                }

            }.execute();
            break;
        case R.id.action_web:
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(StaticUtils.getArtistUrl(listData.artistId))));
            break;
        case R.id.action_share:
            Intent s = new Intent(android.content.Intent.ACTION_SEND);
            s.setType("text/plain");
            s.putExtra(Intent.EXTRA_SUBJECT, listData.artistName);
            s.putExtra(Intent.EXTRA_TEXT, StaticUtils.getArtistUrl(listData.artistId));
            activity.startActivity(Intent.createChooser(s, listData.artistName));
            break;
    }
    return false;
}
 
Example #15
Source File: AlbumListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public void bindView(final ViewHolder holder) {
    if (holder.artist != null) {
        if (artists.size() > 0) {
            holder.artistName.setText(artists.get(0).artistName);
            holder.artistExtra.setText(albumDate);

            new Action<ArtistListData>() {
                @NonNull
                @Override
                public String id() {
                    return "gotoArtist";
                }

                @Nullable
                @Override
                protected ArtistListData run() throws InterruptedException {
                    return holder.pasta.getArtist(artists.get(0).artistId);
                }

                @Override
                protected void done(@Nullable ArtistListData result) {
                    if (result == null) {
                        holder.pasta.onError(holder.activity, "artist action");
                        return;
                    }

                    holder.artist.setTag(result);
                }
            }.execute();
        } else holder.artist.setVisibility(View.GONE);
    }

    holder.name.setText(albumName);
    holder.extra.setText(String.valueOf(tracks) + " track" + (tracks == 1 ? "" : "s"));

    if (!PreferenceUtils.isThumbnails(holder.pasta)) holder.image.setVisibility(View.GONE);
    else {
        Glide.with(holder.pasta).load(albumImage).asBitmap().placeholder(ImageUtils.getVectorDrawable(holder.pasta, R.drawable.preload)).into(new BitmapImageViewTarget(holder.image) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                super.onResourceReady(resource, glideAnimation);

                if (holder.bg == null) return;
                Palette.from(resource).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {

                        ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), Color.DKGRAY, palette.getDarkVibrantColor(Color.DKGRAY));
                        animator.setDuration(250);
                        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                int color = (int) animation.getAnimatedValue();
                                holder.bg.setBackgroundColor(color);
                                holder.artist.setBackgroundColor(Color.argb(255, Math.max(Color.red(color) - 10, 0), Math.max(Color.green(color) - 10, 0), Math.max(Color.blue(color) - 10, 0)));
                            }
                        });
                        animator.start();
                    }
                });
            }
        });
    }
}
 
Example #16
Source File: AlbumListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onMenuItemClick(final MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_fav:
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "favAlbum";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    if (!pasta.toggleFavorite(listData)) {
                        return null;
                    } else return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) {
                        pasta.onError(activity, "favorite album menu action");
                        return;
                    }

                    item.setTitle(result ? R.string.unfav : R.string.fav);
                }

            }.execute();
            break;
        case R.id.action_web:
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(StaticUtils.getAlbumUrl(listData.albumId))));
            break;
        case R.id.action_share:
            Intent s = new Intent(android.content.Intent.ACTION_SEND);
            s.setType("text/plain");
            s.putExtra(Intent.EXTRA_SUBJECT, listData.albumName);
            s.putExtra(Intent.EXTRA_TEXT, StaticUtils.getAlbumUrl(listData.albumId));
            activity.startActivity(Intent.createChooser(s, listData.albumName));
            break;
    }
    return false;
}
 
Example #17
Source File: PlaylistListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.menu:
            PopupMenu popup = new PopupMenu(v.getContext(), v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_playlist, popup.getMenu());

            final MenuItem fav = popup.getMenu().findItem(R.id.action_fav);
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "isPlaylistFav";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) return;
                    if (result) {
                        fav.setTitle(R.string.unfav);
                    } else {
                        fav.setTitle(R.string.fav);
                    }
                }

            }.execute();

            popup.setOnMenuItemClickListener(this);
            popup.getMenu().findItem(R.id.action_edit).setVisible(listData.editable);
            popup.show();
            break;
        default:
            Bundle args = new Bundle();
            args.putParcelable("playlist", listData);

            Fragment f = new PlaylistFragment();
            f.setArguments(args);

            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, f).addToBackStack(null).commit();
            break;
    }
}
 
Example #18
Source File: PlaylistListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onMenuItemClick(final MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_fav:
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "favPlaylist";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    if (!pasta.toggleFavorite(listData)) {
                        return null;
                    } else return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) {
                        pasta.onError(activity, "favorite playlist menu action");
                        return;
                    }
                    if (result) {
                        item.setTitle(R.string.unfav);
                    } else {
                        item.setTitle(R.string.fav);
                    }
                }

            }.execute();
            break;
        case R.id.action_edit:
            new NewPlaylistDialog(activity).setPlaylist(listData).show();
            break;
        case R.id.action_web:
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(StaticUtils.getPlaylistUrl(listData.playlistOwnerId, listData.playlistId))));
            break;
        case R.id.action_share:
            Intent s = new Intent(android.content.Intent.ACTION_SEND);
            s.setType("text/plain");
            s.putExtra(Intent.EXTRA_SUBJECT, listData.playlistName);
            s.putExtra(Intent.EXTRA_TEXT, StaticUtils.getPlaylistUrl(listData.playlistOwnerId, listData.playlistId));
            activity.startActivity(Intent.createChooser(s, listData.playlistName));
            break;
    }
    return false;
}
 
Example #19
Source File: TrackListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.menu:
            PopupMenu popup = new PopupMenu(v.getContext(), v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_track, popup.getMenu());

            final MenuItem fav = popup.getMenu().findItem(R.id.action_fav);
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "isTrackFav";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) return;
                    if (result) {
                        fav.setTitle(R.string.unfav);
                    } else {
                        fav.setTitle(R.string.fav);
                    }
                }

            }.execute();

            popup.setOnMenuItemClickListener(this);
            popup.show();
            break;
        default:
            ArrayList<TrackListData> trackList = new ArrayList<TrackListData>();
            if (listData.tracks != null)
                trackList.addAll(listData.tracks);
            else trackList.add(listData);

            StaticUtils.play(trackList.indexOf(listData), trackList, activity);

            ActivityOptionsCompat options = ActivityOptionsCompat.makeCustomAnimation(activity, R.anim.slide_up, R.anim.blank);
            activity.startActivity(new Intent(activity, PlayerActivity.class), options.toBundle());
            break;
    }
}