org.openmrs.mobile.activities.syncedpatients.SyncedPatientsActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.mobile.activities.syncedpatients.SyncedPatientsActivity.java

Source

/*
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

package org.openmrs.mobile.activities.syncedpatients;

import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

import org.openmrs.mobile.R;
import org.openmrs.mobile.activities.ACBaseActivity;
import org.openmrs.mobile.application.OpenMRS;
import org.openmrs.mobile.utilities.ApplicationConstants;
import org.openmrs.mobile.utilities.StringUtils;

public class SyncedPatientsActivity extends ACBaseActivity {

    private SyncedPatientsPresenter mPresenter;
    private SearchView searchView;
    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_patients);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            getSupportActionBar().setElevation(0);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        // Create fragment
        SyncedPatientsFragment syncedPatientsFragment = (SyncedPatientsFragment) getSupportFragmentManager()
                .findFragmentById(R.id.syncedPatientsContentFrame);
        if (syncedPatientsFragment == null) {
            syncedPatientsFragment = SyncedPatientsFragment.newInstance();
        }
        if (!syncedPatientsFragment.isActive()) {
            addFragmentToActivity(getSupportFragmentManager(), syncedPatientsFragment,
                    R.id.syncedPatientsContentFrame);
        }

        if (savedInstanceState != null) {
            query = savedInstanceState.getString(ApplicationConstants.BundleKeys.PATIENT_QUERY_BUNDLE, "");
            mPresenter = new SyncedPatientsPresenter(syncedPatientsFragment, query);
        } else {
            mPresenter = new SyncedPatientsPresenter(syncedPatientsFragment);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String query = searchView.getQuery().toString();
        outState.putString(ApplicationConstants.BundleKeys.PATIENT_QUERY_BUNDLE, query);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.find_locally_and_add_patients_menu, menu);

        // Search function
        MenuItem searchMenuItem = menu.findItem(R.id.actionSearchLocal);
        if (OpenMRS.getInstance().isRunningHoneycombVersionOrHigher()) {
            searchView = (SearchView) searchMenuItem.getActionView();
        } else {
            searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        }
        if (StringUtils.notEmpty(query)) {
            searchMenuItem.expandActionView();
            searchView.setQuery(query, true);
            searchView.clearFocus();
        }
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.clearFocus();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                mPresenter.setQuery(query);
                mPresenter.updateLocalPatientsList();
                return true;
            }
        });

        return true;
    }

}