Android Open Source - ponyville-live-android Station Adapter






From Project

Back to project page ponyville-live-android.

License

The source code is released under:

Apache License

If you think the Android project ponyville-live-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ponyvillelive.app.ui;
//from  www  . j  av a 2 s  .  c  o  m
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.ponyvillelive.app.PvlApp;
import com.ponyvillelive.app.R;
import com.ponyvillelive.app.model.Station;
import com.ponyvillelive.app.net.API;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * An implementation of {@link android.widget.BaseAdapter}
 * for {@link com.ponyvillelive.app.model.Station}s
 */
public class StationAdapter extends RecyclerView.Adapter<StationAdapter.ViewHolder> {

    @Inject
    Picasso picasso;
    @Inject
    API     api;

    private LayoutInflater inflater;
    private Station[]      stations;
    private Subscription   apiSub;
    private Resources      res;

    public StationAdapter(LayoutInflater inflater, String type) {
        this.inflater = inflater;
        this.stations = new Station[0];
        this.res = inflater.getContext().getResources();
        PvlApp.get(inflater.getContext()).inject(this);
        apiSub = api
                .getStationList(type)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((stationResponse) -> {
                    this.stations = stationResponse.result;
                    notifyDataSetChanged();
                    apiSub.unsubscribe();
                });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.view_station_list_item, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Station station = stations[position];

        holder.name.setText(station.name);
        holder.genre.setText(station.genre);
        picasso
                .load(station.imageUrl)
                .placeholder(R.drawable.ic_launcher)
                .into(holder);
    }

    @Override
    public int getItemCount() {
        return stations.length;
    }

    public Station getItem(int position) {
        return stations[position];
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements Target {

        @InjectView(R.id.station_icon)
        ImageView   icon;
        @InjectView(R.id.station_name)
        TextView    name;
        @InjectView(R.id.station_genre)
        TextView    genre;
        @InjectView(R.id.station_menu)
        ImageButton menuButton;

        CardView baseView;
        Palette  colourPalette;

        public ViewHolder(View itemView) {
            super(itemView);
            baseView = (CardView) itemView;

            ButterKnife.inject(this, baseView);
        }

        @OnClick(R.id.station_menu)
        public void menuClicked(View v) {
            PopupMenu popupMenu = new PopupMenu(StationAdapter.this.inflater.getContext(), v);
            MenuInflater menuInflater = popupMenu.getMenuInflater();
            menuInflater.inflate(R.menu.view_station, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(item -> {
                if (item.getItemId() == R.id.action_change_stream) {
                    // TODO: Switch streams here
                    Toast.makeText(inflater.getContext(), "Change streams pressed", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            });
            popupMenu.show();
        }

        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            if (colourPalette == null) {
                Palette.generateAsync(bitmap, 8, (palette) -> baseView.post(() -> {
                    icon.setImageBitmap(bitmap);
                    colourPalette = palette;
                    colourise();
                }));
            } else {
                baseView.post(() -> {
                    icon.setImageBitmap(bitmap);
                    colourise();
                });
            }
        }

        public void colourise() {
           name.setTextColor(colourPalette.getVibrantColor(Color.BLACK));
           genre.setTextColor(colourPalette.getMutedColor(Color.DKGRAY));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            // noop
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            icon.post(() -> icon.setImageDrawable(placeHolderDrawable));
        }
    }
}




Java Source Code List

com.ponyvillelive.app.DebugPvlModule.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.PvlApp.java
com.ponyvillelive.app.PvlModule.java
com.ponyvillelive.app.model.ArrayResponse.java
com.ponyvillelive.app.model.DebugData.java
com.ponyvillelive.app.model.Entity.java
com.ponyvillelive.app.model.MapResponse.java
com.ponyvillelive.app.model.NowPlayingMeta.java
com.ponyvillelive.app.model.ObjectResponse.java
com.ponyvillelive.app.model.Show.java
com.ponyvillelive.app.model.SongWrapper.java
com.ponyvillelive.app.model.Song.java
com.ponyvillelive.app.model.StationMeta.java
com.ponyvillelive.app.model.Station.java
com.ponyvillelive.app.net.API.java
com.ponyvillelive.app.net.DebugNetModule.java
com.ponyvillelive.app.net.MockAPI.java
com.ponyvillelive.app.net.NetModule.java
com.ponyvillelive.app.prefs.AnimationSpeed.java
com.ponyvillelive.app.prefs.ApiEndpoint.java
com.ponyvillelive.app.prefs.ApiEndpoints.java
com.ponyvillelive.app.prefs.BooleanPreference.java
com.ponyvillelive.app.prefs.Endpoint.java
com.ponyvillelive.app.prefs.Endpoints.java
com.ponyvillelive.app.prefs.IntPreference.java
com.ponyvillelive.app.prefs.IsMockMode.java
com.ponyvillelive.app.prefs.MockDownloader.java
com.ponyvillelive.app.prefs.NetworkProxy.java
com.ponyvillelive.app.prefs.ObjectPreference.java
com.ponyvillelive.app.prefs.PicassoDebugging.java
com.ponyvillelive.app.prefs.PixelGridEnabled.java
com.ponyvillelive.app.prefs.PixelRatioEnabled.java
com.ponyvillelive.app.prefs.ScalpelEnabled.java
com.ponyvillelive.app.prefs.ScalpelWireframeEnabled.java
com.ponyvillelive.app.prefs.SeenDebugDrawer.java
com.ponyvillelive.app.prefs.StringPreference.java
com.ponyvillelive.app.ui.ActionbarHideSlidePanelListener.java
com.ponyvillelive.app.ui.ActivityHierarchyServer.java
com.ponyvillelive.app.ui.AnimationSpeedAdapter.java
com.ponyvillelive.app.ui.AppContainer.java
com.ponyvillelive.app.ui.BindableAdapter.java
com.ponyvillelive.app.ui.BottomDrawerFragment.java
com.ponyvillelive.app.ui.DebugAppContainer.java
com.ponyvillelive.app.ui.DebugUiModule.java
com.ponyvillelive.app.ui.EnumAdapter.java
com.ponyvillelive.app.ui.HierarchyTreeChangeListener.java
com.ponyvillelive.app.ui.MainActivity.java
com.ponyvillelive.app.ui.NetworkDelayAdapter.java
com.ponyvillelive.app.ui.NetworkErrorAdapter.java
com.ponyvillelive.app.ui.NetworkVarianceAdapter.java
com.ponyvillelive.app.ui.ProxyAdapter.java
com.ponyvillelive.app.ui.ServerEndpointAdapter.java
com.ponyvillelive.app.ui.SocketActivityHierarchyServer.java
com.ponyvillelive.app.ui.StationAdapter.java
com.ponyvillelive.app.ui.StationFragment.java
com.ponyvillelive.app.ui.TrackListAdapter.java
com.ponyvillelive.app.ui.UiModule.java
com.ponyvillelive.app.util.Strings.java