Android Open Source - iwannawatch-android Movie View






From Project

Back to project page iwannawatch-android.

License

The source code is released under:

MIT License

If you think the Android project iwannawatch-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.meoyawn.iwannawatch;
// w  ww.  jav  a2 s .c  om
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.meoyawn.iwannawatch.models.Movie;
import com.meoyawn.iwannawatch.qualifiers.Delete;
import com.meoyawn.iwannawatch.qualifiers.Done;
import com.squareup.picasso.Picasso;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.Optional;
import rx.subjects.PublishSubject;

/**
 * Created by adel on 3/1/14
 */
public class MovieView extends FrameLayout {
    @Inject         Picasso               picasso;
    @Inject @Done   PublishSubject<Movie> doneSubject;
    @Inject @Delete PublishSubject<Movie> deleteSubject;

    @InjectView(R.id.img_movie_item_poster) DynamicHeightImageView poster;
    @InjectView(R.id.txt_movie_item_name)   TextView               name;
    @InjectView(R.id.txt_movie_item_date)   TextView               date;

    @Nullable @Optional @InjectView(R.id.btn_movie_item_delete) ImageButton delete;
    @Nullable @Optional @InjectView(R.id.btn_movie_item_done)   ImageButton changeStatus;

    @Nullable Movie movie;

    public MovieView(Context context) { super(context); }

    public MovieView(Context context, AttributeSet attrs) { super(context, attrs); }

    @Override @SuppressWarnings("deprecation") protected void onFinishInflate() {
        super.onFinishInflate();
        if (!isInEditMode()) {
            ButterKnife.inject(this);
            App.getObjectGraph(getContext()).inject(this);
            poster.setHeightRatio(1.5);

            if (changeStatus != null) {
                changeStatus.setBackgroundDrawable(blackFilter(changeStatus));
            }
            if (delete != null) {
                delete.setBackgroundDrawable(blackFilter(delete));

                setOnClickListener(v -> {
                    if (movie != null && getContext() != null) {
                        String url = String.format("http://www.themoviedb.org/movie/%d", movie._id);
                        getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    }
                });
            }
        }
    }

    @Nullable static Drawable blackFilter(@NotNull View view) {
        Drawable drawable = view.getBackground();
        if (drawable != null) {
            Drawable clone = drawable.getConstantState().newDrawable();
            clone.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
            return clone;
        }
        return null;
    }

    public void draw(@Nullable Movie movie) {
        this.movie = movie;
        if (movie != null) {
            String posterUrl = movie.getPosterUrl(poster.getCalculatedWidth() > 0 ?
                    poster.getCalculatedWidth() :
                    185);
            picasso.load(posterUrl)
                    .placeholder(R.drawable.placeholder)
                    .into(poster);
            name.setText(movie.title);
            date.setText(movie.getReleaseYear());
            if (changeStatus != null) {
                changeStatus.setImageResource(movie.watched ?
                        R.drawable.ic_action_add :
                        R.drawable.ic_action_accept);
            }
        }
    }

    @Optional @OnClick(R.id.btn_movie_item_done) void done() { doneSubject.onNext(movie); }

    @Optional @OnClick(R.id.btn_movie_item_delete) void delete() {
        if (getContext() != null && movie != null) {
            new AlertDialog.Builder(getContext())
                    .setMessage(getContext().getString(R.string.are_you_sure_delete, movie.title))
                    .setPositiveButton(R.string.yes, (dialog, which) -> deleteSubject.onNext(movie))
                    .setNegativeButton(R.string.no, null)
                    .show();
        }
    }
}




Java Source Code List

com.meoyawn.iwannawatch.AppModule.java
com.meoyawn.iwannawatch.App.java
com.meoyawn.iwannawatch.CrashReportingTree.java
com.meoyawn.iwannawatch.DynamicHeightImageView.java
com.meoyawn.iwannawatch.MainActivity.java
com.meoyawn.iwannawatch.MainFragment.java
com.meoyawn.iwannawatch.MovieView.java
com.meoyawn.iwannawatch.adapters.BaseMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.CursorMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.DynamicMoviesAdapter.java
com.meoyawn.iwannawatch.doers.DatabaseHelper.java
com.meoyawn.iwannawatch.doers.TheMovieDbApi.java
com.meoyawn.iwannawatch.models.Movie.java
com.meoyawn.iwannawatch.models.Search.java
com.meoyawn.iwannawatch.qualifiers.Delete.java
com.meoyawn.iwannawatch.qualifiers.Done.java