net.d53.syman.mobile.ui.ContentListFragment.java Source code

Java tutorial

Introduction

Here is the source code for net.d53.syman.mobile.ui.ContentListFragment.java

Source

/*
 * Copyright 2012 GitHub Inc.
 *
 * 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 net.d53.syman.mobile.ui;

import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;
import com.github.kevinsawicki.wishlist.SingleTypeAdapter;
import com.google.inject.Inject;
import net.d53.syman.mobile.R.layout;
import net.d53.syman.mobile.R.string;
import net.d53.syman.mobile.SymanServiceProvider;
import net.d53.syman.mobile.core.Gauge;
import net.d53.syman.mobile.core.PageContent;

import java.util.List;

import static android.content.Intent.ACTION_VIEW;
import static net.d53.syman.mobile.IntentConstants.GAUGE_ID;

/**
 * Fragment to load page content information for a {@link Gauge}
 */
public class ContentListFragment extends ItemListFragment<PageContent> {

    @Inject
    private SymanServiceProvider serviceProvider;

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText(string.no_contents);
    }

    protected void configureList(Activity activity, ListView listView) {
        super.configureList(activity, listView);

        listView.setFastScrollEnabled(true);
        listView.setDividerHeight(0);

        getListAdapter().addHeader(activity.getLayoutInflater().inflate(layout.content_list_item_labels, null));
    }

    @Override
    public void onDestroyView() {
        setListAdapter(null);

        super.onDestroyView();
    }

    @Override
    public Loader<List<PageContent>> onCreateLoader(int id, Bundle args) {
        final List<PageContent> initialItems = items;
        return new ThrowableLoader<List<PageContent>>(getActivity(), items) {

            @Override
            public List<PageContent> loadData() throws Exception {
                try {
                    return serviceProvider.getService().getContent(getArguments().getString(GAUGE_ID));
                } catch (OperationCanceledException e) {
                    Activity activity = getActivity();
                    if (activity != null)
                        activity.finish();
                    return initialItems;
                }
            }
        };
    }

    @Override
    protected SingleTypeAdapter<PageContent> createAdapter(List<PageContent> items) {
        return new ContentListAdapter(getActivity().getLayoutInflater(), items);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String url = ((PageContent) l.getItemAtPosition(position)).getUrl();
        startActivity(new Intent(ACTION_VIEW, Uri.parse(url)));
    }

    @Override
    protected int getErrorMessage(Exception exception) {
        return string.error_loading_contents;
    }
}