dmacan23
5/1/2015 - 5:27 PM

LessonItemPresenter.java

package hr.foi.mtlab.lorovoz.lessons.presenter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lightandroid.collection.presenter.LightPresenterSearchable;
import com.lightandroid.util.common.LightFont;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import hr.foi.mtlab.lorovoz.R;
import hr.foi.mtlab.lorovoz.lessons.list.OnBookmarkListener;
import hr.foi.mtlab.lorovoz.model.api.v1.Lesson;
import hr.foi.mtlab.lorovoz.widget.RatingLayout;

/**
 * Created by David on 16.4.2015..
 */
public class LessonItemPresenter implements LightPresenterSearchable {

    @InjectView(R.id.txtLessonTitle)
    TextView txtLessonTitle;
    @InjectView(R.id.txtLessonDescription)
    TextView txtLessonDescription;
    @InjectView(R.id.imgVideoThumbnail)
    ImageView imgVideoThumbnail;
    @InjectView(R.id.btnBookmark)
    TextView btnBookmark;
    @InjectView(R.id.rbLessonRating)
    RatingLayout rbLessonRating;
    @InjectView(R.id.txtDuration)
    TextView txtDuration;

    private final Lesson lesson;
    private OnLessonItemSelectedListener onSelectedListener;
    private OnBookmarkListener onBookmarkListener;
    private int position;

    public LessonItemPresenter(Lesson lesson) {
        this.lesson = lesson;
    }

    @Override
    public String provideSearchKey() {
        return this.lesson.getTitle();
    }

    @Override
    public void inject(View view) {
        ButterKnife.inject(this, view);
    }

    @Override
    public void display(View view, int i) {
        this.position = i;
        this.txtLessonTitle.setText(this.lesson.getTitle());
        this.txtLessonDescription.setText(this.lesson.getDescription());
        Picasso.with(view.getContext()).load(this.lesson.getThumbnailPath()).into(this.imgVideoThumbnail);
        LightFont.setFont(this.btnBookmark);
        this.rbLessonRating.setRating(this.lesson.getAverageRating(), 5);
        //this.txtDuration.setText(this.lesson.get);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.presenter_lesson;
    }

    public void setOnSelectedListener(OnLessonItemSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public void setOnBookmarkListener(OnBookmarkListener onBookmarkListener) {
        this.onBookmarkListener = onBookmarkListener;
    }

    @OnClick(R.id.itemPresenter)
    void clickItemPresenter() {
        if (this.onSelectedListener != null)
            this.onSelectedListener.onLessonItemSelected(LessonItemPresenter.this, LessonItemPresenter.this.lesson, LessonItemPresenter.this.position);
    }

    @OnClick(R.id.btnBookmark)
    void clickBookmark() {
        if (this.onBookmarkListener != null)
            this.onBookmarkListener.onBookmark(this.lesson, this.position);
    }

    public static List<LessonItemPresenter> generateFromModels(List<Lesson> models, OnLessonItemSelectedListener onSelectedListener, OnBookmarkListener onBookmarkListener) {
        List<LessonItemPresenter> presenters = new ArrayList<>();
        for (Lesson model : models) {
            LessonItemPresenter presenter = new LessonItemPresenter(model);
            presenter.setOnSelectedListener(onSelectedListener);
            presenter.setOnBookmarkListener(onBookmarkListener);
            presenters.add(presenter);
        }
        return presenters;
    }
}