package com.lukekorth.android_500px;

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.view.ViewTreeObserver;

import com.lukekorth.android_500px.models.Photos;

import it.sephiroth.android.library.imagezoom.ImageViewTouch;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.JELLY_BEAN;

public class PhotoFragment extends Fragment {

    public static final String PHOTO_ID_KEY = "com.lukekorth.android_500px.PhotoFragment.PHOTO_ID_KEY";

    private Photos mPhoto;
    private boolean mPhotoSet;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPhoto = Photos.getPhoto(getArguments().getInt(PHOTO_ID_KEY));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.full_screen_photo, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();

        mPhotoSet = false;

        ViewTreeObserver viewTreeObserver = getView().getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (SDK_INT >= JELLY_BEAN) {
                        ViewTreeObserver viewTreeObserver = getView().getViewTreeObserver();
                        if (viewTreeObserver.isAlive()) {
                            viewTreeObserver.removeOnGlobalLayoutListener(this);
                        }
                    }

                    if (!mPhotoSet) {
                        ImageViewTouch photoView = (ImageViewTouch) getView().findViewById(R.id.photo);
                        WallpaperApplication.getPicasso(getActivity())
                                .load(mPhoto.imageUrl)
                                .resize(photoView.getWidth(), photoView.getHeight())
                                .centerInside()
                                .into(photoView);
                        mPhotoSet = true;
                    }
                }
            });
        }
    }

}

@@ -238,46 +238,58 @@
 up;%0A
+%0A
 import 
-android.view.ViewTreeObserver;%0A
+com.lukekorth.android_500px.helpers.Utils;
 %0Aimp
@@ -403,114 +403,8 @@
 h;%0A%0A
-import static android.os.Build.VERSION.SDK_INT;%0Aimport static android.os.Build.VERSION_CODES.JELLY_BEAN;%0A%0A
 publ
@@ -594,25 +594,33 @@
 ate 
-boolean mPhotoSet
+ImageViewTouch mImageView
 ;%0A%0A 
@@ -987,14 +987,19 @@
     
-return
+View view =
  inf
@@ -1067,343 +1067,101 @@
     
-%7D%0A%0A
     
-@Override%0A    public void onResume() %7B%0A        super.onResume();%0A%0A        mPhotoSet = false;%0A%0A        ViewTreeObserver viewTreeObserver = getView().
+mImageView = (Ima
 ge
-t
 ViewT
-reeObserver();%0A        if (viewTreeObserver.isAlive()) %7B%0A            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() %7B%0A            
+ouch) view.findViewById(R.id.photo);%0A%0A        return view;%0A   %7D%0A%0A
     
@@ -1170,28 +1170,16 @@
 verride%0A
-            
     publ
@@ -1192,513 +1192,46 @@
 d on
-GlobalLayout() %7B%0A                    if (SDK_INT %3E= JELLY_BEAN) %7B%0A                        ViewTreeObserver viewTreeObserver = getView().getViewTreeObserver();%0A                        if (viewTreeObserver.isAlive()) %7B%0A                            viewTreeObserver.removeOnGlobalLayoutListener(this);%0A                        %7D%0A                    %7D%0A%0A                    if (!mPhotoSet) %7B%0A                        ImageViewTouch photoView = (ImageViewTouch) getView().findViewById(R.id.photo);%0A                
+Resume() %7B%0A        super.onResume();%0A%0A
     
@@ -1297,32 +1297,16 @@
         
-          
-      
 .load(mP
@@ -1340,32 +1340,16 @@
     
-           
-     
 .resize(
 phot
@@ -1348,69 +1348,83 @@
 ize(
-photoView.getWidth
+Utils.getScreenWidth(getActivity
 ()
+)
 , 
-photoView.getHeight())%0A                
+Utils.getScreenHeight(getActivity()))%0A
     
@@ -1471,149 +1471,26 @@
     
-   
-             .into(photoView);%0A                        mPhotoSet = true;%0A                    %7D%0A                %7D%0A            %7D);%0A        %7D
+.into(mImageView);
 %0A   

Fix PhotoFragment crash