Commit e09e499f authored by Robert Hitt's avatar Robert Hitt
Browse files

Merge branch 'detail-view' into 'master'

Detail view

See merge request !2
parents 1b9b464b 02778054
......@@ -2,7 +2,6 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/WhatsOpen.iml" filepath="$PROJECT_DIR$/WhatsOpen.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/whats-open-android.iml" filepath="$PROJECT_DIR$/whats-open-android.iml" />
</modules>
......
......@@ -32,6 +32,7 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'io.reactivex:rxandroid:1.2.0'
......
......@@ -10,13 +10,20 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity">
<activity android:name=".ui.activities.MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ui.activities.DetailActivity"
android:parentActivityName=".ui.activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.activities.MainActivity"/>
</activity>
</application>
</manifest>
\ No newline at end of file
package srct.whatsopen.ui.activities;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmList;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.ui.adapters.FacilityListAdapter;
public class DetailActivity extends AppCompatActivity {
@BindView(R.id.open_status) TextView openStatusTextView;
@BindView(R.id.open_duration) TextView openDurationTextView;
@BindView(R.id.location_text) TextView locationTextView;
@BindView(R.id.schedule_text) TextView scheduleTextView;
MenuItem mFavoriteMenuItem;
private Facility mFacility;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getFacility(getIntent().getStringExtra("name"));
// Set up layout
ButterKnife.bind(this);
configureToolbar();
fillTextViews();
}
@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;
}
@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());
// Display back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
// Display the content to the text views
private void fillTextViews() {
String statusText = mFacility.isOpen() ? "Open" : "Closed";
openStatusTextView.setText(statusText);
openDurationTextView.setText(getStatusDuration());
locationTextView.setText(mFacility.getLocation());
scheduleTextView.setText(Html.fromHtml(getSchedule()));
}
// Finds the next time the facility closes or opens and returns it
private String getStatusDuration() {
Calendar now = Calendar.getInstance();
RealmList<OpenTimes> openTimesList = mFacility.getMainSchedule().getOpenTimesList();
if(openTimesList.size() == 0)
return "No open time on schedule";
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
String durationMessage;
if(mFacility.isOpen()) {
String closingTime = openTimesList.get(currentDay).getEndTime();
closingTime = parseTo12HourTime(closingTime);
durationMessage = "Closes at " + closingTime;
return durationMessage;
}
// Check if the facility opens later today
if(currentDay < openTimesList.size()) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date currentTime = sdf.parse(sdf.format(now.getTime()));
Date startTime = sdf.parse(openTimesList.get(currentDay).getStartTime());
if(currentTime.compareTo(startTime) < 0) {
String openingTime = openTimesList.get(currentDay).getStartTime();
openingTime = parseTo12HourTime(openingTime);
return "Opens today at " + openingTime;
}
} catch (ParseException pe) {
pe.printStackTrace();
return "";
}
}
// Else return the opening time of the next day
int nextDay = (currentDay + 1) % openTimesList.size();
String nextDayStr = parseIntToDay(nextDay);
String openingTime = openTimesList.get(nextDay).getStartTime();
openingTime = parseTo12HourTime(openingTime);
durationMessage = "Opens on " + nextDayStr + " at " + openingTime;
return durationMessage;
}
// Parses 24 hour formatted time String to 12 hour formatted time String
private String parseTo12HourTime(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
final Date date = sdf.parse(time);
return new SimpleDateFormat("h:mm a").format(date);
} catch (ParseException pe) {
pe.printStackTrace();
return "";
}
}
// Parses an integer to a String of the day of the week
private String parseIntToDay(int day) {
switch(day) {
case 0:
return "Monday";
case 1:
return "Tuesday";
case 2:
return "Wednesday";
case 3:
return "Thursday";
case 4:
return "Friday";
case 5:
return "Saturday";
case 6:
return "Sunday";
default:
return "";
}
}
// Parses the schedule into an HTML string
private String getSchedule() {
RealmList<OpenTimes> openTimesList = mFacility.getMainSchedule().getOpenTimesList();
if(openTimesList.size() == 0)
return "No schedule available";
StringBuilder scheduleString = new StringBuilder();
boolean first = true;
for(OpenTimes o : openTimesList) {
if(first)
first = false;
else
scheduleString.append("<br/>");
scheduleString.append("<b>" + parseIntToDay(o.getStartDay()) + "</b>: ");
scheduleString.append(parseTo12HourTime(o.getStartTime()));
scheduleString.append(" - ");
scheduleString.append(parseTo12HourTime(o.getEndTime()));
}
return scheduleString.toString();
}
}
package srct.whatsopen.ui;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import com.astuetz.PagerSlidingTabStrip;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import srct.whatsopen.R;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.adapters.FacilityListFragmentPagerAdapter;
public class MainActivity extends AppCompatActivity {
private Realm mRealm;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get Realm singleton
mRealm = Realm.getDefaultInstance();
// Get WhatsOpenClient singleton
WhatsOpenService service = WhatsOpenClient.getInstance();
callWhatsOpenAPI(service);
// Get the ViewPager and set its PagerAdapter
ViewPager viewPager = ButterKnife.findById(this, R.id.view_pager);
viewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(viewPager);
viewPager.setCurrentItem(1);
}
@Override
protected void onDestroy() {
super.onDestroy();
mRealm.close();
}
// does not work currently
/*
private void setDefaultTab(ViewPager viewPager) {
RealmResults<Facility> results = mRealm.where(Facility.class).equalTo("isFavorited", true)
.findAllAsync();
if(results.size() == 0)
viewPager.setCurrentItem(1);
else
viewPager.setCurrentItem(0);
}
*/
// Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB
private void callWhatsOpenAPI(WhatsOpenService service) {
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Call<List<Facility>> call = service.facilityList();
call.enqueue(new Callback<List<Facility>>() {
@Override
public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) {
List<Facility> facilities = response.body();
// Query SharedReferences for each Facility's favorite status. defaults to false
for(Facility facility : facilities) {
facility.setOpen(getOpenStatus(facility));
facility.setFavorited(pref.getBoolean(facility.getName(), false));
}
mRealm.beginTransaction();
mRealm.copyToRealmOrUpdate(facilities);
mRealm.commitTransaction();
}
@Override
public void onFailure(Call<List<Facility>> call, Throwable t) {
// do some stuff
}
});
}
// Uses the device time to determine which facilities should be open
private boolean getOpenStatus(Facility facility) {
Calendar now = Calendar.getInstance();
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
// have to mess with the current day value, as Calender.DAY_OF_WEEK
// starts with Saturday as 1 and the Whats Open Api starts with Monday
// at 0, for some reason.
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
OpenTimes currentOpenTimes = null;
for(OpenTimes o : openTimesList) {
if(o.getStartDay() == currentDay || o.getEndDay() == currentDay)
currentOpenTimes = o;
}
if(currentOpenTimes == null)
return false;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date startTime = sdf.parse(currentOpenTimes.getStartTime());
Date endTime = sdf.parse(currentOpenTimes.getEndTime());
// have to parse it from date to string to date. how fun
Date currentTime = sdf.parse(sdf.format(now.getTime()));
if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0)
return true;
else
return false;
} catch (ParseException pe) {
return false;
}
}
}
package srct.whatsopen.ui.activities;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import srct.whatsopen.R;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.adapters.FacilityListFragmentPagerAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
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);
// Get the ViewPager and set its PagerAdapter
ViewPager viewPager = ButterKnife.findById(this, R.id.view_pager);
viewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(viewPager);
viewPager.setCurrentItem(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
// does not work currently
/*
private void setDefaultTab(ViewPager viewPager) {
RealmResults<Facility> results = mRealm.where(Facility.class).equalTo("isFavorited", true)
.findAllAsync();
if(results.size() == 0)
viewPager.setCurrentItem(1);
else
viewPager.setCurrentItem(0);
}
*/
// Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB
private void callWhatsOpenAPI(WhatsOpenService service) {
// Get Realm and SharedPreference instances
final Realm realm = Realm.getDefaultInstance();
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Call<List<Facility>> call = service.facilityList();
call.enqueue(new Callback<List<Facility>>() {
@Override
public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) {
List<Facility> facilities = response.body();
for(Facility facility : facilities) {
// Query SharedReferences for each Facility's favorite status. defaults to false
facility.setFavorited(pref.getBoolean(facility.getName(), false));
facility.setOpen(getOpenStatus(facility));
}
realm.beginTransaction();
realm.copyToRealmOrUpdate(facilities);
realm.commitTransaction();
realm.close();
}
@Override
public void onFailure(Call<List<Facility>> call, Throwable t) {
// do some stuff
realm.close();
}
});
}
// Uses the device time to determine which facilities should be open
private boolean getOpenStatus(Facility facility) {
Calendar now = Calendar.getInstance();
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
// have to mess with the current day value, as Calender.DAY_OF_WEEK
// starts with Sunday as 1 and the Whats Open Api starts with Monday at 0
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
OpenTimes currentOpenTimes = null;
for(OpenTimes o : openTimesList) {
if(o.getStartDay() == currentDay || o.getEndDay() == currentDay)
currentOpenTimes = o;
}
if(currentOpenTimes == null)
return false;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date startTime = sdf.parse(currentOpenTimes.getStartTime());
Date endTime = sdf.parse(currentOpenTimes.getEndTime());
// have to parse it from date to string to date. how fun
Date currentTime = sdf.parse(sdf.format(now.getTime()));
if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0)
return true;
else
return false;
} catch (ParseException pe) {
pe.printStackTrace();
return false;
}
}
}
......@@ -2,6 +2,7 @@ package srct.whatsopen.ui.adapters;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
......@@ -21,6 +22,7 @@ import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter;
import srct.whatsopen.R;