Skip to content

Commit

Permalink
Add next-previous image support.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdryomov committed Jul 11, 2014
1 parent ea5bd17 commit 4476343
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 47 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.+"
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/amahi/anywhere/AmahiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +53,7 @@
ServerFileWebActivity.class,
NavigationFragment.class,
ServerFilesFragment.class,
ServerFileImageFragment.class,
SettingsFragment.class,
AudioService.class,
VideoService.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> SUPPORTED_FORMATS;

Expand Down Expand Up @@ -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<ServerFile> getImageFiles() {
List<ServerFile> imageFiles = new ArrayList<ServerFile>();

for (ServerFile file : getFiles()) {
if (SUPPORTED_FORMATS.contains(file.getMime())) {
imageFiles.add(file);
}
}

return imageFiles;
}

private List<ServerFile> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http ://www.gnu.org/licenses/>.
*/

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<ServerFile> imageFiles;

public ServerFileImagePagerAdapter(FragmentManager fragmentManager, ServerShare imageShare, List<ServerFile> 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));
}
}
108 changes: 108 additions & 0 deletions src/main/java/org/amahi/anywhere/fragment/ServerFileImageFragment.java
Original file line number Diff line number Diff line change
@@ -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 <http ://www.gnu.org/licenses/>.
*/

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() {
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/amahi/anywhere/util/Fragments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
22 changes: 3 additions & 19 deletions src/main/res/layout/activity_server_file_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,7 @@
~ along with Amahi. If not, see <http ://www.gnu.org/licenses/>.
-->

<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animator"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
android:animateFirstView="false"
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager_images"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
android:id="@android:id/progress"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</ViewAnimator>
android:layout_height="match_parent"/>
Loading

0 comments on commit 4476343

Please sign in to comment.