Skip to content

Commit

Permalink
Refactored video listing into separate fragments and adapters, added …
Browse files Browse the repository at this point in the history
…favorites page.
  • Loading branch information
nessup committed Oct 29, 2013
1 parent 470495a commit 139e3fa
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package com.joumaa.workouthome;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.joumaa.workouthome.temporary.FavoritesPage;

/**
* Created by dany on 10/27/13.
*/
public class FavoritesFragment extends Fragment {
public class FavoritesFragment extends VideoListFragment {
private FavoritesPage page;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.favorites_fragment, container, false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);

page = FavoritesPage.buildTestFavoritesPage();
int noFavoritesVisibility = View.VISIBLE;
if (page.section.videos.size() > 0) {
configureWithSection(page.section);
noFavoritesVisibility = View.GONE;
}


return view;
}
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,28 @@
package com.joumaa.workouthome;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.joumaa.workouthome.model.SectionHeaderRow;
import com.joumaa.workouthome.model.VideoRow;
import com.joumaa.workouthome.temporary.FeaturedPage;
import com.joumaa.workouthome.temporary.Video;

/**
* Created by dany on 10/27/13.
*/
public class MainFragment extends Fragment {
public class MainFragment extends VideoListFragment {
private FeaturedPage featuredPage;

private ListView listView;
@Override
protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, container, false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
featuredPage = FeaturedPage.buildTestFeaturedPage();

View view = inflater.inflate(R.layout.main_fragment, container, false);

listView = (ListView) view.findViewById(R.id.listView);

View headerView = inflater.inflate(R.layout.list_view_header, listView, false);
SectionHeaderRow headerRow = SectionHeaderRow.newInstance(headerView);
headerRow.configureViewWithSection(featuredPage.mainSection);
listView.addHeaderView(headerView, null, false);

VideoListAdapter videoListAdapter = new VideoListAdapter(inflater);
listView.setAdapter(videoListAdapter);

configureWithSection(featuredPage.mainSection);
return view;
}

private void didSelectRowAtIndexPath(int index, View row) {
getFragmentManager()
.beginTransaction()
.replace(R.id.container, VideoInfoFragment.newInstance(Video.buildTestVideo()))
.addToBackStack(null)
.commit();
}

private class VideoListAdapter extends BaseAdapter {
LayoutInflater inflater;

private VideoListAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}

@Override
public int getCount() {
return featuredPage.mainSection.videos.size();
}

@Override
public Video getItem(int i) {
return featuredPage.mainSection.videos.get(i);
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(final int i, View view, ViewGroup parentViewGroup) {
VideoRow videoRow;
if (view == null) {
view = inflater.inflate(R.layout.video_row, parentViewGroup, false);
videoRow = VideoRow.newInstance(view);
videoRow.playButtonVisible = true;
videoRow.thumbnailHeight = getResources().getDimensionPixelSize(R.dimen.featured_thumbnail_height);
view.setTag(videoRow);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainFragment.this.didSelectRowAtIndexPath(i, view);
}
});
}
else {
videoRow = (VideoRow) view.getTag();
}

videoRow.configureWithVideo(getItem(i));

return view;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
layoutInflater = inflater;

View view = super.onCreateView(inflater, container, savedInstanceState);
findViews(view);
configureUI();

return view;
}

private void findViews(View view) {
@Override
public void findViews(View view) {
super.findViews(view);
videoRow = VideoRow.newInstance(view);
playButton = (ViewGroup) view.findViewById(R.id.playContainer);
myFavoritesButton = (ViewGroup) view.findViewById(R.id.myFavoriteContainer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.joumaa.workouthome;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.joumaa.workouthome.model.SectionHeaderRow;
import com.joumaa.workouthome.model.VideoRow;
import com.joumaa.workouthome.temporary.Section;
import com.joumaa.workouthome.temporary.Video;

/**
* Created by dany on 10/29/13.
*/
class VideoListAdapter extends BaseAdapter {
public interface OnVideoClickListener {
void onVideoClicked(int position, View rowView, Video video);
};

private ListView listView;
private LayoutInflater inflater;
private Section section;
private OnVideoClickListener onVideoClickListener;

public VideoListAdapter(ListView listView, LayoutInflater inflater, Section section) {
this.listView = listView;
this.inflater = inflater;
this.section = section;

// Section header
if (section.title != null && !section.title.isEmpty()) {
View headerView = inflater.inflate(R.layout.list_view_header, listView, false);
SectionHeaderRow headerRow = SectionHeaderRow.newInstance(headerView);
headerRow.configureViewWithSection(section);
listView.addHeaderView(headerView, null, false);
}

listView.setAdapter(this);
}

public void setOnVideoClickListener(OnVideoClickListener onVideoClickListener) {
this.onVideoClickListener = onVideoClickListener;
}

@Override
public int getCount() {
return section.videos.size();
}

@Override
public Video getItem(int i) {
return section.videos.get(i);
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(final int i, View view, ViewGroup parentViewGroup) {
VideoRow videoRow;
final Video video = getItem(i);
if (view == null) {
view = inflater.inflate(R.layout.video_row, parentViewGroup, false);
videoRow = VideoRow.newInstance(view);
videoRow.playButtonVisible = true;
videoRow.thumbnailHeight = listView.getResources().getDimensionPixelSize(R.dimen.featured_thumbnail_height);
view.setTag(videoRow);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onVideoClickListener != null) {
onVideoClickListener.onVideoClicked(i, view, video);
}
}
});
}
else {
videoRow = (VideoRow) view.getTag();
}

videoRow.configureWithVideo(video);

return view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.joumaa.workouthome;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.joumaa.workouthome.temporary.Section;
import com.joumaa.workouthome.temporary.Video;

/**
* Created by dany on 10/29/13.
*/
public abstract class VideoListFragment extends WOAHFragment implements VideoListAdapter.OnVideoClickListener {
private ListView listView;
private LayoutInflater inflater;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void findViews(View view) {
super.findViews(view);
listView = (ListView) view.findViewById(R.id.listView);
}

public void configureWithSection(Section section) {
VideoListAdapter videoListAdapter = new VideoListAdapter(listView, inflater, section);
videoListAdapter.setOnVideoClickListener(this);
listView.setAdapter(videoListAdapter);
}

// On video click listener

@Override
public void onVideoClicked(int position, View rowView, Video video) {
getFragmentManager()
.beginTransaction()
.replace(R.id.container, VideoInfoFragment.newInstance(Video.buildTestVideo()))
.addToBackStack(null)
.commit();
}

public ListView getListView() {
return listView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ public abstract class WOAHFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = createView(inflater, container, savedInstanceState);

this.titleView = (TextView) view.findViewById(R.id.navigationTitleView);

this.backButton = (ImageView) view.findViewById(R.id.navigationBackButton);
this.backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WOAHFragment.this.onBackPressed();
}
});

findViews(view);
return view;
}

public void findViews(View view) {
if (view.findViewById(R.id.fragmentNavigationBar) != null) {
this.titleView = (TextView) view.findViewById(R.id.navigationTitleView);

this.backButton = (ImageView) view.findViewById(R.id.navigationBackButton);
this.backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WOAHFragment.this.onBackPressed();
}
});
}
}

public TextView getTitleView() {
return titleView;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.joumaa.workouthome.temporary;

/**
* Created by dany on 10/29/13.
*/
public class FavoritesPage {
public Section section;

public static FavoritesPage buildTestFavoritesPage() {
FavoritesPage favoritesPage = new FavoritesPage();
favoritesPage.section = Section.buildTestSection();
return favoritesPage;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 139e3fa

Please sign in to comment.