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 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity">
<activity android:name=".ui.activities.MainActivity">
<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">
<intent-filter>
<action android:name="android.intent.action.DETAIL"/>
</intent-filter>
</activity>
</application>
</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;
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 RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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();
}
// 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) {
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 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);
// 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);
}
// 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) {
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;
......@@ -11,7 +12,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
......@@ -22,6 +22,8 @@ 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;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
......@@ -85,11 +87,13 @@ public class FacilityListAdapter extends
ButterKnife.bind(this, itemView);
}
// should expand to the facility's detail view
// expands to the facility's detail view
@OnClick(R.id.text_layout)
public void expandFacilityView() {
String name = data.getName();
Toast.makeText(context, name + " clicked", Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, DetailActivity.class);
i.putExtra("name", data.getName());
context.startActivity(i);
}
// toggles favorite status
......
......@@ -2,6 +2,7 @@ package srct.whatsopen.ui.fragments;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
......@@ -50,11 +51,15 @@ public class FacilityListFragment extends android.support.v4.app.Fragment {
@Override
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);
setUpRecyclerView(view);
return 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"?>
<LinearLayout
android:id="@+id/activity_main"
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.MainActivity">
tools:context="srct.whatsopen.ui.activities.MainActivity">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
android:id="@+id/fragment_facility"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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