Fragment

mail@pastecode.io avatar
unknown
java
3 years ago
11 kB
2
Indexable
Never
package com.spnf.app.spnf.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jama.carouselview.CarouselView;
import com.jama.carouselview.CarouselViewListener;
import com.jama.carouselview.enums.IndicatorAnimationType;
import com.jama.carouselview.enums.OffsetType;
import com.spnf.app.spnf.DetailLokerAct;
import com.spnf.app.spnf.InfoAct;
import com.spnf.app.spnf.KegiatanAct;
import com.spnf.app.spnf.LokerAct;
import com.spnf.app.spnf.ModulAct;
import com.spnf.app.spnf.MoreAct;
import com.spnf.app.spnf.Produk2Act;
import com.spnf.app.spnf.R;
import com.spnf.app.spnf.SPreferenced.PrefManager;
import com.spnf.app.spnf.TugasAct;
import com.spnf.app.spnf.VideoAct;
import com.spnf.app.spnf.adapter.AdapterProduk;
import com.spnf.app.spnf.api.client;
import com.spnf.app.spnf.models.loker.LokerItem;
import com.spnf.app.spnf.models.loker.ResponseLoker;
import com.spnf.app.spnf.models.produk.ProdukItem;
import com.spnf.app.spnf.models.produk.ResponseProduk;
import com.spnf.app.spnf.utils.move;
import com.squareup.picasso.Picasso;

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

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.spnf.app.spnf.api.client.IMG_LOWONGAN;

public class DashboardFragment extends Fragment implements View.OnClickListener{
    CarouselView carouselView;
    Context mContext;
    LinearLayout btn_modul, btn_video, btn_tugas, btn_berita, btn_info, btn_kegiatan, btn_produk, btn_more;
    TextView display_name, display_kelas;
    ImageView display_pict;
    List<LokerItem> semuaBerita;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    int[] sampleImages = {R.drawable.error_news, R.drawable.error_news, R.drawable.error_news};
    ArrayList<String> sampleTitles = new ArrayList<String>();
    ArrayList<String> sampleNetworkImageURLs = new ArrayList<String>();
    View root;
    Fragment mFrag;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        mFrag = this;
        startInit();
        return root;
    }

    public void startInit() {
        initUI();
        initEvent();
        initValue();
    }

    public void initUI() {
        carouselView = (CarouselView) root.findViewById(R.id.carouselView);
        carouselView.setResource(R.layout.view_custom);
        carouselView.setAutoPlay(true);
        carouselView.setIndicatorAnimationType(IndicatorAnimationType.THIN_WORM);
        carouselView.setCarouselOffset(OffsetType.CENTER);
        carouselView.setCarouselViewListener(new CarouselViewListener() {
            @Override
            public void onBindView(View view, final int position) {
                TextView labelTextView = (TextView) view.findViewById(R.id.labelTextView);
                ImageView fruitImageView = (ImageView) view.findViewById(R.id.fruitImageView);
                Picasso.with(mContext).load(sampleNetworkImageURLs.get(position)).placeholder(R.drawable.progress_animation).error(sampleImages[1]).fit().centerCrop().into(fruitImageView);
                labelTextView.setText(sampleTitles.get(position));
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int idBerita = semuaBerita.get(position).getId();
                        final String judulBerita = semuaBerita.get(position).getJudul();
                        final String isiBerita= semuaBerita.get(position).getIsi();
                        final String joindateBerita = semuaBerita.get(position).getJoindate();
                        final String authorBerita= semuaBerita.get(position).getAuthor();
                        final String gambar= semuaBerita.get(position).getGambar();
                        Intent goto_detail_berita = new Intent(mContext, DetailLokerAct.class);
                        goto_detail_berita.putExtra("id", idBerita);
                        goto_detail_berita.putExtra("judul", judulBerita);
                        goto_detail_berita.putExtra("isi", isiBerita);
                        goto_detail_berita.putExtra("joindate", joindateBerita);
                        goto_detail_berita.putExtra("author", authorBerita);
                        goto_detail_berita.putExtra("gambar", gambar);
                        mContext.startActivity(goto_detail_berita);
                    }
                });
            }
        });
        mContext = root.getContext();
        //declarasi RV
        mRecyclerView = (RecyclerView) root.findViewById(R.id.rv_berita);
        mRecyclerView.setNestedScrollingEnabled(true);
        mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);

        //declarasi button
        btn_modul       = root.findViewById(R.id.btn_modul);
        btn_video       = root.findViewById(R.id.btn_video);
        btn_tugas       = root.findViewById(R.id.btn_tugas);
        btn_berita      = root.findViewById(R.id.btn_berita);
        btn_info        = root.findViewById(R.id.btn_info);
        btn_kegiatan    = root.findViewById(R.id.btn_kegiatan);
        btn_produk      = root.findViewById(R.id.btn_produk);
        btn_more        = root.findViewById(R.id.btn_more);
        //declarasi info diri
        display_name    = root.findViewById(R.id.display_name);
        display_kelas   = root.findViewById(R.id.kelas);
        display_pict    = root.findViewById(R.id.myphoto);
    }

    public void initEvent() {
        btn_modul.setOnClickListener(this);
        btn_video.setOnClickListener(this);
        btn_tugas.setOnClickListener(this);
        btn_berita.setOnClickListener(this);
        btn_info.setOnClickListener(this);
        btn_kegiatan.setOnClickListener(this);
        btn_produk.setOnClickListener(this);
        btn_more.setOnClickListener(this);
    }

    public void initValue(){
        //set Display Name
        String nama = new PrefManager(mContext).getNama();
        display_name.setText(nama);
        //set Foto Profile
        String url = new PrefManager(mContext).getPhoto();
        if(!TextUtils.isEmpty(url)){
            Picasso.with(mContext).load(url).into(display_pict);
        }
        //set Kelas
        String kelas = new PrefManager(mContext).getKelas();
        display_kelas.setText(kelas);
        getBerita();
        getProduk();
    }

    private void getBerita() {

        Call<ResponseLoker> user = client.getApi().berita(5);
        user.enqueue(new Callback<ResponseLoker>() {
            @Override
            public void onResponse(Call<ResponseLoker> call, Response<ResponseLoker> response) {
                if (response.body().getStatus()){
                    semuaBerita = response.body().getData();
                    for (int i = 0; i < semuaBerita.size(); i++) {
                        final LokerItem brt = semuaBerita.get(i);
                        sampleNetworkImageURLs.add(IMG_LOWONGAN+brt.getGambar());
                        sampleTitles.add(brt.getJudul());
                    }
                    carouselView.setSize(semuaBerita.size());

                    carouselView.show();
                }else{
                    Toast.makeText(mContext, "!200", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onFailure(Call<ResponseLoker> call, Throwable t) {
                Toast.makeText(mContext, "NConn", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void getProduk() {
        Call<ResponseProduk> produk = client.getApi().produk("slider");
        produk.enqueue(new Callback<ResponseProduk>() {
            @Override
            public void onResponse(Call<ResponseProduk> call, Response<ResponseProduk> response) {
                if (response.body().getStatus()){
                    List<ProdukItem> semuaProduk = response.body().getData();
                    mAdapter = new AdapterProduk(semuaProduk, mContext);
                    mRecyclerView.setAdapter(mAdapter);
                    mRecyclerView.setLayoutManager(mLayoutManager);
                }else{
                    Toast.makeText(mContext, "!200", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onFailure(Call<ResponseProduk> call, Throwable t) {
                Toast.makeText(mContext, "NConn", Toast.LENGTH_LONG).show();
            }
        });
    }

//    ViewListener viewListener = new ViewListener() {
//        @Override
//        public View setViewForPosition(int position) {
//
//            View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
//
//            TextView labelTextView = (TextView) customView.findViewById(R.id.labelTextView);
//            ImageView fruitImageView = (ImageView) customView.findViewById(R.id.fruitImageView);
//            Picasso.with(mContext).load(sampleNetworkImageURLs.get(position)).placeholder(R.drawable.progress_animation).error(sampleImages[1]).fit().centerCrop().into(fruitImageView);
//            labelTextView.setText(sampleTitles.get(position));
//            carouselView.setIndicatorGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
//
//            return customView;
//        }
//    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_modul:
                move.moveActivity(mContext, ModulAct.class);
                break;
            case R.id.btn_video:
                move.moveActivity(mContext, VideoAct.class);
                break;
            case R.id.btn_tugas:
                move.moveActivity(mContext, TugasAct.class);
                break;
            case R.id.btn_berita:
                move.moveActivity(mContext, LokerAct.class);
                break;
            case R.id.btn_info:
                move.moveActivity(mContext, InfoAct.class);
                break;
            case R.id.btn_kegiatan:
                move.moveActivity(mContext, KegiatanAct.class);
                break;
            case R.id.btn_produk:
                move.moveActivity(mContext, Produk2Act.class);
                break;
            case R.id.btn_more:
                move.moveActivity(mContext, MoreAct.class);
                break;
        }

    }
}