Android Open Source - iwannawatch-android Main Fragment






From Project

Back to project page iwannawatch-android.

License

The source code is released under:

MIT License

If you think the Android project iwannawatch-android 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 com.meoyawn.iwannawatch;
//from  ww w .  ja v a  2  s.co  m
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.meoyawn.iwannawatch.adapters.CursorMoviesAdapter;
import com.meoyawn.iwannawatch.adapters.DynamicMoviesAdapter;
import com.meoyawn.iwannawatch.doers.DatabaseHelper;
import com.meoyawn.iwannawatch.doers.TheMovieDbApi;
import com.meoyawn.iwannawatch.models.Movie;
import com.meoyawn.iwannawatch.qualifiers.Delete;
import com.meoyawn.iwannawatch.qualifiers.Done;
import com.neenbedankt.bundles.annotation.Argument;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Observable;
import rx.Observer;
import rx.android.observables.AndroidObservable;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import timber.log.Timber;

public class MainFragment extends Fragment {
    @Inject         TheMovieDbApi         theMovieDbApi;
    @Inject         DatabaseHelper        databaseHelper;
    @Inject @Done   PublishSubject<Movie> doneMovies;
    @Inject @Delete PublishSubject<Movie> deletedMovies;
    @Inject         ConnectivityManager   connectivityManager;

    @InjectView(R.id.grid)          GridView gridView;
    @InjectView(R.id.txt_add_more)  TextView textAddMore;
    @InjectView(R.id.btn_add_more)  TextView btnAddMore;
    @InjectView(android.R.id.empty) View     empty;

    final PublishSubject<Movie> newMovies = PublishSubject.create();
    @Nullable MenuItem searchMenuItem;

    @Argument boolean watched;
    boolean initialNavigationTrigger;
    int     scroll;

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainFragmentBuilder.injectArguments(this);
        if (!watched) {
            setHasOptionsMenu(true);
        }
        if (theMovieDbApi == null) {
            App.getObjectGraph(getActivity()).inject(this);
        }
    }

    @Override public void onStart() {
        super.onStart();
        EasyTracker tracker = EasyTracker.getInstance(getActivity());
        tracker.set(Fields.SCREEN_NAME, watched ? "Watched" : "Unwatched");
        tracker.send(MapBuilder.createAppView().build());
    }

    @Override public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(actionBar.getThemedContext(),
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.watched_status));
        actionBar.setListNavigationCallbacks(adapter, (i, l) -> {
            if (initialNavigationTrigger) {
                getFragmentManager().beginTransaction()
                        .replace(android.R.id.content, new MainFragmentBuilder(i == 1).build())
                        .commit();
            } else {
                initialNavigationTrigger = true;
            }
            return true;
        });
        actionBar.setSelectedNavigationItem(watched ? 1 : 0);
    }

    @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main, menu);
        searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        if (theMovieDbApi == null) {
            App.getObjectGraph(getActivity()).inject(this);
        }
        DynamicMoviesAdapter adapter = new DynamicMoviesAdapter(getActivity(),
                LayoutInflater.from(getActivity()), theMovieDbApi);
        searchView.setQueryHint(getString(R.string.movie_search));
        searchView.setSuggestionsAdapter(adapter);
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override public boolean onSuggestionSelect(int i) { return true; }

            @Override public boolean onSuggestionClick(int i) {
                newMovies.onNext(adapter.getItem(i));
                MenuItemCompat.collapseActionView(searchMenuItem);
                return true;
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState != null) {
            scroll = savedInstanceState.getInt("scroll");
        }
        ButterKnife.inject(this, view);
        gridView.setEmptyView(empty);

        if (watched) {
            textAddMore.setText(R.string.watched_empty);
            btnAddMore.setVisibility(View.GONE);
        } else {
            textAddMore.setText(R.string.unwatched_empty);
            btnAddMore.setVisibility(View.VISIBLE);
        }

        subscribe(databaseHelper.getMovies(watched));
        subscribe(newMovies.flatMap(databaseHelper.saveAndGetAll(watched)));
        subscribe(deletedMovies.flatMap(databaseHelper.deleteAndGetAll(watched)));
        subscribe(doneMovies.flatMap(databaseHelper.changeStatusAndGetAll(watched)));

        if (!isNetworkConnected()) {
            Toast.makeText(getActivity(), R.string.check_your_internet, Toast.LENGTH_LONG).show();
        }
    }

    @OnClick(R.id.btn_add_more) void addMore() {
        if (searchMenuItem != null) {
            MenuItemCompat.expandActionView(searchMenuItem);
        }
    }

    void subscribe(Observable<Cursor> cursorObservable) {
        AndroidObservable.fromFragment(this, cursorObservable.observeOn(Schedulers.io()))
                .subscribe(new Observer<Cursor>() {
                    @Override public void onCompleted() { }

                    @Override public void onError(Throwable e) {
                        Timber.e(e, "fuck");
                        setListAdapter(null);
                    }

                    @Override public void onNext(@NotNull Cursor cursor) {
                        if (getListAdapter() == null) {
                            setListAdapter(new CursorMoviesAdapter(getActivity(),
                                    LayoutInflater.from(getActivity()), cursor));
                            gridView.setSelection(scroll);
                        } else {
                            getListAdapter().setCursor(cursor);
                        }
                    }
                });
    }

    @Override public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("scroll", gridView.getFirstVisiblePosition());
    }

    @Override public void onDestroyView() {
        if (getListAdapter() != null) {
            getListAdapter().getCursor().close();
        }
        ButterKnife.reset(this);
        super.onDestroyView();
    }

    @Nullable CursorMoviesAdapter getListAdapter() {
        return (CursorMoviesAdapter) gridView.getAdapter();
    }

    void setListAdapter(ListAdapter listAdapter) {
        gridView.setAdapter(listAdapter);
    }

    boolean isNetworkConnected() {
        return connectivityManager.getActiveNetworkInfo() != null;
    }
}




Java Source Code List

com.meoyawn.iwannawatch.AppModule.java
com.meoyawn.iwannawatch.App.java
com.meoyawn.iwannawatch.CrashReportingTree.java
com.meoyawn.iwannawatch.DynamicHeightImageView.java
com.meoyawn.iwannawatch.MainActivity.java
com.meoyawn.iwannawatch.MainFragment.java
com.meoyawn.iwannawatch.MovieView.java
com.meoyawn.iwannawatch.adapters.BaseMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.CursorMoviesAdapter.java
com.meoyawn.iwannawatch.adapters.DynamicMoviesAdapter.java
com.meoyawn.iwannawatch.doers.DatabaseHelper.java
com.meoyawn.iwannawatch.doers.TheMovieDbApi.java
com.meoyawn.iwannawatch.models.Movie.java
com.meoyawn.iwannawatch.models.Search.java
com.meoyawn.iwannawatch.qualifiers.Delete.java
com.meoyawn.iwannawatch.qualifiers.Done.java