Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atroshchenko Sergey - @SerG3z #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/src/main/java/ru/yandex/yamblz/ui/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -15,7 +16,9 @@

public class MainActivity extends BaseActivity {

@Inject @Named(DeveloperSettingsModule.MAIN_ACTIVITY_VIEW_MODIFIER)
public static final String TAG_COTENT_FRAGMENT = "tag_content_fragment";
@Inject
@Named(DeveloperSettingsModule.MAIN_ACTIVITY_VIEW_MODIFIER)
ViewModifier viewModifier;

@SuppressLint("InflateParams") // It's okay in our case.
Expand All @@ -29,8 +32,23 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_frame_layout, new ContentFragment())
.replace(R.id.main_frame_layout, new ContentFragment(), TAG_COTENT_FRAGMENT)
.commit();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
((ContentFragment) getSupportFragmentManager()
.findFragmentByTag(TAG_COTENT_FRAGMENT)).onKeyUp();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
((ContentFragment) getSupportFragmentManager()
.findFragmentByTag(TAG_COTENT_FRAGMENT)).onKeyDown();
return true;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.yandex.yamblz.ui.adapters;

/**
* Created by user on 28.07.16.
*/

public interface ItemTouchHelperAdapter {
boolean onItemMove(int startPosition, int endPosition);
void onItemDismiss(int position);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ru.yandex.yamblz.ui.adapters;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import ru.yandex.yamblz.R;
import ru.yandex.yamblz.ui.fragments.FrameItemDecoration;

/**
* Created by user on 28.07.16.
*/

public class SimpleItemTouchCallback extends ItemTouchHelper.Callback {

private final ItemTouchHelperAdapter touchHelperAdapter;
private final FrameItemDecoration frameItemDecoration;
private Paint paint;
private Context context;

public SimpleItemTouchCallback(ItemTouchHelperAdapter touchHelperAdapter, FrameItemDecoration frameItemDecoration, Context context) {
this.touchHelperAdapter = touchHelperAdapter;
this.frameItemDecoration = frameItemDecoration;
this.context = context;
paint = new Paint();
paint.setColor(Color.RED);
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT;
final int swipeFlag = ItemTouchHelper.END;
return makeMovementFlags(dragFlag, swipeFlag);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
frameItemDecoration.setIndexSelectedItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
touchHelperAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
touchHelperAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.delete_icon);
View itemView = viewHolder.itemView;
float alpha = Math.abs(dX) / itemView.getWidth();
itemView.setTranslationX(dX);
if (dX >= itemView.getWidth()) {
paint.setAlpha(255);
drawable.setAlpha(255);
} else {
paint.setAlpha((int) (255 * alpha));
drawable.setAlpha((int) (255 * alpha));
}
c.drawRect(itemView.getLeft(), itemView.getTop(), itemView.getRight(),
itemView.getBottom(), paint);

final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int left = layoutManager.getDecoratedLeft(itemView);
int top = layoutManager.getDecoratedTop(itemView) + drawable.getMinimumHeight();
int bottom = layoutManager.getDecoratedBottom(itemView) - drawable.getMinimumHeight();
int right = layoutManager.getDecoratedLeft(itemView) + bottom - top;
drawable.setBounds(left, top, right, bottom);
drawable.draw(c);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}

@Override
public boolean isLongPressDragEnabled() {
return true;
}

@Override
public boolean isItemViewSwipeEnabled() {
return true;
}


}
20 changes: 20 additions & 0 deletions app/src/main/java/ru/yandex/yamblz/ui/fragments/Animation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.yandex.yamblz.ui.fragments;

import android.animation.ObjectAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Created by SerG3z on 06.08.16.
*/

public class Animation {

public static void animationAppearence(RecyclerView.ViewHolder holder) {
ObjectAnimator.ofFloat(
holder.itemView,
View.ROTATION_X, 0, 360)
.setDuration(1000)
.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.yandex.yamblz.ui.fragments;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
* Created by SerG3z on 06.08.16.
*/

public class AnimationAppearanceLayoutManager extends GridLayoutManager {

public AnimationAppearanceLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public AnimationAppearanceLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}

public AnimationAppearanceLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
return super.scrollVerticallyBy(dy, recycler, state);

}

@Override
public void removeAndRecycleView(View child, RecyclerView.Recycler recycler) {
super.removeAndRecycleView(child, recycler);
}

@Override
public void removeAndRecycleViewAt(int index, RecyclerView.Recycler recycler) {
super.removeAndRecycleViewAt(index, recycler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.yandex.yamblz.ui.fragments;

import android.animation.ObjectAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Created by user on 28.07.16.
*/

class AnimationAppearanceScrollListener extends RecyclerView.OnScrollListener {

private static final int DURATION = 1000;
private static final int ROTATION = 360;

private int tempFirstItemPosition;
private int tempLastItemPosition;

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();

if (dy < 0 && tempFirstItemPosition != firstVisibleItemPosition) {
onDrawAnimation(recyclerView, firstVisibleItemPosition,
firstVisibleItemPosition + layoutManager.getSpanCount());
}
if (dy > 0 && tempLastItemPosition != lastVisibleItemPosition) {
onDrawAnimation(recyclerView, lastVisibleItemPosition - layoutManager.getSpanCount() + 1,
lastVisibleItemPosition + 1);
}

tempFirstItemPosition = layoutManager.findFirstVisibleItemPosition();
tempLastItemPosition = layoutManager.findLastVisibleItemPosition();
}

private void onDrawAnimation(RecyclerView recyclerView, int startPos, int endPos) {
for (int i = startPos; i < endPos; i++) {
ObjectAnimator.ofFloat(
recyclerView.findViewHolderForAdapterPosition(i).itemView,
View.ROTATION_X, 0, ROTATION)
.setDuration(DURATION)
.start();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а останавливать эту анимацию когда будешь?

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
viewBinder = ButterKnife.bind(this, view);
}

protected void runOnUiThreadIfFragmentAlive(@NonNull Runnable runnable) {
void runOnUiThreadIfFragmentAlive(@NonNull Runnable runnable) {
if (Looper.myLooper() == Looper.getMainLooper() && isFragmentAlive()) {
runnable.run();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import ru.yandex.yamblz.R;
import ru.yandex.yamblz.ui.adapters.ItemTouchHelperAdapter;

class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentHolder> {
class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentHolder> implements ItemTouchHelperAdapter {

private final Random rnd = new Random();
private final List<Integer> colors = new ArrayList<>();

@Override
public ContentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item, parent, false));
ContentHolder contentHolder = new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item, parent, false));
contentHolder.itemView.setOnClickListener(v -> onChangeItemColor(contentHolder.getAdapterPosition()));
return contentHolder;
}

@Override
Expand All @@ -33,13 +37,37 @@ public int getItemCount() {
return Integer.MAX_VALUE;
}

private void onChangeItemColor(int position) {
if (position != RecyclerView.NO_POSITION) {
colors.set(position, getColor());
notifyItemChanged(position);
}
}

private Integer createColorForPosition(int position) {
if (position >= colors.size()) {
while (position >= colors.size()) {
colors.add(Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)));
}
return colors.get(position);
}

private int getColor() {
return Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
}

@Override
public boolean onItemMove(int startPosition, int endPosition) {
Collections.swap(colors, startPosition, endPosition);
notifyItemMoved(startPosition, endPosition);
return true;
}

@Override
public void onItemDismiss(int startPosition) {
colors.remove(startPosition);
notifyItemRemoved(startPosition);
}

static class ContentHolder extends RecyclerView.ViewHolder {
ContentHolder(View itemView) {
super(itemView);
Expand Down
Loading