Commit 064203da authored by Robert Hitt's avatar Robert Hitt
Browse files

Pimped out the toolbar

parent 7366a38b
......@@ -3,8 +3,12 @@ package srct.whatsopen.ui.activities;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
......@@ -14,12 +18,15 @@ import butterknife.ButterKnife;
import io.realm.Realm;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.adapters.FacilityListAdapter;
public class DetailActivity extends AppCompatActivity {
@BindView(R.id.facility_name) TextView nameTextView;
MenuItem mFavoriteMenuItem;
private Facility mFacility;
@Override
......@@ -27,16 +34,70 @@ public class DetailActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getFacility(getIntent().getStringExtra("name"));
// Set up layout
ButterKnife.bind(this);
setUpViews(getIntent().getStringExtra("name"));
configureToolbar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_detail, menu);
mFavoriteMenuItem = menu.findItem(R.id.miFavorite);
if(mFacility.isFavorited())
mFavoriteMenuItem.setIcon(R.drawable.ic_fav_button_on_24dp);
else
mFavoriteMenuItem.setIcon(R.drawable.ic_fav_button_white_24dp);
return true;
}
private void setUpViews(String key) {
// Get facility from Realm
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.miFavorite:
toggleFavoriteStatus();
return true;
case R.id.miOptions:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Updates the UI and Realm data for mFacility
private void toggleFavoriteStatus() {
if(mFacility.isFavorited()) {
mFavoriteMenuItem.setIcon(R.drawable.ic_fav_button_white_24dp);
FacilityListAdapter.toggleFavoriteAsync(this, mFacility, false);
}
else {
mFavoriteMenuItem.setIcon(R.drawable.ic_fav_button_on_24dp);
FacilityListAdapter.toggleFavoriteAsync(this, mFacility, true);
}
}
// Queries Realm for the facility matching the key
private void getFacility(String key) {
Realm realm = Realm.getDefaultInstance();
mFacility = realm.where(Facility.class).equalTo("mName", key).findFirst();
realm.close();
}
// Configures the toolbar title, actions, etc
private void configureToolbar() {
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(mFacility.getName());
nameTextView.setText(mFacility.getName());
// Display back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
......@@ -8,6 +8,8 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import com.astuetz.PagerSlidingTabStrip;
......@@ -42,6 +44,10 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Configure toolbar
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
setSupportActionBar(toolbar);
// Get WhatsOpenClient singleton
WhatsOpenService service = WhatsOpenClient.getInstance();
callWhatsOpenAPI(service);
......@@ -57,6 +63,12 @@ public class MainActivity extends AppCompatActivity {
viewPager.setCurrentItem(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
// does not work currently
/*
......
......@@ -22,7 +22,6 @@ import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.activities.MainActivity;
import srct.whatsopen.ui.activities.DetailActivity;
/**
......@@ -63,10 +62,10 @@ public class FacilityListAdapter extends
}
if(facility.isFavorited()) {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
holder.favoriteButton.setImageResource(R.drawable.ic_fav_button_on_24dp);
}
else {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
holder.favoriteButton.setImageResource(R.drawable.ic_fav_button_off_24dp);
}
holder.data = facility;
......@@ -74,11 +73,41 @@ public class FacilityListAdapter extends
textView.setText(facility.getName());
}
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
public static void toggleFavoriteAsync(Context context, Facility facility,
final boolean status) {
Realm realm = Realm.getDefaultInstance();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = facility.getName();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
// have to requery for the object as it was created on a separate thread
Facility facility = bgRealm.where(Facility.class)
.equalTo("mName", facilityName).findFirst();
facility.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
}
}, null, null);
realm.close();
}
// Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.facility_name) TextView nameTextView;
@BindView(R.id.favorite_button) ImageButton favoriteButton;
@BindView(R.id.facility_name)
TextView nameTextView;
@BindView(R.id.favorite_button)
ImageButton favoriteButton;
public Facility data;
......@@ -87,7 +116,7 @@ public class FacilityListAdapter extends
ButterKnife.bind(this, itemView);
}
// expands to the facility's detail view
// transitions to the facility's detail view
@OnClick(R.id.text_layout)
public void expandFacilityView() {
Intent i = new Intent(context, DetailActivity.class);
......@@ -99,41 +128,13 @@ public class FacilityListAdapter extends
// toggles favorite status
@OnClick(R.id.favorite_button)
public void setFavorite(ImageButton favoriteButton) {
if(data.isFavorited()) {
favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
toggleFavoriteAsync(false);
if (data.isFavorited()) {
favoriteButton.setImageResource(R.drawable.ic_fav_button_off_24dp);
toggleFavoriteAsync(context, data, false);
} else {
favoriteButton.setImageResource(R.drawable.ic_fav_button_on_24dp);
toggleFavoriteAsync(context, data, true);
}
else {
favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
toggleFavoriteAsync(true);
}
}
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
void toggleFavoriteAsync(final boolean status) {
Realm realm = Realm.getDefaultInstance();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = data.getName();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
// have to requery for the object as it was created on a separate thread
Facility facility = bgRealm.where(Facility.class)
.equalTo("mName", facilityName).findFirst();
facility.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
}
}, null, null);
realm.close();
}
}
}
......
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>
......@@ -3,7 +3,13 @@
android:id="@+id/activity_detail"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/toolbar_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:text="TextView"
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/fragment_facility"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="srct.whatsopen.ui.activities.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_facility"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="srct.whatsopen.ui.activities.MainActivity">
<include
layout="@layout/toolbar_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
......
......@@ -27,8 +27,8 @@
<ImageButton android:id="@+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/favorite_button_off_24dp"
android:background="#00ffffff"
android:src="@drawable/ic_fav_button_off_24dp"
android:background="#00FFFFFF"
android:elevation="2dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:titleTextColor="@android:color/white"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"/>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/miFavorite"
android:icon="@drawable/ic_fav_button_white_24dp"
app:showAsAction="ifRoom"
android:title="Favorite"/>
<item
android:id="@+id/miOptions"
android:icon="@drawable/ic_options_24dp"
app:showAsAction="ifRoom"
android:title="Options"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/miOptions"
android:icon="@drawable/ic_options_24dp"
app:showAsAction="ifRoom"
android:title="Options"/>
</menu>
\ No newline at end of file
<resources>
<string name="app_name">WhatsOpen</string>
<string name="app_name">What\'s Open</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlHighlight">@color/tabButtonPressed</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
</style>
</resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment