From 6f629cc5f5ee0d03275fd22f7d64ca3edecbaa33 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 29 Feb 2016 21:25:08 +0100 Subject: [PATCH] implement deleting files from viewpager --- .../gallery/activities/ViewPagerActivity.java | 49 +++++++++++++++++-- .../gallery/adapters/MyPagerAdapter.java | 10 +++- .../res/menu/{menu.xml => viewpager_menu.xml} | 6 ++- 3 files changed, 58 insertions(+), 7 deletions(-) rename app/src/main/res/menu/{menu.xml => viewpager_menu.xml} (65%) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 98e397599..da357ccf6 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.activities; import android.content.Intent; import android.database.Cursor; +import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; @@ -29,6 +30,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On private ActionBar actionbar; private List photos; private MyViewPager pager; + private String path; @Override protected void onCreate(Bundle savedInstanceState) { @@ -40,10 +42,10 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On actionbar = getSupportActionBar(); hideSystemUI(); - final String path = getIntent().getStringExtra(Constants.PHOTO); + path = getIntent().getStringExtra(Constants.PHOTO); final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); pager = (MyViewPager) findViewById(R.id.view_pager); - photos = getPhotos(path); + photos = getPhotos(); adapter.setPaths(photos); pager.setAdapter(adapter); pager.setCurrentItem(pos); @@ -63,7 +65,7 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu, menu); + getMenuInflater().inflate(R.menu.viewpager_menu, menu); return true; } @@ -73,6 +75,9 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On case R.id.menu_share: shareImage(); return true; + case R.id.menu_remove: + deleteImage(); + return true; default: return super.onOptionsItemSelected(item); } @@ -89,7 +94,43 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On startActivity(Intent.createChooser(sendIntent, shareTitle)); } - private List getPhotos(final String path) { + private void deleteImage() { + final String path = photos.get(pager.getCurrentItem()); + final File file = new File(path); + file.delete(); + + MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() { + @Override + public void onScanCompleted(final String path, final Uri uri) { + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadViewPager(); + } + }); + } + }); + } + + private void reloadViewPager() { + final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter(); + final int pos = pager.getCurrentItem(); + photos = getPhotos(); + if (photos.size() <= 0) { + finish(); + return; + } + + pager.setAdapter(null); + adapter.updateItems(photos); + pager.setAdapter(adapter); + + final int newPos = Math.min(pos, adapter.getCount()); + pager.setCurrentItem(newPos); + updateActionbarTitle(); + } + + private List getPhotos() { final List photos = new ArrayList<>(); final String fileDir = new File(path).getParent(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java index d3565d353..03c6cdab8 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java @@ -4,10 +4,10 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; -import java.util.List; - import com.simplemobiletools.gallery.ViewPagerFragment; +import java.util.List; + public class MyPagerAdapter extends FragmentStatePagerAdapter { private List paths; @@ -30,4 +30,10 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter { fragment.setPath(paths.get(position)); return fragment; } + + public void updateItems(List newPaths) { + paths.clear(); + paths.addAll(newPaths); + notifyDataSetChanged(); + } } diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/viewpager_menu.xml similarity index 65% rename from app/src/main/res/menu/menu.xml rename to app/src/main/res/menu/viewpager_menu.xml index 69a27ba98..d4899382f 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/viewpager_menu.xml @@ -1,10 +1,14 @@ + -