Android Open Source - ShoppingList Shopping List Detail Fragment






From Project

Back to project page ShoppingList.

License

The source code is released under:

GNU General Public License

If you think the Android project ShoppingList 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

package net.sledzdev.shoppinglist;
//from   w w  w.  j av a2s.  c  o m
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.common.base.Optional;
import com.google.common.eventbus.EventBus;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import net.sledzdev.shoppinglist.adapter.DataModel;
import net.sledzdev.shoppinglist.adapter.ItemAdapter;
import net.sledzdev.shoppinglist.event.ClearListEvent;
import net.sledzdev.shoppinglist.event.EventBusFactory;
import net.sledzdev.shoppinglist.event.ListTitleChangedEvent;
import net.sledzdev.shoppinglist.event.NewItemEvent;
import net.sledzdev.shoppinglist.event.TextWatcherAdapter;
import net.sledzdev.shoppinglist.manager.ContentManager;
import net.sledzdev.shoppinglist.manager.OnUiThreadFutureCallback;
import net.sledzdev.shoppinglist.model.ShoppingItem;
import net.sledzdev.shoppinglist.model.ShoppingList;

public class ShoppingListDetailFragment extends Fragment {
    public static final String LIST_ID = "shopping_list_id";
    private final DetailFragmentHandlersRegister detailFragmentHandlersRegister = new DetailFragmentHandlersRegister(this);

    private ContentManager manager;
    private ItemAdapter currentItemAdapter;
    private ListDetailViews listDetailViews;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        manager = ContentManager.createContentManager(activity);

        detailFragmentHandlersRegister.registerEventHandlers();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_shoppinglist_detail, container, false);

        initViews(rootView);

        return rootView;
    }

    private void initViews(final View rootView) {
        Futures.addCallback(initList(), new FutureCallback<Optional<ShoppingList>>() {
            @Override
            public void onSuccess(Optional<ShoppingList> shoppingListOptional) {
                if (shoppingListOptional.isPresent()) {
                    initViewsForList(rootView, shoppingListOptional);
                } else {
                    Log.d("shopping app error", "list with id " + getArguments().getLong(LIST_ID) + " couldn't be found!");
                }
            }

            @Override
            public void onFailure(Throwable throwable) {
                Log.d("shopping app error", "exception: " + throwable);
            }
        });
    }

    private ListenableFuture<Optional<ShoppingList>> initList() {
        if (getArguments().containsKey(LIST_ID)) {
            final long listId = getArguments().getLong(LIST_ID);
            return manager.getList(listId);
        }

        Optional<ShoppingList> absent = Optional.absent();
        return Futures.immediateFuture(absent);
    }

    private void initViewsForList(View rootView, Optional<ShoppingList> shoppingListOptional) {
        final ShoppingList list = shoppingListOptional.get();

        listDetailViews = new ListDetailViews(rootView).invoke();

        updateViews(list);

        addEventListeners(list);
    }

    private void updateViews(ShoppingList list) {
        listDetailViews.getListTitle().setText(list.name);
        populateItemsList(list, listDetailViews.getItems());
    }

    private void populateItemsList(ShoppingList list, final ListView items) {
        ListenableFuture<DataModel<ShoppingItem>> future = manager.loadItems(list);
        Futures.addCallback(future, new OnUiThreadFutureCallback<DataModel<ShoppingItem>>(getActivity()) {

            @Override
            protected void onSuccessUiThread(DataModel<ShoppingItem> shoppingItems) {
                if (getActivity() != null) {
                    currentItemAdapter = new ItemAdapter(getActivity(), shoppingItems);
                    items.setAdapter(currentItemAdapter);
                }
            }

            @Override
            public void onFailure(Throwable throwable) {
                Log.d("shopping app error", "exception: " + throwable);
            }
        });
    }

    private void addEventListeners(final ShoppingList list) {
       final EventBus eventBus = EventBusFactory.getEventBus();

       listDetailViews.getListTitle().addTextChangedListener(new TextWatcherAdapter() {
           @Override
           public void afterTextChanged(Editable s) {
               eventBus.post(new ListTitleChangedEvent(manager, list, s.toString()));
           }
       });

       listDetailViews.getClearList().setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                eventBus.post(new ClearListEvent(currentItemAdapter, list, manager));
           }
       });

       listDetailViews.getNewItem().setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                eventBus.post(new NewItemEvent(currentItemAdapter, list, manager));
           }
       });
    }

    public ContentManager getManager() {
        return manager;
    }

    public ItemAdapter getCurrentItemAdapter() {
        return currentItemAdapter;
    }

    private class ListDetailViews {
        private View rootView;
        private EditText listTitle;
        private ListView items;
        private Button newItem;
        private Button clearList;

        public ListDetailViews(View rootView) {
            this.rootView = rootView;
        }

        public EditText getListTitle() {
            return listTitle;
        }

        public ListView getItems() {
            return items;
        }

        public Button getNewItem() {
            return newItem;
        }

        public Button getClearList() {
            return clearList;
        }

        public ListDetailViews invoke() {
            listTitle = (EditText) rootView.findViewById(R.id.listTitle);
            items = (ListView) rootView.findViewById(R.id.items);
            newItem = (Button) rootView.findViewById(R.id.add_new_item);
            clearList = (Button) rootView.findViewById(R.id.clear_list);
            return this;
        }
    }
}




Java Source Code List

net.sledzdev.shoppinglist.DetailFragmentHandlersRegister.java
net.sledzdev.shoppinglist.ItemDialogActivity.java
net.sledzdev.shoppinglist.ShoppingListDetailActivity.java
net.sledzdev.shoppinglist.ShoppingListDetailFragment.java
net.sledzdev.shoppinglist.ShoppingListsActivity.java
net.sledzdev.shoppinglist.ShoppingListsFragment.java
net.sledzdev.shoppinglist.adapter.DataModelAdapter.java
net.sledzdev.shoppinglist.adapter.DataModel.java
net.sledzdev.shoppinglist.adapter.ElementWithId.java
net.sledzdev.shoppinglist.adapter.ItemAdapterEventController.java
net.sledzdev.shoppinglist.adapter.ItemAdapter.java
net.sledzdev.shoppinglist.adapter.ListMapDataModelTest.java
net.sledzdev.shoppinglist.adapter.ListMapDataModel.java
net.sledzdev.shoppinglist.adapter.ShoppingListAdapterEventsHandler.java
net.sledzdev.shoppinglist.adapter.ShoppingListsAdapter.java
net.sledzdev.shoppinglist.content.DatabaseHelper.java
net.sledzdev.shoppinglist.content.DatabaseUriContentProvider.java
net.sledzdev.shoppinglist.content.ItemRowProvider.java
net.sledzdev.shoppinglist.content.ItemsProvider.java
net.sledzdev.shoppinglist.content.ItemsTable.java
net.sledzdev.shoppinglist.content.ListRowProvider.java
net.sledzdev.shoppinglist.content.ListsProvider.java
net.sledzdev.shoppinglist.content.ListsTable.java
net.sledzdev.shoppinglist.content.RowUriProvider.java
net.sledzdev.shoppinglist.content.ShoppingListProviderTest.java
net.sledzdev.shoppinglist.content.ShoppingListProvider.java
net.sledzdev.shoppinglist.content.ShoppingProviderContract.java
net.sledzdev.shoppinglist.content.TableUriProvider.java
net.sledzdev.shoppinglist.content.UriContentProvider.java
net.sledzdev.shoppinglist.event.ClearListEvent.java
net.sledzdev.shoppinglist.event.EventBusFactory.java
net.sledzdev.shoppinglist.event.ItemChangedEvent.java
net.sledzdev.shoppinglist.event.ItemCheckedChangedEvent.java
net.sledzdev.shoppinglist.event.ItemDeleteEvent.java
net.sledzdev.shoppinglist.event.ItemDialogCloseEvent.java
net.sledzdev.shoppinglist.event.ItemDialogOpenRequestEvent.java
net.sledzdev.shoppinglist.event.ListDeleteEvent.java
net.sledzdev.shoppinglist.event.ListSelectedEvent.java
net.sledzdev.shoppinglist.event.ListTitleChangedEvent.java
net.sledzdev.shoppinglist.event.NewItemEvent.java
net.sledzdev.shoppinglist.event.NewListEvent.java
net.sledzdev.shoppinglist.event.NewListRequestEvent.java
net.sledzdev.shoppinglist.event.TextWatcherAdapter.java
net.sledzdev.shoppinglist.event.UpdateListAdapterEvent.java
net.sledzdev.shoppinglist.handlers.ClearListEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemChangedEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemCheckedChangeEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemDeleteEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemDialogOpenRequestEventHandler.java
net.sledzdev.shoppinglist.handlers.ListDeleteEventHandler.java
net.sledzdev.shoppinglist.handlers.ListSelectedEventHandler.java
net.sledzdev.shoppinglist.handlers.ListTitleChangedEventHandler.java
net.sledzdev.shoppinglist.handlers.NewItemEventHandler.java
net.sledzdev.shoppinglist.handlers.NewListEventHandler.java
net.sledzdev.shoppinglist.manager.ContentManagerTests.java
net.sledzdev.shoppinglist.manager.ContentManager.java
net.sledzdev.shoppinglist.manager.ContentTransformer.java
net.sledzdev.shoppinglist.manager.ItemContentTransformer.java
net.sledzdev.shoppinglist.manager.ListsContentTransformer.java
net.sledzdev.shoppinglist.manager.MockContentManager.java
net.sledzdev.shoppinglist.manager.OnUiThreadFutureCallback.java
net.sledzdev.shoppinglist.model.ShoppingItemBuilderTest.java
net.sledzdev.shoppinglist.model.ShoppingItemBuilder.java
net.sledzdev.shoppinglist.model.ShoppingItem.java
net.sledzdev.shoppinglist.model.ShoppingListFactoryTest.java
net.sledzdev.shoppinglist.model.ShoppingListFactory.java
net.sledzdev.shoppinglist.model.ShoppingList.java