Commit 7366a38b authored by Robert Hitt's avatar Robert Hitt
Browse files

Clicking on a facility now opens its detail view

parent 48d39478
...@@ -10,13 +10,18 @@ ...@@ -10,13 +10,18 @@
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity"> <activity android:name=".ui.activities.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".ui.activities.DetailActivity">
<intent-filter>
<action android:name="android.intent.action.DETAIL"/>
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package srct.whatsopen.ui.activities;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.Realm;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
public class DetailActivity extends AppCompatActivity {
@BindView(R.id.facility_name) TextView nameTextView;
private Facility mFacility;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ButterKnife.bind(this);
setUpViews(getIntent().getStringExtra("name"));
}
private void setUpViews(String key) {
// Get facility from Realm
Realm realm = Realm.getDefaultInstance();
mFacility = realm.where(Facility.class).equalTo("mName", key).findFirst();
realm.close();
nameTextView.setText(mFacility.getName());
}
}
package srct.whatsopen.ui; package srct.whatsopen.ui.activities;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager; import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity; import android.support.v4.app.FragmentTransaction;
import android.os.Bundle; import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.astuetz.PagerSlidingTabStrip; import android.support.v7.widget.RecyclerView;
import java.text.ParseException; import com.astuetz.PagerSlidingTabStrip;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.text.ParseException;
import java.util.Date; import java.text.SimpleDateFormat;
import java.util.List; import java.util.Calendar;
import java.util.Date;
import butterknife.ButterKnife; import java.util.List;
import io.realm.Realm; import butterknife.ButterKnife;
import io.realm.RealmList; import io.realm.Realm;
import retrofit2.Call;
import retrofit2.Callback; import io.realm.RealmList;
import retrofit2.Response; import retrofit2.Call;
import retrofit2.Callback;
import srct.whatsopen.R; import retrofit2.Response;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.service.WhatsOpenClient; import srct.whatsopen.R;
import srct.whatsopen.service.WhatsOpenService; import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.model.Facility; import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.ui.adapters.FacilityListFragmentPagerAdapter; import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.model.Facility;
public class MainActivity extends AppCompatActivity { import srct.whatsopen.ui.adapters.FacilityListFragmentPagerAdapter;
private RecyclerView mRecyclerView; public class MainActivity extends AppCompatActivity {
@Override private RecyclerView mRecyclerView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); @Override
setContentView(R.layout.activity_main); protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get WhatsOpenClient singleton
WhatsOpenService service = WhatsOpenClient.getInstance(); // Get WhatsOpenClient singleton
callWhatsOpenAPI(service); WhatsOpenService service = WhatsOpenClient.getInstance();
callWhatsOpenAPI(service);
// Get the ViewPager and set its PagerAdapter
ViewPager viewPager = ButterKnife.findById(this, R.id.view_pager); // Get the ViewPager and set its PagerAdapter
viewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager())); 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); // Now give the TabStrip the ViewPager
tabStrip.setTabPaddingLeftRight(0); PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setViewPager(viewPager); tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(viewPager);
viewPager.setCurrentItem(1);
} viewPager.setCurrentItem(1);
}
@Override
protected void onDestroy() { // does not work currently
super.onDestroy(); /*
private void setDefaultTab(ViewPager viewPager) {
} RealmResults<Facility> results = mRealm.where(Facility.class).equalTo("isFavorited", true)
.findAllAsync();
// does not work currently
/* if(results.size() == 0)
private void setDefaultTab(ViewPager viewPager) { viewPager.setCurrentItem(1);
RealmResults<Facility> results = mRealm.where(Facility.class).equalTo("isFavorited", true) else
.findAllAsync(); viewPager.setCurrentItem(0);
}
if(results.size() == 0) */
viewPager.setCurrentItem(1);
else // Gets a Call from the given Retrofit service, then asynchronously executes it
viewPager.setCurrentItem(0); // 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();
// Gets a Call from the given Retrofit service, then asynchronously executes it final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
// On success, copies the resulting facility list to the Realm DB
private void callWhatsOpenAPI(WhatsOpenService service) { Call<List<Facility>> call = service.facilityList();
// Get Realm and SharedPreference instances
final Realm realm = Realm.getDefaultInstance(); call.enqueue(new Callback<List<Facility>>() {
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); @Override
public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) {
Call<List<Facility>> call = service.facilityList(); List<Facility> facilities = response.body();
call.enqueue(new Callback<List<Facility>>() { for(Facility facility : facilities) {
@Override // Query SharedReferences for each Facility's favorite status. defaults to false
public void onResponse(Call<List<Facility>> call, Response<List<Facility>> response) { facility.setFavorited(pref.getBoolean(facility.getName(), false));
List<Facility> facilities = response.body(); facility.setOpen(getOpenStatus(facility));
}
for(Facility facility : facilities) {
// Query SharedReferences for each Facility's favorite status. defaults to false realm.beginTransaction();
facility.setFavorited(pref.getBoolean(facility.getName(), false)); realm.copyToRealmOrUpdate(facilities);
facility.setOpen(getOpenStatus(facility)); realm.commitTransaction();
}
realm.close();
realm.beginTransaction(); }
realm.copyToRealmOrUpdate(facilities);
realm.commitTransaction(); @Override
public void onFailure(Call<List<Facility>> call, Throwable t) {
realm.close(); // do some stuff
} 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();
// Uses the device time to determine which facilities should be open // have to mess with the current day value, as Calender.DAY_OF_WEEK
private boolean getOpenStatus(Facility facility) { // starts with Sunday as 1 and the Whats Open Api starts with Monday at 0
Calendar now = Calendar.getInstance(); int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList(); OpenTimes currentOpenTimes = null;
// have to mess with the current day value, as Calender.DAY_OF_WEEK for(OpenTimes o : openTimesList) {
// starts with Sunday as 1 and the Whats Open Api starts with Monday at 0 if(o.getStartDay() == currentDay || o.getEndDay() == currentDay)
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7; currentOpenTimes = o;
OpenTimes currentOpenTimes = null; }
for(OpenTimes o : openTimesList) { if(currentOpenTimes == null)
if(o.getStartDay() == currentDay || o.getEndDay() == currentDay) return false;
currentOpenTimes = o;
} SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
if(currentOpenTimes == null) try {
return false; Date startTime = sdf.parse(currentOpenTimes.getStartTime());
Date endTime = sdf.parse(currentOpenTimes.getEndTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // have to parse it from date to string to date. how fun
Date currentTime = sdf.parse(sdf.format(now.getTime()));
try {
Date startTime = sdf.parse(currentOpenTimes.getStartTime()); if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0)
Date endTime = sdf.parse(currentOpenTimes.getEndTime()); return true;
// have to parse it from date to string to date. how fun else
Date currentTime = sdf.parse(sdf.format(now.getTime())); return false;
} catch (ParseException pe) {
if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0) return false;
return true; }
else }
return false; }
} catch (ParseException pe) {
return false;
}
}
}
...@@ -2,6 +2,7 @@ package srct.whatsopen.ui.adapters; ...@@ -2,6 +2,7 @@ package srct.whatsopen.ui.adapters;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
...@@ -11,7 +12,6 @@ import android.view.View; ...@@ -11,7 +12,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
...@@ -22,6 +22,8 @@ import io.realm.Realm; ...@@ -22,6 +22,8 @@ import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter; import io.realm.RealmRecyclerViewAdapter;
import srct.whatsopen.R; import srct.whatsopen.R;
import srct.whatsopen.model.Facility; import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.activities.MainActivity;
import srct.whatsopen.ui.activities.DetailActivity;
/** /**
* Basic RecyclerView boilerplate, with some added Realm stuff * Basic RecyclerView boilerplate, with some added Realm stuff
...@@ -85,11 +87,13 @@ public class FacilityListAdapter extends ...@@ -85,11 +87,13 @@ public class FacilityListAdapter extends
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
} }
// should expand to the facility's detail view // expands to the facility's detail view
@OnClick(R.id.text_layout) @OnClick(R.id.text_layout)
public void expandFacilityView() { public void expandFacilityView() {
String name = data.getName(); Intent i = new Intent(context, DetailActivity.class);
Toast.makeText(context, name + " clicked", Toast.LENGTH_SHORT).show(); i.putExtra("name", data.getName());
context.startActivity(i);
} }
// toggles favorite status // toggles favorite status
......
...@@ -2,6 +2,7 @@ package srct.whatsopen.ui.fragments; ...@@ -2,6 +2,7 @@ package srct.whatsopen.ui.fragments;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
...@@ -50,11 +51,15 @@ public class FacilityListFragment extends android.support.v4.app.Fragment { ...@@ -50,11 +51,15 @@ public class FacilityListFragment extends android.support.v4.app.Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_facility, container, false); return inflater.inflate(R.layout.fragment_facility, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = ButterKnife.findById(view, R.id.rvFacilities); mRecyclerView = ButterKnife.findById(view, R.id.rvFacilities);
setUpRecyclerView(view); setUpRecyclerView(view);
return view;
} }
// Handles set up for the Recycler View // Handles set up for the Recycler View
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_detail"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/facility_name"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
android:id="@+id/activity_main" android:id="@+id/fragment_facility"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
tools:context="srct.whatsopen.ui.MainActivity"> tools:context="srct.whatsopen.ui.activities.MainActivity">
<com.astuetz.PagerSlidingTabStrip <com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs" android:id="@+id/tabs"
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout
android:id="@+id/activity_main" android:id="@+id/fragment_facility"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
......
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