Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xor.admissiontest.Guardian;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Typeface;
- import android.support.design.widget.AppBarLayout;
- import android.support.design.widget.CollapsingToolbarLayout;
- import android.support.design.widget.FloatingActionButton;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.DefaultItemAnimator;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.inputmethod.EditorInfo;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.orhanobut.dialogplus.DialogPlus;
- import com.orhanobut.dialogplus.ViewHolder;
- import com.xor.admissiontest.AppController;
- import com.xor.admissiontest.Guardian.paging.Item;
- import com.xor.admissiontest.Guardian.paging.RecyclerViewAdapter;
- import com.xor.admissiontest.R;
- import com.xor.admissiontest.SessionManager;
- import com.xor.admissiontest.Shared;
- import com.xor.admissiontest.URL;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- public class GuardianActivity extends AppCompatActivity {
- private String TAG = GuardianActivity.class.getSimpleName();
- private Context mContext = GuardianActivity.this;
- // Session Manager Class
- private SessionManager session;
- private RecyclerView mRecyclerView;
- private RecyclerViewAdapter mAdapter;
- ArrayList<Item> itemList = new ArrayList<>();
- private DialogPlus dialogPlus = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_guardian);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- // Session class instance
- session = new SessionManager(mContext);
- initCollapsingToolbar();
- mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
- mRecyclerView.setHasFixedSize(true);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
- mRecyclerView.setItemAnimator(new DefaultItemAnimator());
- mAdapter = new RecyclerViewAdapter(this, itemList);
- mRecyclerView.setAdapter(mAdapter);
- FloatingActionButton addPost = findViewById(R.id.fab);
- addPost.setOnClickListener(view -> {
- AlertDialog alertDialog = showPostDialog();
- alertDialog.show();
- });
- // make request to server
- getDataFromWeb();
- }
- /*
- * Make a request to server
- */
- private void getDataFromWeb() {
- final ProgressDialog dialog = new ProgressDialog(mContext);
- dialog.setTitle("Please wait...");
- dialog.setMessage("Loading nearby tutor...");
- dialog.show();
- String user_id = Shared.readData("UserID", mContext);
- JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URL.GET_NEAR_TUTORS_URL + user_id, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- Log.e(TAG, response.toString());
- dialog.dismiss();
- try {
- boolean success = response.getBoolean("success");
- if (success) {
- JSONArray jsonArray = response.getJSONArray("data");
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- Item item = new Item();
- item.setId(jsonObject.getString("id"));
- item.setName(jsonObject.getString("fullname"));
- item.setUniversity(jsonObject.getString("college"));
- item.setImage(jsonObject.getString("pic"));
- item.setSubject("Group: " + "Science" + ", Dept: " + jsonObject.getString("dept"));
- itemList.add(item);
- }
- mAdapter.notifyDataSetChanged();
- } else {
- Shared.showAlert("Info", response.getString("msg"), mContext, "Ok");
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- error.printStackTrace();
- }
- });
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(req);
- }
- private AlertDialog showPostDialog() {
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext, R.style.Theme_AppCompat_Light_Dialog_MinWidth);
- View view = getLayoutInflater().inflate(R.layout.custom_post_dialog_style, (ViewGroup) null);
- dialogBuilder.setTitle("Post for a Tutor");
- dialogBuilder.setCancelable(false);
- dialogBuilder.setView(view);
- EditText edt_class = view.findViewById(R.id.edt_class);
- EditText edt_group = view.findViewById(R.id.edt_group);
- EditText edt_school = view.findViewById(R.id.edt_school);
- EditText edt_description = view.findViewById(R.id.edt_description);
- dialogBuilder.setPositiveButton("Post", (dialogInterface, i) -> {
- dialogInterface.dismiss();
- String strClass = edt_class.getText().toString();
- String strGroup = edt_group.getText().toString();
- String strSchool = edt_school.getText().toString();
- String strDescription = edt_description.getText().toString();
- Shared.printMessage(strClass + ", " + strGroup + ", " + strSchool + ", " + strDescription, mContext);
- });
- dialogBuilder.setNegativeButton("Cancel", (dialogInterface, i) -> {
- dialogInterface.dismiss();
- });
- return dialogBuilder.create();
- }
- /*
- * Initializing collapsing toolbar
- * Will show and hide the toolbar title on scroll
- */
- private void initCollapsingToolbar() {
- Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/RealityHyperRegular.ttf");
- final CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
- collapsingToolbar.setCollapsedTitleTypeface(customFont);
- collapsingToolbar.setExpandedTitleTypeface(customFont);
- collapsingToolbar.setTitle(" ");
- AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
- appBarLayout.setExpanded(true);
- // hiding & showing the title when toolbar expanded & collapsed
- appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
- boolean isShow = false;
- int scrollRange = -1;
- @Override
- public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
- if (scrollRange == -1) {
- scrollRange = appBarLayout.getTotalScrollRange();
- }
- if (scrollRange + verticalOffset == 0) {
- collapsingToolbar.setTitle(getString(R.string.backdrop_title));
- isShow = true;
- } else if (isShow) {
- collapsingToolbar.setTitle(" ");
- isShow = false;
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_guardian, menu);
- // // Associate searchable configuration with the SearchView
- // SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
- // SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
- // searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getString(R.string.search_hint) + "</font>"));
- // searchView.setMaxWidth(Integer.MAX_VALUE);
- // assert searchManager != null;
- // searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
- // searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- // @Override
- // public boolean onQueryTextSubmit(String query) {
- // Log.e("onQueryTextSubmit", query);
- // return true;
- // }
- //
- // @Override
- // public boolean onQueryTextChange(String newText) {
- // Log.e("onQueryTextChange", newText);
- // return true;
- // }
- // });
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_search) {
- showAlert();
- return true;
- } else if (id == R.id.action_my_account) {
- startActivity(new Intent(mContext, GuardianProfileActivity.class));
- return true;
- } else if (id == R.id.action_logout) {
- session.logoutUser();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void showAlert() {
- dialogPlus = DialogPlus.newDialog(this)
- .setContentHolder(new ViewHolder(R.layout.top_sheet_dialog))
- .setGravity(Gravity.TOP)
- .setPadding(26, 16, 26, 16)
- .create();
- EditText edtSearch = dialogPlus.getHolderView().findViewById(R.id.edt_search);
- RadioGroup rg = dialogPlus.getHolderView().findViewById(R.id.holderRadioGroup);
- Button btnSearch = dialogPlus.getHolderView().findViewById(R.id.btn_search);
- edtSearch.setOnEditorActionListener((v, actionId, event) -> {
- if (actionId == EditorInfo.IME_ACTION_SEARCH) {
- String strSearch = v.getText().toString();
- String radioValue = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();
- performSearch(strSearch, radioValue);
- return true;
- }
- return false;
- });
- btnSearch.setOnClickListener(view -> {
- String strSearch = edtSearch.getText().toString();
- String radioValue = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();
- performSearch(strSearch, radioValue);
- });
- dialogPlus.show();
- edtSearch.requestFocus();
- showKeyboard();
- }
- private void performSearch(String strSearch, String value) {
- startActivity(new Intent(mContext, SearchResultsActivity.class)
- .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
- .putExtra("query", strSearch)
- .putExtra("group", value));
- closeKeyboard();
- dialogPlus.dismiss();
- }
- public void showKeyboard() {
- InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- assert inputMethodManager != null;
- inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
- }
- public void closeKeyboard() {
- InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- assert inputMethodManager != null;
- inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
- }
- @Override
- public void onBackPressed() {
- if (dialogPlus != null && dialogPlus.isShowing())
- dialogPlus.dismiss();
- else
- super.onBackPressed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement