net.jongrakko.zipsuri.fragment.PostDetailExpertFragment1.java Source code

Java tutorial

Introduction

Here is the source code for net.jongrakko.zipsuri.fragment.PostDetailExpertFragment1.java

Source

package net.jongrakko.zipsuri.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import net.jongrakko.zipsuri.R;
import net.jongrakko.zipsuri.ZipsuriApplication;
import net.jongrakko.zipsuri.activity.PhotoActivity;
import net.jongrakko.zipsuri.manager.ProgressManger;
import net.jongrakko.zipsuri.model.PostModel;
import net.jongrakko.zipsuri.view.CheckableButton;
import net.jongrakko.zipsuri.view.simpleimagedownloadview.SimpleImageDownloadView;
import net.jongrakko.zipsuri.view.simpleimagedownloadview.thread.ImageDownloadThread;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MoonJongRak on 2016. 5. 13..
 */
public class PostDetailExpertFragment1 extends Fragment implements View.OnClickListener, OnMapReadyCallback {

    private int[] postImageViewIds = { R.id.imageView_0, R.id.imageView_1, R.id.imageView_2, R.id.imageView_3,
            R.id.imageView_4, };
    private int[] postTextViewIds = { R.id.textView_0, R.id.textView_1, R.id.textView_2, R.id.textView_3,
            R.id.textView_4, };

    private int[] licenseButtonIds = { R.id.buttonLicense0, R.id.buttonLicense1, R.id.buttonLicense2,
            R.id.buttonLicense3, R.id.buttonLicense4, R.id.buttonLicense5, R.id.buttonLicense6, R.id.buttonLicense7,
            R.id.buttonLicense8, R.id.buttonLicense9, R.id.buttonLicense10, R.id.buttonLicense11,
            R.id.buttonLicense12, R.id.buttonLicense13, R.id.buttonLicense14, R.id.buttonLicense15 };
    ArrayList<String> urls = new ArrayList<String>();
    private View rootView;
    private ImageView mImageViewTitle;
    private PostModel mPostModel;
    private GoogleMap mGoogleMap;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_post_detail1_expert, container, false);

            if (ZipsuriApplication.getUserModel().getIsExpert()) {
                rootView.setBackgroundResource(R.color.colorBackgroundDark);
            } else {
                rootView.setBackgroundResource(R.color.colorBackgroundMint);
            }

            rootView.findViewById(R.id.layoutStatus).setVisibility(View.VISIBLE);

            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.googleMap);
            mapFragment.getMapAsync(this);
        }
        return rootView;
    }

    public void setData(PostModel mPostModel) {
        this.mPostModel = mPostModel;
        if (mPostModel == null) {
            ProgressManger.showToast("? .");
        }
        setLayout();
    }

    private void setLayout() {
        initImageView();
        ((TextView) rootView.findViewById(R.id.textViewContent)).setText(mPostModel.getContent());
        ((TextView) rootView.findViewById(R.id.textViewAddress)).setText(mPostModel.getAddress());
        setLicense();
        setDate();
        setMap();
        setStatus();
        setVideo();
        urls.clear();
        for (int id : postImageViewIds) {
            View v = rootView.findViewById(id);
            if (((SimpleImageDownloadView) v).getUrl() != null)
                urls.add(((SimpleImageDownloadView) v).getUrl());
        }
    }

    private void setVideo() {
        ImageView videoView = (ImageView) rootView.findViewById(R.id.imageViewVideo);
        if (mPostModel.getVideo() == null) {
            videoView.setVisibility(View.GONE);
        } else {
            videoView.setImageResource(R.drawable.ic_play_circle_outline_white_48dp);
            videoView.setOnClickListener(this);

        }
    }

    private void playVideo(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "video/*");
        startActivity(intent);
    }

    private void setStatus() {
        if (!ZipsuriApplication.getUserModel().getIsExpert()) {
            ((TextView) rootView.findViewById(R.id.textViewStatus)).setText(mPostModel.getStatusUserValue());
        } else {
            ((TextView) rootView.findViewById(R.id.textViewStatus)).setText(mPostModel.getStatusExpertValue());
        }

        ((TextView) rootView.findViewById(R.id.textViewRemain)).setText(mPostModel.getRemainTime());

    }

    private void initImageView() {
        List<String> paths = mPostModel.getImagePathUrl();
        mImageViewTitle = (ImageView) rootView.findViewById(R.id.imageViewTitle);
        mImageViewTitle.setOnClickListener(this);
        rootView.findViewById(R.id.imageViewRight).setOnClickListener(this);
        rootView.findViewById(R.id.imageViewLeft).setOnClickListener(this);
        for (int i = 0; i < postImageViewIds.length; i++) {
            try {
                final String imageUrl = paths.get(i);
                SimpleImageDownloadView imageView = (SimpleImageDownloadView) rootView
                        .findViewById(postImageViewIds[i]);
                imageView.setOnClickListener(this);
                imageView.setImageURL(imageUrl, 4);
                imageView.setPadding(10, 10, 10, 10);

                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            } catch (Exception e) {
                rootView.findViewById(postImageViewIds[i]).setVisibility(View.GONE);
                rootView.findViewById(postTextViewIds[i]).setVisibility(View.GONE);
            }
        }
    }

    private void setLicense() {
        for (int i = 0; i < PostModel.FLAG_LICENSE.length; i++) {
            if ((mPostModel.getLicense() & PostModel.FLAG_LICENSE[i]) != 0) {
                ((CheckableButton) rootView.findViewById(licenseButtonIds[i])).setChecked(true);
            } else {
                ((CheckableButton) rootView.findViewById(licenseButtonIds[i])).setChecked(false);
            }
            rootView.findViewById(licenseButtonIds[i]).setClickable(false);

        }
    }

    private void setDate() {
        ((TextView) rootView.findViewById(R.id.textViewDate))
                .setText(new SimpleDateFormat("yyyy-MM-dd").format(mPostModel.getEndDate()));
        ((TextView) rootView.findViewById(R.id.textViewTime))
                .setText(new SimpleDateFormat("HH:mm").format(mPostModel.getEndDate()));
    }

    private void titlePictureRight() {
        Object tag = mImageViewTitle.getTag();
        if (tag == null) {
            return;
        }

        for (int i = 0; i < postImageViewIds.length - 1; i++) {
            int id = postImageViewIds[i + 1];
            if (rootView.findViewById(id).getVisibility() == View.GONE) {
                return;
            }
            if (tag.equals(postImageViewIds[i])) {
                mImageViewTitle.setImageDrawable(((ImageView) rootView.findViewById(id)).getDrawable());
                mImageViewTitle.setTag(id);
                break;
            }
        }
    }

    private void titlePictureLeft() {
        Object tag = mImageViewTitle.getTag();
        if (tag == null) {
            return;
        }

        for (int i = 1; i < postImageViewIds.length; i++) {
            int id = postImageViewIds[i - 1];
            if (rootView.findViewById(id).getVisibility() == View.GONE) {
                return;
            }
            if (tag.equals(postImageViewIds[i])) {
                mImageViewTitle.setImageDrawable(((ImageView) rootView.findViewById(id)).getDrawable());
                mImageViewTitle.setTag(id);
                break;
            }
        }
    }

    @Override
    public void onClick(View v) {

        for (int id : postImageViewIds) {
            if (id == v.getId()) {
                Intent intent = new Intent(getContext(), PhotoActivity.class);
                intent.putExtra(PhotoActivity.INTENT_PHOTO, ((SimpleImageDownloadView) v).getUrl());
                intent.putExtra("photo_array", urls);
                startActivity(intent);
                return;
            }
        }

        switch (v.getId()) {
        case R.id.imageViewLeft:
            titlePictureLeft();
            break;
        case R.id.imageViewRight:
            titlePictureRight();
            break;
        case R.id.imageViewTitle:
            if (v.getTag() != null) {
                SimpleImageDownloadView imageView = (SimpleImageDownloadView) rootView
                        .findViewById((int) v.getTag());
                Intent intent = new Intent(getContext(), PhotoActivity.class);
                intent.putExtra(PhotoActivity.INTENT_PHOTO, imageView.getUrl());
                startActivity(intent);
            }
            break;
        case R.id.imageViewVideo:
            playVideo(mPostModel.getVideoUrl());
            break;
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.mGoogleMap = googleMap;
        this.mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.541, 126.986), 18));
        setMap();
    }

    private void setMap() {
        if (mGoogleMap != null) {
            mGoogleMap.clear();
            if (mPostModel == null) {
                this.mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.541, 126.986), 18));
                return;
            }
            LatLng mLocation = new LatLng(mPostModel.getLatitude(), mPostModel.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(mLocation);
            mGoogleMap.addMarker(markerOptions);
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mLocation, 18));
        }
    }

}