Android Open Source - wally Grid Fragment






From Project

Back to project page wally.

License

The source code is released under:

Apache License

If you think the Android project wally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
 *//  w w  w . j  a  v a  2  s.c  o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.musenkishi.wally.base;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.musenkishi.wally.R;
import com.musenkishi.wally.adapters.RecyclerImagesAdapter;
import com.musenkishi.wally.dataprovider.models.DataProviderError;
import com.musenkishi.wally.util.TextLinkBuilder;
import com.musenkishi.wally.views.AutoGridView;

/**
 * A base class where common logic for gridview based fragments is placed.
 * Created by Freddie (Musenkishi) Lust-Hed on 2014-10-07.
 */
public abstract class GridFragment extends BaseFragment {

    public static final short REQUEST_CODE = 25380;

    private ViewGroup errorLayout;
    protected AutoGridView gridView;
    protected View progressBar;
    protected RecyclerImagesAdapter imagesAdapter;

    protected int itemSize;
    protected String query;
    private GridLayoutManager gridLayoutManager;

    /**
     * Call this to setup basic views like the gridview.
     * @param rootView
     */
    protected void onCreateView(@NonNull View rootView) {
        errorLayout = (ViewGroup) rootView.findViewById(R.id.error_layout);
        gridView = (AutoGridView) rootView.findViewById(R.id.listview);

        gridLayoutManager = new GridLayoutManager(rootView.getContext(), 2);
        gridView.setLayoutManager(gridLayoutManager);
        gridView.getItemAnimator().setSupportsChangeAnimations(true);

        progressBar = rootView.findViewById(R.id.loader);
        if (progressBar != null) {
            progressBar.setAlpha(0.0f);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && imagesAdapter != null) {
            gridView.post(new Runnable() {
                @Override
                public void run() {
                    imagesAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    protected void showLoader() {
        progressBar.animate().alpha(1.0f).setDuration(300).start();
    }

    protected void hideLoader() {
        progressBar.animate().alpha(0.0f).setDuration(300).start();
    }

    protected void setupAutoSizeGridView() {
        final ViewTreeObserver vto = gridView.getViewTreeObserver();
        if (vto != null) {
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                int lastWidth = -1;

                @Override
                public void onGlobalLayout() {
                    int width = gridView.getWidth() - gridView.getPaddingLeft() - gridView.getPaddingRight();
                    if (width == lastWidth || width <= 0) {
                        return;
                    }

                    // Compute number of columns
                    int maxItemWidth = gridView.getDefaultCellWidth();
                    int numColumns = 1;
                    while (true) {
                        if (width / numColumns > maxItemWidth) {
                            ++numColumns;
                        } else {
                            break;
                        }
                    }

                    itemSize = width / numColumns;
                    if (imagesAdapter != null) {
                        imagesAdapter.setItemSize(itemSize);
                    }
                    gridLayoutManager.setSpanCount(numColumns);

                }
            });
        }
    }

    protected abstract void getImages(int index, String query);

    protected void showErrorMessage(DataProviderError dataProviderError, int index) {
        if (dataProviderError.getType().equals(DataProviderError.Type.LOCAL) && dataProviderError.getHttpStatusCode() == 204){
            //TODO Even if we don't show an error, it's still requesting images... find a way to stop it.
            if (index == 1){
                //No images was found with current filter and search settings

            }
        } else if (errorLayout != null) {

            int numberOfRetries = (errorLayout.getTag() instanceof Integer) ?
                    (Integer) errorLayout.getTag()
                    : 1;

            hideLoader();
            gridView.setVisibility(View.GONE);

            TextView message = (TextView) errorLayout.findViewById(R.id.error_backend_textview_message);
            TextView status = (TextView) errorLayout.findViewById(R.id.error_backend_textview_status);
            TextView checkBackend = (TextView) errorLayout.findViewById(R.id.error_backend_textview_check_backend);

            if (numberOfRetries >= 3) {
                TextLinkBuilder backendTextLinkBuilder = new TextLinkBuilder(
                        errorLayout.getContext(),
                        R.string.error_backend_check_backend,
                        R.string.error_backend_check_backend
                );
                backendTextLinkBuilder.color(getResources().getColor(R.color.Material_Blue_500));
                backendTextLinkBuilder.onClick(new TextLinkBuilder.OnTextClickedListener() {
                    @Override
                    public void onClick(View textView) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://alpha.wallhaven.cc"));
                        startActivity(browserIntent);
                    }
                });
                checkBackend.setMovementMethod(LinkMovementMethod.getInstance());
                checkBackend.setText(backendTextLinkBuilder.build());
                checkBackend.setVisibility(View.VISIBLE);
            } else {
                checkBackend.setVisibility(View.GONE);
            }

            TextLinkBuilder textLinkBuilder = new TextLinkBuilder(errorLayout.getContext(), R.string.error_backend_message_text, R.string.error_backend_message_text_link);
            textLinkBuilder.color(getResources().getColor(R.color.Material_Blue_500));
            message.setTag(index);
            textLinkBuilder.onClick(new TextLinkBuilder.OnTextClickedListener() {
                @Override
                public void onClick(View textView) {
                    int index = (Integer) textView.getTag();
                    errorLayout.setVisibility(View.GONE);
                    gridView.setVisibility(View.VISIBLE);
                    showLoader();
                    getImages(index, query);
                    int numberOfRetries = (errorLayout.getTag() instanceof Integer) ?
                            (Integer) errorLayout.getTag()
                            : 1;
                    numberOfRetries++;
                    errorLayout.setTag(numberOfRetries);
                }
            });
            message.setMovementMethod(LinkMovementMethod.getInstance());
            message.setText(textLinkBuilder.build());

            if (dataProviderError != null) {
                status.setText(dataProviderError.getHttpStatusCode() + " " + dataProviderError.getMessage());
            } else {
                status.setVisibility(View.GONE);
            }
            errorLayout.setVisibility(View.VISIBLE);
        }
    }

}




Java Source Code List

com.musenkishi.wally.activities.ImageDetailsActivity.java
com.musenkishi.wally.activities.MainActivity.java
com.musenkishi.wally.adapters.RecyclerImagesAdapter.java
com.musenkishi.wally.adapters.RecyclerSavedImagesAdapter.java
com.musenkishi.wally.adapters.SmartFragmentPagerAdapter.java
com.musenkishi.wally.adapters.SmartFragmentStatePagerAdapter.java
com.musenkishi.wally.anim.BaseItemAnimator.java
com.musenkishi.wally.anim.ScaleInOutItemAnimator.java
com.musenkishi.wally.anim.interpolator.EaseInOutBezierInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutLinearInInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutSlowInInterpolator.java
com.musenkishi.wally.anim.interpolator.LinearOutSlowInInterpolator.java
com.musenkishi.wally.base.BaseActivity.java
com.musenkishi.wally.base.BaseFragment.java
com.musenkishi.wally.base.GridFragment.java
com.musenkishi.wally.base.WallyApplication.java
com.musenkishi.wally.dataprovider.DataProvider.java
com.musenkishi.wally.dataprovider.FileManager.java
com.musenkishi.wally.dataprovider.NetworkDataProvider.java
com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider.java
com.musenkishi.wally.dataprovider.models.DataProviderError.java
com.musenkishi.wally.dataprovider.models.SaveImageRequest.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpStreamFetcher.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpUrlLoader.java
com.musenkishi.wally.dataprovider.util.Parser.java
com.musenkishi.wally.fragments.CustomResolutionDialogFragment.java
com.musenkishi.wally.fragments.FilterDialogFragment.java
com.musenkishi.wally.fragments.ImageZoomFragment.java
com.musenkishi.wally.fragments.LatestFragment.java
com.musenkishi.wally.fragments.MaterialDialogFragment.java
com.musenkishi.wally.fragments.RandomImagesFragment.java
com.musenkishi.wally.fragments.SavedImagesFragment.java
com.musenkishi.wally.fragments.SearchFragment.java
com.musenkishi.wally.fragments.ToplistFragment.java
com.musenkishi.wally.models.Author.java
com.musenkishi.wally.models.ExceptionReporter.java
com.musenkishi.wally.models.Filter.java
com.musenkishi.wally.models.ImagePage.java
com.musenkishi.wally.models.Image.java
com.musenkishi.wally.models.ListFilterGroup.java
com.musenkishi.wally.models.Rating.java
com.musenkishi.wally.models.SavedImageData.java
com.musenkishi.wally.models.Size.java
com.musenkishi.wally.models.Tag.java
com.musenkishi.wally.models.filters.FilterAspectRatioKeys.java
com.musenkishi.wally.models.filters.FilterBoardsKeys.java
com.musenkishi.wally.models.filters.FilterBoards.java
com.musenkishi.wally.models.filters.FilterGroup.java
com.musenkishi.wally.models.filters.FilterGroupsStructure.java
com.musenkishi.wally.models.filters.FilterPurityKeys.java
com.musenkishi.wally.models.filters.FilterPurity.java
com.musenkishi.wally.models.filters.FilterResOptKeys.java
com.musenkishi.wally.models.filters.FilterResOpt.java
com.musenkishi.wally.models.filters.FilterResolutionKeys.java
com.musenkishi.wally.models.filters.FilterTimeSpanKeys.java
com.musenkishi.wally.muzei.WallyArtSource.java
com.musenkishi.wally.notification.NotificationProvider.java
com.musenkishi.wally.observers.FileChangeReceiver.java
com.musenkishi.wally.observers.FiltersChangeReceiver.java
com.musenkishi.wally.util.Blur.java
com.musenkishi.wally.util.PaletteLoader.java
com.musenkishi.wally.util.PaletteRequest.java
com.musenkishi.wally.util.SparseBooleanArrayParcelable.java
com.musenkishi.wally.util.TextClickableSpan.java
com.musenkishi.wally.util.TextLinkBuilder.java
com.musenkishi.wally.util.TypefaceSpan.java
com.musenkishi.wally.views.AutoGridView.java
com.musenkishi.wally.views.GridRecyclerView.java
com.musenkishi.wally.views.ObservableScrollView.java
com.musenkishi.wally.views.TabBarView.java
com.musenkishi.wally.views.TabView.java
com.musenkishi.wally.views.swipeclearlayout.SwipeClearLayout.java
net.margaritov.preference.colorpicker.dialog.ColorPickerDialogFragment.java
net.margaritov.preference.colorpicker.drawable.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.preference.ColorPickerPreference.java
net.margaritov.preference.colorpicker.view.ColorPanelView.java
net.margaritov.preference.colorpicker.view.ColorPickerView.java
nl.codesoup.cubicbezier.CubicBezierInterpolator.java