luxsyp.corona.com.base.ui.activity.HomeActivity.java Source code

Java tutorial

Introduction

Here is the source code for luxsyp.corona.com.base.ui.activity.HomeActivity.java

Source

/*
 * Copyright (C) 2014 Pedro Vicente Gmez Snchez.
 *
 * 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 luxsyp.corona.com.base.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import butterknife.InjectView;
import butterknife.Optional;
import luxsyp.corona.com.base.app.BaseActivity;
import luxsyp.corona.com.base.R;
import luxsyp.corona.com.base.ui.fragment.HomeFragment;
import luxsyp.corona.com.base.ui.fragment.MovieFragment;

public class HomeActivity extends BaseActivity {

    @Optional
    @InjectView(R.id.toolbar)
    Toolbar toolbar;

    @Optional
    @InjectView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;

    @Optional
    @InjectView(R.id.search_fab)
    FloatingActionButton search_fab;

    private HomeFragment homeFragment;

    private final static String HOME_FRAGMENT = "HOME_FRAGMENT ";

    // -------------------------------------------------------------------------------------------
    //                                      Lifecycle
    // -------------------------------------------------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_home, null);
        setContentView(view);
        injectViews();

        if (savedInstanceState != null && savedInstanceState.containsKey(HOME_FRAGMENT))
            homeFragment = (HomeFragment) getSupportFragmentManager().getFragment(savedInstanceState,
                    HOME_FRAGMENT);
        else
            homeFragment = new HomeFragment();

        initToolbar();
        initFragment();
        initSearchButton();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (homeFragment != null && getSupportFragmentManager().findFragmentById(R.id.container) == homeFragment)
            getSupportFragmentManager().putFragment(outState, HOME_FRAGMENT, homeFragment);
    }

    // -------------------------------------------------------------------------------------------
    //                                      Method(s)
    // -------------------------------------------------------------------------------------------

    private void initToolbar() {
        if (toolbar != null)
            setSupportActionBar(toolbar);
    }

    private void initFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, homeFragment);
        transaction.commit();
    }

    private void initSearchButton() {
        search_fab.setClickable(true);
        search_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search_fab.setClickable(true);
                search_fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            new MaterialDialog.Builder(HomeActivity.this).title(R.string.movie_name_search)
                                    .content(R.string.movie_name_search)
                                    .inputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME
                                            | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)
                                    .input(R.string.movie_name_search, R.string.no_prefill,
                                            new MaterialDialog.InputCallback() {
                                                @Override
                                                public void onInput(MaterialDialog dialog, CharSequence input) {
                                                    homeFragment.doSearch(input.toString());
                                                    InputMethodManager imm = (InputMethodManager) getSystemService(
                                                            Context.INPUT_METHOD_SERVICE);
                                                    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                                                }
                                            })
                                    .show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    // -------------------------------------------------------------------------------------------
    //                                      Override(s)
    // -------------------------------------------------------------------------------------------

    private Boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }
        doubleBackToExitPressedOnce = true;
        Toast.makeText(this, getString(R.string.back_exit), Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }

}