Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

HardwareLayerSpringListener #68

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

package com.facebook.rebound;

import android.view.View;
import java.util.ArrayList;
import java.util.WeakHashMap;

/**
* Spring Listener with Hardware Layer implementation, it's useful if
* you use with complex transition in your views.
*/
public class HardwareLayerSpringListener implements SpringListener {

/**
* WeakHashMap can avoid any memory leak, needs to be tested.
*/
private WeakHashMap<Integer, View> weakViews = new WeakHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just use

List<WeakReference<View>> 


public HardwareLayerSpringListener(View... views) {
addViews(views);
}

public HardwareLayerSpringListener(WeakHashMap<Integer, View> weakViews) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This constructor seems unnecessary. I'd prefer to just have the var args version. Sharing a HashMap between a caller and this class seems fragile.

this.weakViews = weakViews;
}

public void addViews(ArrayList<View> views) {
int countViews = views.size();
for(int i = 0; i < countViews; i++) {
addView(i, views.get(i));
}
}

public void addViews(WeakHashMap<Integer, View> weakViews) {
int countViews = weakViews.size();
for(int i = 0; i < countViews; i++) {
addView(i, weakViews.get(i));
}
}

public void addViews(View... views) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be private, I don't see a reason to update the views dynamically

int countViews = views.length;
for(int i = 0; i < countViews; i++) {
addView(i, views[i]);
}
}

public void addView(int index, View view) {
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed

weakViews.put(index, view);
}

@Override
public void onSpringUpdate(Spring spring) {
}

/**
* Removes the all views of the Hardware Layer.
*
* @param spring the spring that's now resting.
*/
@Override
public void onSpringAtRest(Spring spring) {
for(View view : weakViews.values()) {
view.setLayerType(View.LAYER_TYPE_NONE, null);
}
}

/**
* Set the layer type of the all views to View.LAYER_TYPE_HARDWARE.
*
* @param spring the spring that has left its resting state.
*/
@Override
public void onSpringActivate(Spring spring) {
for(View view : weakViews.values()) {
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
}

@Override
public void onSpringEndStateChange(Spring spring) {
}

}