Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section headers, including DfP banners. #2

Merged
merged 9 commits into from
Oct 9, 2015
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ dependencies {
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:support-v13:21.0.0'
// Don't want version 5.2.08, as it's not yet available on devices
//compile 'com.google.android.gms:play-services-ads:8.0.+'
//compile 'com.google.android.gms:play-services-analytics:5.0.+'
compile 'com.google.android.gms:play-services:5.0.+'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'it.sephiroth.android.library.imagezoom:imagezoom:+'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.os.Handler;
import android.os.Looper;
import android.support.test.espresso.Espresso;
import android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.ActivityLifecycleCallback;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.test.espresso.action.ViewActions;
import android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.espresso.core.deps.guava.base.Charsets;
import android.support.test.espresso.core.deps.guava.io.Resources;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.util.Log;

import com.android.support.test.deps.guava.base.Charsets;
import com.android.support.test.deps.guava.io.Resources;
import com.dancedeets.android.models.FullEvent;

import org.json.JSONArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.text.StringContains.containsString;
import static org.hamcrest.core.StringContains.containsString;

/**
* Created by lambert on 2014/10/28.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
</intent-filter>
</receiver>

<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

</application>

</manifest>
26 changes: 26 additions & 0 deletions app/src/main/java/com/dancedeets/android/AdManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dancedeets.android;

import android.location.Location;

import com.facebook.AccessToken;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;

/**
* Created by lambert on 2015/10/08.
*/
public class AdManager {

public static PublisherAdRequest getAdRequest(Location location) {
PublisherAdRequest.Builder adRequestBuilder = new PublisherAdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("F92F46CF903B1E0BD86D386FC9813E7F") // Mike's Nexus 5
.setLocation(location);
AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
String userID = token.getUserId();
adRequestBuilder.setPublisherProvidedId(Hashing.md5(userID));
}
return adRequestBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public Object instantiateItem(ViewGroup container, int position) {
return fragment;
}

public EventInfoFragment getExistingItem(int i) {
if (i < mFragments.size()) {
return mFragments.get(i);
} else {
return null;
}
}

@Override
public Fragment getItem(int i) {
FullEvent event = mEventList.get(i);
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/dancedeets/android/EventListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ protected void onEventListFilled() {
mEmptyText.setVisibility(View.GONE);
}
mRetryButton.setVisibility(View.GONE);
eventAdapter.rebuildList(mBundled.mEventList);
setListAdapter(eventAdapter);
}

Expand Down Expand Up @@ -214,9 +215,10 @@ public void onClick(View view) {

mListDescription = (TextView) rootView.findViewById(R.id.event_list_description);

eventAdapter = new EventUIAdapter(inflater.getContext(), mBundled.mEventList, R.layout.event_row);
eventAdapter = new EventUIAdapter(inflater.getContext());
setListAdapter(null);

if (savedInstanceState != null && !mBundled.mWaitingForSearch) {
if (mBundled.mEventList.size() > 0 && !mBundled.mWaitingForSearch) {
onEventListFilled();
}

Expand Down Expand Up @@ -355,7 +357,12 @@ public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);

FullEvent event = mBundled.mEventList.get(position);
int translatedPosition = eventAdapter.translatePosition(position);
if (translatedPosition < 0) {
return;
}
FullEvent event = mBundled.mEventList.get(translatedPosition);

Log("onListItemClick: fb event id: " + event.getId());

VolleySingleton volley = VolleySingleton.getInstance();
Expand All @@ -369,7 +376,7 @@ public void onListItemClick(ListView listView, View view, int position,
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
if (mCallbacks != null) {
mCallbacks.onEventSelected(mBundled.mEventList, position);
mCallbacks.onEventSelected(mBundled.mEventList, translatedPosition);
}
}

Expand Down
119 changes: 107 additions & 12 deletions app/src/main/java/com/dancedeets/android/EventUIAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dancedeets.android;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -12,13 +13,18 @@
import com.dancedeets.android.models.CoverData;
import com.dancedeets.android.models.FullEvent;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
* An Adapter for mapping Event objects to the Event ListView UI.
*/
public class EventUIAdapter extends BaseAdapter {

private static String LOG_TAG = "EventUIAdapter";

static class ViewBinder {
NetworkImageView icon;
PlaceholderNetworkImageView cover;
Expand All @@ -28,28 +34,79 @@ static class ViewBinder {
TextView categories;
}
private LayoutInflater mInflater;
private List<FullEvent> mEventList;
private int mResource;
private List<Object> mSectionedEventList;
private List<Integer> mMapping;
private static int mSectionResource = 0;
private static int mResource = R.layout.event_row;

public enum ItemType {
HEADER(0),
EVENT(1);

private final int queryArg;
ItemType(int queryArg) {
this.queryArg = queryArg;
}
public int value() {
return queryArg;
}
}

static DateFormat localizedDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);

public EventUIAdapter(Context context, List<FullEvent> eventBundleList, int resource) {
public EventUIAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mEventList = eventBundleList;
mResource = resource;
}

public void rebuildList(List<FullEvent> eventBundleList) {
mMapping = new ArrayList<>();
mSectionedEventList = fillSectionedList(eventBundleList);
}

private List<Object> fillSectionedList(List<FullEvent> eventList) {
Calendar lastCal = Calendar.getInstance();
Calendar curCal = Calendar.getInstance();
List<Object> sectionedList = new ArrayList<>();
for (int i = 0; i < eventList.size(); i++) {
FullEvent event = eventList.get(i);
curCal.setTime(event.getStartTime());
if (i == 0 ||
curCal.get(Calendar.YEAR) != lastCal.get(Calendar.YEAR) ||
curCal.get(Calendar.DAY_OF_YEAR) != lastCal.get(Calendar.DAY_OF_YEAR)
) {
sectionedList.add(localizedDateFormat.format(event.getStartTimeLong()));
mMapping.add(-1);
}
sectionedList.add(event);
lastCal.setTime(event.getStartTime());
mMapping.add(i);
}
Log.i(LOG_TAG, " "+sectionedList);
return sectionedList;
}

public int translatePosition(int position) {
return mMapping.get(position);
}

public int getCount() {
return mEventList.size();
return mSectionedEventList.size();
}

@Override
public Object getItem(int position) {
return mEventList.get(position);
return mSectionedEventList.get(position);
}

@Override
public long getItemId(int position) {
// The facebook ID is not guaranteed to fit into a 'long',
// so we use the hashcode of the string, which should be good enough for uniqueness.
return Math.abs(mEventList.get(position).getId().hashCode());
if (getItemViewType(position) == ItemType.HEADER.value()) {
return Math.abs(mSectionedEventList.get(position).hashCode());
} else {
return Math.abs(((FullEvent) mSectionedEventList.get(position)).getId().hashCode());
}
}

@Override
Expand All @@ -58,6 +115,26 @@ public boolean hasStableIds() {
return true;
}

@Override
public int getViewTypeCount() {
return 2;
}

protected ItemType getItemType(int position) {
Object o = mSectionedEventList.get(position);
if (o instanceof FullEvent) {
return ItemType.EVENT;
}
else {
return ItemType.HEADER;
}
}

@Override
public int getItemViewType(int position) {
return getItemType(position).value();
}

protected void bindView(int position, View view) {
FullEvent event = (FullEvent)getItem(position);
ImageLoader thumbnailLoader = VolleySingleton.getInstance().getThumbnailLoader();
Expand Down Expand Up @@ -87,14 +164,32 @@ protected void bindView(int position, View view) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
if (getItemType(position) == ItemType.EVENT) {
return createEventViewFromResource(position, convertView, parent);
} else {
return createSectionViewFromResource(position, convertView, parent);
}
}

private View createSectionViewFromResource(int position, View convertView,
ViewGroup parent) {
TextView view;
if (convertView == null) {
view = (TextView) mInflater.inflate(parent.getResources().getLayout(android.R.layout.simple_list_item_1), null);
view.setTag("sticky");
} else {
view = (TextView) convertView;
}
view.setText((String)mSectionedEventList.get(position));

return view;
}

private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
private View createEventViewFromResource(int position, View convertView,
ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
view = mInflater.inflate(mResource, parent, false);
ViewBinder viewBinder = new ViewBinder();
//viewBinder.icon = (NetworkImageView)view.findViewById(R.id.event_list_icon);
viewBinder.cover = (PlaceholderNetworkImageView)view.findViewById(R.id.event_list_cover);
Expand Down
Loading