diff --git a/build.gradle b/build.gradle index 0eb446b81..1fbf5f0a9 100644 --- a/build.gradle +++ b/build.gradle @@ -56,6 +56,7 @@ dependencies { } compile "com.android.support:support-v4:19.1.+" + compile "com.android.support:support-v13:19.1.+" compile "com.crashlytics.android:crashlytics:1.+" compile "com.jakewharton.timber:timber:2.3.+" compile "com.squareup.dagger:dagger:1.2.+" diff --git a/src/main/java/org/amahi/anywhere/AmahiModule.java b/src/main/java/org/amahi/anywhere/AmahiModule.java index c224362fe..a9f9d727d 100644 --- a/src/main/java/org/amahi/anywhere/AmahiModule.java +++ b/src/main/java/org/amahi/anywhere/AmahiModule.java @@ -28,6 +28,7 @@ import org.amahi.anywhere.activity.ServerFileWebActivity; import org.amahi.anywhere.activity.ServerFilesActivity; import org.amahi.anywhere.fragment.NavigationFragment; +import org.amahi.anywhere.fragment.ServerFileImageFragment; import org.amahi.anywhere.fragment.ServerFilesFragment; import org.amahi.anywhere.fragment.SettingsFragment; import org.amahi.anywhere.server.ApiModule; @@ -52,6 +53,7 @@ ServerFileWebActivity.class, NavigationFragment.class, ServerFilesFragment.class, + ServerFileImageFragment.class, SettingsFragment.class, AudioService.class, VideoService.class diff --git a/src/main/java/org/amahi/anywhere/activity/ServerFileImageActivity.java b/src/main/java/org/amahi/anywhere/activity/ServerFileImageActivity.java index 2393f8fa7..f3dc94beb 100644 --- a/src/main/java/org/amahi/anywhere/activity/ServerFileImageActivity.java +++ b/src/main/java/org/amahi/anywhere/activity/ServerFileImageActivity.java @@ -20,29 +20,27 @@ package org.amahi.anywhere.activity; import android.app.Activity; -import android.net.Uri; import android.os.Bundle; +import android.support.v4.view.ViewPager; import android.view.MenuItem; -import android.widget.ImageView; -import android.widget.ViewAnimator; - -import com.squareup.picasso.Callback; -import com.squareup.picasso.Picasso; import org.amahi.anywhere.AmahiApplication; import org.amahi.anywhere.R; +import org.amahi.anywhere.adapter.ServerFileImagePagerAdapter; import org.amahi.anywhere.server.client.ServerClient; import org.amahi.anywhere.server.model.ServerFile; import org.amahi.anywhere.server.model.ServerShare; import org.amahi.anywhere.util.Intents; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.inject.Inject; -public class ServerFileImageActivity extends Activity implements Callback +public class ServerFileImageActivity extends Activity implements ViewPager.OnPageChangeListener { public static final Set SUPPORTED_FORMATS; @@ -81,50 +79,70 @@ private void setUpHomeNavigation() { private void setUpImage() { setUpImageTitle(); - setUpImageContent(); + setUpImageAdapter(); + setUpImagePosition(); + setUpImageListener(); } private void setUpImageTitle() { - getActionBar().setTitle(getFile().getName()); + setUpImageTitle(getFile()); + } + + private void setUpImageTitle(ServerFile file) { + getActionBar().setTitle(file.getName()); } - private void setUpImageContent() { - Picasso - .with(this) - .load(getImageUri()) - .fit() - .centerInside() - .into(getImageView(), this); + private ServerFile getFile() { + return getIntent().getParcelableExtra(Intents.Extras.SERVER_FILE); } - private Uri getImageUri() { - return serverClient.getFileUri(getShare(), getFile()); + private void setUpImageAdapter() { + getImagePager().setAdapter(new ServerFileImagePagerAdapter(getFragmentManager(), getShare(), getImageFiles())); + } + + private ViewPager getImagePager() { + return (ViewPager) findViewById(R.id.pager_images); } private ServerShare getShare() { return getIntent().getParcelableExtra(Intents.Extras.SERVER_SHARE); } - private ServerFile getFile() { - return getIntent().getParcelableExtra(Intents.Extras.SERVER_FILE); + private List getImageFiles() { + List imageFiles = new ArrayList(); + + for (ServerFile file : getFiles()) { + if (SUPPORTED_FORMATS.contains(file.getMime())) { + imageFiles.add(file); + } + } + + return imageFiles; + } + + private List getFiles() { + return getIntent().getParcelableArrayListExtra(Intents.Extras.SERVER_FILES); + } + + private void setUpImagePosition() { + getImagePager().setCurrentItem(getImageFiles().indexOf(getFile())); } - private ImageView getImageView() { - return (ImageView) findViewById(R.id.image); + private void setUpImageListener() { + getImagePager().setOnPageChangeListener(this); } @Override - public void onSuccess() { - showImageContent(); + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } - private void showImageContent() { - ViewAnimator animator = (ViewAnimator) findViewById(R.id.animator); - animator.setDisplayedChild(animator.indexOfChild(findViewById(R.id.image))); + @Override + public void onPageScrollStateChanged(int state) { } @Override - public void onError() { + public void onPageSelected(int position) { + setUpImageTitle(getImageFiles().get(position)); } @Override diff --git a/src/main/java/org/amahi/anywhere/adapter/ServerFileImagePagerAdapter.java b/src/main/java/org/amahi/anywhere/adapter/ServerFileImagePagerAdapter.java new file mode 100644 index 000000000..fcf54e553 --- /dev/null +++ b/src/main/java/org/amahi/anywhere/adapter/ServerFileImagePagerAdapter.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2014 Amahi + * + * This file is part of Amahi. + * + * Amahi is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Amahi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Amahi. If not, see . + */ + +package org.amahi.anywhere.adapter; + +import android.app.Fragment; +import android.app.FragmentManager; +import android.support.v13.app.FragmentStatePagerAdapter; + +import org.amahi.anywhere.server.model.ServerFile; +import org.amahi.anywhere.server.model.ServerShare; +import org.amahi.anywhere.util.Fragments; + +import java.util.List; + +public class ServerFileImagePagerAdapter extends FragmentStatePagerAdapter +{ + private final ServerShare imageShare; + private final List imageFiles; + + public ServerFileImagePagerAdapter(FragmentManager fragmentManager, ServerShare imageShare, List imageFiles) { + super(fragmentManager); + + this.imageShare = imageShare; + this.imageFiles = imageFiles; + } + + @Override + public int getCount() { + return imageFiles.size(); + } + + @Override + public Fragment getItem(int imagePosition) { + return Fragments.Builder.buildServerFileImageFragment(imageShare, imageFiles.get(imagePosition)); + } +} diff --git a/src/main/java/org/amahi/anywhere/fragment/ServerFileImageFragment.java b/src/main/java/org/amahi/anywhere/fragment/ServerFileImageFragment.java new file mode 100644 index 000000000..c31011a7e --- /dev/null +++ b/src/main/java/org/amahi/anywhere/fragment/ServerFileImageFragment.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2014 Amahi + * + * This file is part of Amahi. + * + * Amahi is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Amahi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Amahi. If not, see . + */ + +package org.amahi.anywhere.fragment; + +import android.app.Fragment; +import android.net.Uri; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.ViewAnimator; + +import com.squareup.picasso.Callback; +import com.squareup.picasso.Picasso; + +import org.amahi.anywhere.AmahiApplication; +import org.amahi.anywhere.R; +import org.amahi.anywhere.server.client.ServerClient; +import org.amahi.anywhere.server.model.ServerFile; +import org.amahi.anywhere.server.model.ServerShare; +import org.amahi.anywhere.util.Fragments; + +import javax.inject.Inject; + +public class ServerFileImageFragment extends Fragment implements Callback +{ + @Inject + ServerClient serverClient; + + @Override + public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) { + return layoutInflater.inflate(R.layout.fragment_server_file_image, container, false); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + setUpInjections(); + + setUpImage(); + } + + private void setUpInjections() { + AmahiApplication.from(getActivity()).inject(this); + } + + private void setUpImage() { + setUpImageContent(); + } + + private void setUpImageContent() { + Picasso + .with(getActivity()) + .load(getImageUri()) + .fit() + .centerInside() + .into(getImageView(), this); + } + + private Uri getImageUri() { + return serverClient.getFileUri(getShare(), getFile()); + } + + private ServerShare getShare() { + return getArguments().getParcelable(Fragments.Arguments.SERVER_SHARE); + } + + private ServerFile getFile() { + return getArguments().getParcelable(Fragments.Arguments.SERVER_FILE); + } + + private ImageView getImageView() { + return (ImageView) getView().findViewById(R.id.image); + } + + @Override + public void onSuccess() { + showImageContent(); + } + + private void showImageContent() { + ViewAnimator animator = (ViewAnimator) getView().findViewById(R.id.animator); + animator.setDisplayedChild(animator.indexOfChild(getView().findViewById(R.id.image))); + } + + @Override + public void onError() { + } +} diff --git a/src/main/java/org/amahi/anywhere/util/Fragments.java b/src/main/java/org/amahi/anywhere/util/Fragments.java index 6dc3d9a42..88ba16b75 100644 --- a/src/main/java/org/amahi/anywhere/util/Fragments.java +++ b/src/main/java/org/amahi/anywhere/util/Fragments.java @@ -25,6 +25,7 @@ import android.os.Bundle; import org.amahi.anywhere.fragment.NavigationFragment; +import org.amahi.anywhere.fragment.ServerFileImageFragment; import org.amahi.anywhere.fragment.ServerFilesFragment; import org.amahi.anywhere.server.model.ServerFile; import org.amahi.anywhere.server.model.ServerShare; @@ -63,6 +64,18 @@ public static Fragment buildServerFilesFragment(ServerShare share, ServerFile di return filesFragment; } + + public static Fragment buildServerFileImageFragment(ServerShare share, ServerFile file) { + Fragment fileFragment = new ServerFileImageFragment(); + + Bundle arguments = new Bundle(); + arguments.putParcelable(Arguments.SERVER_SHARE, share); + arguments.putParcelable(Arguments.SERVER_FILE, file); + + fileFragment.setArguments(arguments); + + return fileFragment; + } } public static final class Operator diff --git a/src/main/res/layout/activity_server_file_image.xml b/src/main/res/layout/activity_server_file_image.xml index 6408406bf..2c73fbc81 100644 --- a/src/main/res/layout/activity_server_file_image.xml +++ b/src/main/res/layout/activity_server_file_image.xml @@ -18,23 +18,7 @@ ~ along with Amahi. If not, see . --> - - - - - - - \ No newline at end of file + android:layout_height="match_parent"/> \ No newline at end of file diff --git a/src/main/res/layout/fragment_server_file_image.xml b/src/main/res/layout/fragment_server_file_image.xml new file mode 100644 index 000000000..6408406bf --- /dev/null +++ b/src/main/res/layout/fragment_server_file_image.xml @@ -0,0 +1,40 @@ + + + + + + + + + + \ No newline at end of file