itvector.wineguide.ui.wines.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for itvector.wineguide.ui.wines.MainActivity.java

Source

/*
 * Copyright 2016, The Android Open Source Project
 *
 * 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 itvector.wineguide.ui.wines;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import org.apache.commons.lang.StringUtils;

import java.util.Locale;

import butterknife.BindView;
import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
import io.realm.RealmResults;
import itvector.wineguide.R;
import itvector.wineguide.dao.models.WineModel;
import itvector.wineguide.dao.models.common.App;
import itvector.wineguide.ui.BaseActivity;
import itvector.wineguide.ui.adapters.WinesRecyclerViewAdapter;

public class MainActivity extends BaseActivity
        implements WineContract.View, WinesRecyclerViewAdapter.OnItemClickListener {

    @BindView(R.id.realm_recycler_view)
    RealmRecyclerView mRecyclerView;

    private WinesRecyclerViewAdapter mAdapter;

    private MainActivityPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String lang = Locale.getDefault().getLanguage();
        App.install(getApplicationContext(), lang);
        mPresenter = new MainActivityPresenter(this);
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void showWinesList(RealmResults<WineModel> wineModels) {
        mAdapter = new WinesRecyclerViewAdapter(getBaseContext(), wineModels);
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onShowDescription(WineModel wineModels) {
        AlertDialog builder = new AlertDialog.Builder(this).setCancelable(true)
                .setTitle(R.string.dialog_title_description)
                .setMessage(StringUtils.capitalize(wineModels.getWine_type()))
                .setPositiveButton(R.string.button_ok, (dialog, id) -> dialog.dismiss()).create();
        builder.show();

        TextView textView = (TextView) builder.findViewById(android.R.id.message);
        textView.setTextSize(getResources().getDimension(R.dimen.dialog_message_description));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    @Override
    public void onItemClick(int position) {
        mPresenter.actionShowDescription(position);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.help:
            showDialogInfo();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void showDialogInfo() {
        AlertDialog builder = new AlertDialog.Builder(this).setCancelable(true).setTitle(R.string.about_app)
                .setMessage(R.string.about_app_desk)
                .setPositiveButton(R.string.button_ok, (dialog, id) -> dialog.dismiss()).create();
        builder.show();
    }
}