/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nf2m.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.ProgressBar;
import com.nf2m.R;
import com.nf2m.util.ImageFetcher;
import com.nf2m.util.ImageWorker;
/**
* This fragment will populate the children of the ViewPager from .
*/
public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
@Nullable
private String mImageUrl;
private ImageView mImageView;
public static ProgressBar mProgress;
/**
* Factory method to generate UriObserver new instance of the fragment given an image number.
*
* @param imageUrl The image url to load
* @return A new instance of ImageDetailFragment with imageNum extras
*/
@NonNull
public static ImageDetailFragment newInstance(String imageUrl) {
final ImageDetailFragment f = new ImageDetailFragment();
final Bundle args = new Bundle();
args.putString(IMAGE_DATA_EXTRA, imageUrl);
f.setArguments(args);
return f;
}
/**
* Empty constructor as per the Fragment documentation
*/
public ImageDetailFragment() {
}
/**
* Populate image using UriObserver url from extras, use the convenience factory method
* {@link ImageDetailFragment#newInstance(String)} to create this fragment.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : null;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate and locate the main ImageView
final View v = inflater.inflate(R.layout.album_image_fragment, container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView);
mProgress = (ProgressBar) v.findViewById(R.id.progress);
mProgress.setVisibility(View.VISIBLE);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Use the parent activity to load the image asynchronously into the ImageView (so UriObserver single
// cache can be used over all pages in the ViewPager
ImageFetcher mImageFetcher = NowPlayingFragment.getImageFetcher();
mImageFetcher.loadImage(mImageUrl, mImageView);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mImageView != null) {
// Cancel any pending image work
ImageWorker.cancelWork(mImageView);
mImageView.setImageDrawable(null);
}
}
}