From bbab856e3fd0066705263764bb846b4a6d7b7b91 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 2 Jun 2017 10:30:53 +0300 Subject: [PATCH] decouple gallery image view into a fragment --- .../org/fox/ttrss/ArticleImageFragment.java | 181 ++++++++++++++++++ .../fox/ttrss/ArticleImagesPagerActivity.java | 161 ++-------------- ...s_image.xml => fragment_article_image.xml} | 0 3 files changed, 194 insertions(+), 148 deletions(-) create mode 100644 org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImageFragment.java rename org.fox.ttrss/src/main/res/layout/{article_images_image.xml => fragment_article_image.xml} (100%) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImageFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImageFragment.java new file mode 100644 index 00000000..f0055cd2 --- /dev/null +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImageFragment.java @@ -0,0 +1,181 @@ +package org.fox.ttrss; + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v4.app.Fragment; +import android.support.v4.view.ViewCompat; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.PopupMenu; +import android.widget.ProgressBar; + +import com.bogdwellers.pinchtozoom.ImageMatrixTouchHandler; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; +import com.bumptech.glide.load.resource.drawable.GlideDrawable; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.GlideDrawableImageViewTarget; +import com.bumptech.glide.request.target.Target; + +public class ArticleImageFragment extends Fragment { + private final String TAG = this.getClass().getSimpleName(); + + String m_url; + private ArticleImagesPagerActivity m_activity; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setRetainInstance(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_article_image, container, false); + + if (savedInstanceState != null) { + m_url = savedInstanceState.getString("url"); + } + + Log.d(TAG, "called for URL: " + m_url); + + ImageView imgView = (ImageView) view.findViewById(R.id.flavor_image); + + ImageMatrixTouchHandler touchHandler = new ImageMatrixTouchHandler(view.getContext()); + + imgView.setOnTouchListener(touchHandler); + + // shared element transitions stop GIFs from playing + if (m_url.toLowerCase().indexOf(".gif") == -1) { + ViewCompat.setTransitionName(imgView, "gallery:" + m_url); + } + + //registerForContextMenu(imgView); + + view.findViewById(R.id.flavor_image_overflow).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + PopupMenu popup = new PopupMenu(getContext(), v); + MenuInflater inflater = popup.getMenuInflater(); + inflater.inflate(R.menu.context_article_content_img, popup.getMenu()); + + popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + return onImageMenuItemSelected(item, m_url); + } + }); + + popup.show(); + + } + }); + + final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.flavor_image_progress); + final View errorMessage = view.findViewById(R.id.flavor_image_error); + + final GlideDrawableImageViewTarget glideImage = new GlideDrawableImageViewTarget(imgView); + + Glide.with(this) + .load(m_url) + .dontAnimate() + .diskCacheStrategy(DiskCacheStrategy.ALL) + .skipMemoryCache(false) + .listener(new RequestListener() { + @Override + public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorMessage.setVisibility(View.VISIBLE); + + ActivityCompat.startPostponedEnterTransition(m_activity); + return false; + } + + @Override + public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorMessage.setVisibility(View.GONE); + + ActivityCompat.startPostponedEnterTransition(m_activity); + return false; + } + }) + .into(glideImage); + + return view; + } + + public void initialize(String url) { + m_url = url; + } + + /*@Override + public boolean onContextItemSelected(MenuItem item) { + int position = m_pager.getCurrentItem(); + String url = m_checkedUrls.get(position); + + if (!onImageMenuItemSelected(item, url)) + return super.onContextItemSelected(item); + else + return true; + }*/ + + public boolean onImageMenuItemSelected(MenuItem item, String url) { + switch (item.getItemId()) { + case R.id.article_img_open: + if (url != null) { + try { + m_activity.openUri(Uri.parse(url)); + } catch (Exception e) { + e.printStackTrace(); + m_activity.toast(R.string.error_other_error); + } + } + return true; + case R.id.article_img_copy: + if (url != null) { + m_activity.copyToClipboard(url); + } + return true; + case R.id.article_img_share: + if (url != null) { + m_activity.shareText(url); + } + return true; + case R.id.article_img_view_caption: + if (url != null) { + m_activity.displayImageCaption(url, m_activity.m_content); + } + return true; + default: + Log.d(TAG, "onImageMenuItemSelected, unhandled id=" + item.getItemId()); + return false; + } + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + + //m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); + m_activity = (ArticleImagesPagerActivity) activity; + + } + + @Override + public void onSaveInstanceState (Bundle out) { + super.onSaveInstanceState(out); + + out.setClassLoader(getClass().getClassLoader()); + out.putString("url", m_url); + } + +} diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImagesPagerActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImagesPagerActivity.java index e58cdc5c..daed56a8 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImagesPagerActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/ArticleImagesPagerActivity.java @@ -1,39 +1,23 @@ package org.fox.ttrss; import android.annotation.SuppressLint; -import android.content.Context; import android.graphics.Bitmap; -import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.ActivityCompat; -import android.support.v4.view.PagerAdapter; -import android.support.v4.view.ViewCompat; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.ContextMenu; -import android.view.GestureDetector; -import android.view.LayoutInflater; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; -import android.widget.FrameLayout; -import android.widget.ImageView; -import android.widget.PopupMenu; import android.widget.ProgressBar; import com.ToxicBakery.viewpager.transforms.DepthPageTransformer; -import com.bogdwellers.pinchtozoom.ImageMatrixTouchHandler; import com.bumptech.glide.Glide; -import com.bumptech.glide.load.engine.DiskCacheStrategy; -import com.bumptech.glide.load.resource.drawable.GlideDrawable; -import com.bumptech.glide.request.RequestListener; -import com.bumptech.glide.request.target.GlideDrawableImageViewTarget; -import com.bumptech.glide.request.target.Target; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -53,16 +37,15 @@ public class ArticleImagesPagerActivity extends CommonActivity { private ArrayList m_checkedUrls; private String m_title; private ArticleImagesPagerAdapter m_adapter; - private String m_content; + public String m_content; private ProgressBar m_progress; private ViewPager m_pager; - private class ArticleImagesPagerAdapter extends PagerAdapter { + private class ArticleImagesPagerAdapter extends FragmentStatePagerAdapter { private List m_urls; - public ArticleImagesPagerAdapter(List urls) { - super(); - + public ArticleImagesPagerAdapter(FragmentManager fm, List urls) { + super(fm); m_urls = urls; } @@ -72,89 +55,14 @@ public class ArticleImagesPagerActivity extends CommonActivity { } @Override - public boolean isViewFromObject(View view, Object o) { - return view == o; - } + public Fragment getItem(int position) { + ArticleImageFragment frag = new ArticleImageFragment(); - @Override - public Object instantiateItem(ViewGroup container, int position) { - final String url = m_urls.get(position); + Log.d(TAG, "getItem: " + position + " " + m_urls.get(position)); - Log.d(TAG, "called for URL: " + url); + frag.initialize(m_urls.get(position)); - LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - View view = inflater.inflate(R.layout.article_images_image, null); - - ImageView imgView = (ImageView) view.findViewById(R.id.flavor_image); - - ImageMatrixTouchHandler touchHandler = new ImageMatrixTouchHandler(view.getContext()); - - imgView.setOnTouchListener(touchHandler); - - // shared element transitions stop GIFs from playing - if (position == 0 && url.toLowerCase().indexOf(".gif") == -1) { - ViewCompat.setTransitionName(imgView, "gallery:" + url); - } - - registerForContextMenu(imgView); - - view.findViewById(R.id.flavor_image_overflow).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - PopupMenu popup = new PopupMenu(ArticleImagesPagerActivity.this, v); - MenuInflater inflater = popup.getMenuInflater(); - inflater.inflate(R.menu.context_article_content_img, popup.getMenu()); - - popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - return onImageMenuItemSelected(item, url); - } - }); - - popup.show(); - - } - }); - - final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.flavor_image_progress); - final View errorMessage = view.findViewById(R.id.flavor_image_error); - - final GlideDrawableImageViewTarget glideImage = new GlideDrawableImageViewTarget(imgView); - - container.addView(view, 0); - - Glide.with(ArticleImagesPagerActivity.this) - .load(url) - .dontAnimate() - .diskCacheStrategy(DiskCacheStrategy.ALL) - .skipMemoryCache(false) - .listener(new RequestListener() { - @Override - public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - errorMessage.setVisibility(View.VISIBLE); - return false; - } - - @Override - public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - errorMessage.setVisibility(View.GONE); - - ActivityCompat.startPostponedEnterTransition(ArticleImagesPagerActivity.this); - return false; - } - }) - .into(glideImage); - - return view; - } - - @Override - public void destroyItem(ViewGroup container, int position, Object object) { - container.removeView((FrameLayout)object); + return frag; } } @@ -299,7 +207,7 @@ public class ArticleImagesPagerActivity extends CommonActivity { setTitle(m_title); - m_adapter = new ArticleImagesPagerAdapter(m_checkedUrls); + m_adapter = new ArticleImagesPagerAdapter(getSupportFragmentManager(), m_urls); m_pager = (ViewPager) findViewById(R.id.article_images_pager); m_pager.setAdapter(m_adapter); @@ -331,48 +239,5 @@ public class ArticleImagesPagerActivity extends CommonActivity { out.putString("content", m_content); } - @Override - public boolean onContextItemSelected(MenuItem item) { - int position = m_pager.getCurrentItem(); - String url = m_checkedUrls.get(position); - - if (!onImageMenuItemSelected(item, url)) - return super.onContextItemSelected(item); - else - return true; - } - - public boolean onImageMenuItemSelected(MenuItem item, String url) { - switch (item.getItemId()) { - case R.id.article_img_open: - if (url != null) { - try { - openUri(Uri.parse(url)); - } catch (Exception e) { - e.printStackTrace(); - toast(R.string.error_other_error); - } - } - return true; - case R.id.article_img_copy: - if (url != null) { - copyToClipboard(url); - } - return true; - case R.id.article_img_share: - if (url != null) { - shareText(url); - } - return true; - case R.id.article_img_view_caption: - if (url != null) { - displayImageCaption(url, m_content); - } - return true; - default: - Log.d(TAG, "onImageMenuItemSelected, unhandled id=" + item.getItemId()); - return false; - } - } } diff --git a/org.fox.ttrss/src/main/res/layout/article_images_image.xml b/org.fox.ttrss/src/main/res/layout/fragment_article_image.xml similarity index 100% rename from org.fox.ttrss/src/main/res/layout/article_images_image.xml rename to org.fox.ttrss/src/main/res/layout/fragment_article_image.xml