Skip to content

Commit

Permalink
Multiple improvements
Browse files Browse the repository at this point in the history
Some craches prevented
  • Loading branch information
Stevan Medic committed Oct 11, 2017
1 parent 8bbf02d commit a92dcf9
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 110 deletions.
2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created by smedic on 9.2.17..
*/

public class BaseFragment extends Fragment {
public abstract class BaseFragment extends Fragment {

protected void share(String url) {
Intent intent = new Intent(Intent.ACTION_SEND);
Expand All @@ -27,4 +27,6 @@ public void setUserVisibleHint(boolean visible) {
}
}

protected abstract void updateList();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.smedic.tubtub.MainActivity;
import com.smedic.tubtub.R;
Expand All @@ -47,6 +49,7 @@ public class FavoritesFragment extends BaseFragment implements ItemEventsListene
private VideosAdapter videoListAdapter;
private OnItemSelected itemSelected;
private Context context;
private RelativeLayout nothingFoundMessageHolder;

public FavoritesFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -74,8 +77,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
videoListAdapter.setOnItemEventsListener(this);
favoritesListView.setAdapter(videoListAdapter);

//disable swipe to refresh for this tab
v.findViewById(R.id.swipe_to_refresh).setEnabled(false);
nothingFoundMessageHolder = (RelativeLayout) v.findViewById(R.id.nothing_found_holder);

return v;
}

Expand All @@ -84,7 +87,7 @@ public void onResume() {
super.onResume();
favoriteVideos.clear();
favoriteVideos.addAll(YouTubeSqlDb.getInstance().videos(YouTubeSqlDb.VIDEOS_TYPE.FAVORITE).readAll());
videoListAdapter.notifyDataSetChanged();
updateList();
}

@Override
Expand All @@ -108,7 +111,7 @@ public void onDetach() {
*/
public void clearFavoritesList() {
favoriteVideos.clear();
videoListAdapter.notifyDataSetChanged();
updateList();
}

public void addToFavoritesList(YouTubeVideo video) {
Expand All @@ -118,7 +121,7 @@ public void addToFavoritesList(YouTubeVideo video) {
public void removeFromFavorites(YouTubeVideo video) {
YouTubeSqlDb.getInstance().videos(YouTubeSqlDb.VIDEOS_TYPE.FAVORITE).delete(video.getId());
favoriteVideos.remove(video);
videoListAdapter.notifyDataSetChanged();
updateList();
}

@Override
Expand All @@ -140,4 +143,19 @@ public void onItemClick(YouTubeVideo video) {
YouTubeSqlDb.getInstance().videos(YouTubeSqlDb.VIDEOS_TYPE.RECENTLY_WATCHED).create(video);
itemSelected.onPlaylistSelected(favoriteVideos, favoriteVideos.indexOf(video));
}

@Override
public void updateList() {
Log.d(TAG, "updateList: ");
videoListAdapter.notifyDataSetChanged();
if (videoListAdapter.getItemCount() > 0) {
Log.d(TAG, "updateList: show");
nothingFoundMessageHolder.setVisibility(View.GONE);
favoritesListView.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, "updateList: hide");
nothingFoundMessageHolder.setVisibility(View.VISIBLE);
favoritesListView.setVisibility(View.GONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.smedic.tubtub.MainActivity;
import com.smedic.tubtub.R;
Expand Down Expand Up @@ -55,9 +55,10 @@ public class PlaylistsFragment extends BaseFragment implements
private ArrayList<YouTubePlaylist> playlists;
private RecyclerView playlistsListView;
private PlaylistsAdapter playlistsAdapter;
private SwipeRefreshLayout swipeToRefresh;
//private SwipeRefreshLayout swipeToRefresh;
private Context context;
private OnItemSelected itemSelected;
private RelativeLayout nothingFoundMessageHolder;

public PlaylistsFragment() {
// Required empty public constructor
Expand All @@ -80,19 +81,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
/* Setup the ListView */
playlistsListView = (RecyclerView) v.findViewById(R.id.fragment_list_items);
playlistsListView.setLayoutManager(new LinearLayoutManager(context));

swipeToRefresh = (SwipeRefreshLayout) v.findViewById(R.id.swipe_to_refresh);
nothingFoundMessageHolder = (RelativeLayout) v.findViewById(R.id.nothing_found_holder);

playlistsAdapter = new PlaylistsAdapter(context, playlists);
playlistsAdapter.setOnItemEventsListener(this);
playlistsListView.setAdapter(playlistsAdapter);

swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
searchPlaylists();
}
});
// swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
// @Override
// public void onRefresh() {
// searchPlaylists();
// }
// });
return v;
}

Expand All @@ -117,7 +117,7 @@ public void onResume() {
super.onResume();
playlists.clear();
playlists.addAll(YouTubeSqlDb.getInstance().playlists().readAll());
playlistsAdapter.notifyDataSetChanged();
updateList();
}

public void searchPlaylists() {
Expand All @@ -130,7 +130,7 @@ public Loader<List<YouTubePlaylist>> onCreateLoader(final int id, final Bundle a
@Override
public void onLoadFinished(Loader<List<YouTubePlaylist>> loader, List<YouTubePlaylist> data) {
if (data == null) {
swipeToRefresh.setRefreshing(false);
//swipeToRefresh.setRefreshing(false);
return;
}
YouTubeSqlDb.getInstance().playlists().deleteAll();
Expand All @@ -142,20 +142,15 @@ public void onLoadFinished(Loader<List<YouTubePlaylist>> loader, List<YouTubePla

playlists.clear();
playlists.addAll(data);
playlistsAdapter.notifyDataSetChanged();
swipeToRefresh.setRefreshing(false);

for (YouTubePlaylist playlist : playlists) {
Log.d(TAG, "onLoadFinished: >>> " + playlist.getTitle());
}

//swipeToRefresh.setRefreshing(false);
updateList();
}

@Override
public void onLoaderReset(Loader<List<YouTubePlaylist>> loader) {
playlists.clear();
playlists.addAll(Collections.<YouTubePlaylist>emptyList());
playlistsAdapter.notifyDataSetChanged();
updateList();
}
}).forceLoad();
}
Expand Down Expand Up @@ -197,8 +192,7 @@ private void removePlaylist(final String playlistId) {
break;
}
}

playlistsAdapter.notifyDataSetChanged();
updateList();
}

/**
Expand Down Expand Up @@ -234,4 +228,16 @@ public void onItemClick(YouTubePlaylist youTubePlaylist) {
//results are in onVideosReceived callback method
acquirePlaylistVideos(youTubePlaylist.getId());
}

@Override
public void updateList() {
playlistsAdapter.notifyDataSetChanged();
if (playlistsAdapter.getItemCount() > 0) {
nothingFoundMessageHolder.setVisibility(View.GONE);
playlistsListView.setVisibility(View.VISIBLE);
} else {
nothingFoundMessageHolder.setVisibility(View.VISIBLE);
playlistsListView.setVisibility(View.GONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.smedic.tubtub.MainActivity;
import com.smedic.tubtub.R;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class RecentlyWatchedFragment extends BaseFragment implements
private OnItemSelected itemSelected;
private OnFavoritesSelected onFavoritesSelected;
private Context context;
private RelativeLayout nothingFoundMessageHolder;

public RecentlyWatchedFragment() {
// Required empty public constructor
Expand All @@ -76,9 +78,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
videoListAdapter.setOnItemEventsListener(this);
recentlyPlayedListView.setAdapter(videoListAdapter);

nothingFoundMessageHolder = (RelativeLayout) v.findViewById(R.id.nothing_found_holder);
//disable swipe to refresh for this tab
v.findViewById(R.id.swipe_to_refresh).setEnabled(false);

return v;
}

Expand All @@ -87,7 +88,7 @@ public void onResume() {
super.onResume();
recentlyPlayedVideos.clear();
recentlyPlayedVideos.addAll(YouTubeSqlDb.getInstance().videos(YouTubeSqlDb.VIDEOS_TYPE.RECENTLY_WATCHED).readAll());
videoListAdapter.notifyDataSetChanged();
updateList();
}

@Override
Expand All @@ -113,7 +114,7 @@ public void onDetach() {
*/
public void clearRecentlyPlayedList() {
recentlyPlayedVideos.clear();
videoListAdapter.notifyDataSetChanged();
updateList();
}

@Override
Expand All @@ -130,4 +131,16 @@ public void onFavoriteClicked(YouTubeVideo video, boolean isChecked) {
public void onItemClick(YouTubeVideo video) {
itemSelected.onPlaylistSelected(recentlyPlayedVideos, recentlyPlayedVideos.indexOf(video));
}

@Override
public void updateList() {
videoListAdapter.notifyDataSetChanged();
if (videoListAdapter.getItemCount() > 0) {
nothingFoundMessageHolder.setVisibility(View.GONE);
recentlyPlayedListView.setVisibility(View.VISIBLE);
} else {
nothingFoundMessageHolder.setVisibility(View.VISIBLE);
recentlyPlayedListView.setVisibility(View.GONE);
}
}
}
21 changes: 17 additions & 4 deletions app/src/main/java/com/smedic/tubtub/fragments/SearchFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.smedic.tubtub.MainActivity;
import com.smedic.tubtub.R;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class SearchFragment extends BaseFragment implements ItemEventsListener<Y
private Context context;
private OnItemSelected itemSelected;
private OnFavoritesSelected onFavoritesSelected;
private RelativeLayout nothingFoundMessageHolder;

public SearchFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -85,9 +87,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
videoListAdapter = new VideosAdapter(context, searchResultsList);
videoListAdapter.setOnItemEventsListener(this);
videosFoundListView.setAdapter(videoListAdapter);

nothingFoundMessageHolder = (RelativeLayout) v.findViewById(R.id.nothing_found_holder);
//disable swipe to refresh for this tab
v.findViewById(R.id.swipe_to_refresh).setEnabled(false);
return v;
}

Expand Down Expand Up @@ -137,15 +138,15 @@ public void onLoadFinished(Loader<List<YouTubeVideo>> loader, List<YouTubeVideo>
videosFoundListView.smoothScrollToPosition(0);
searchResultsList.clear();
searchResultsList.addAll(data);
videoListAdapter.notifyDataSetChanged();
loadingProgressBar.setVisibility(View.INVISIBLE);
updateList();
}

@Override
public void onLoaderReset(Loader<List<YouTubeVideo>> loader) {
searchResultsList.clear();
searchResultsList.addAll(Collections.<YouTubeVideo>emptyList());
videoListAdapter.notifyDataSetChanged();
updateList();
}
}).forceLoad();
}
Expand All @@ -166,4 +167,16 @@ public void onItemClick(YouTubeVideo video) {
//itemSelected.onVideoSelected(video);
itemSelected.onPlaylistSelected(searchResultsList, searchResultsList.indexOf(video));
}

@Override
public void updateList() {
videoListAdapter.notifyDataSetChanged();
if (videoListAdapter.getItemCount() > 0) {
nothingFoundMessageHolder.setVisibility(View.GONE);
videosFoundListView.setVisibility(View.VISIBLE);
} else {
nothingFoundMessageHolder.setVisibility(View.VISIBLE);
videosFoundListView.setVisibility(View.GONE);
}
}
}
14 changes: 8 additions & 6 deletions app/src/main/java/com/smedic/tubtub/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static String formatTo2Digits(String str) {

/**
* Prints videos nicely formatted
*
* @param videos
*/
public static void prettyPrintVideos(List<YouTubeVideo> videos) {
Expand All @@ -86,17 +87,18 @@ public static void prettyPrintVideos(List<YouTubeVideo> videos) {

/**
* Prints video nicely formatted
* @param playlistEntry
*
* @param videoEntry
*/
public static void prettyPrintVideoItem(YouTubeVideo playlistEntry) {
public static void prettyPrintVideoItem(YouTubeVideo videoEntry) {
Log.d(TAG, "*************************************************************");
Log.d(TAG, "\t\tItem:");
Log.d(TAG, "*************************************************************");

Log.d(TAG, " video name = " + playlistEntry.getTitle());
Log.d(TAG, " video id = " + playlistEntry.getId());
Log.d(TAG, " duration = " + playlistEntry.getDuration());
Log.d(TAG, " thumbnail = " + playlistEntry.getThumbnailURL());
Log.d(TAG, " video name = " + videoEntry.getTitle());
Log.d(TAG, " video id = " + videoEntry.getId());
Log.d(TAG, " duration = " + videoEntry.getDuration());
Log.d(TAG, " thumbnail = " + videoEntry.getThumbnailURL());
Log.d(TAG, "\n*************************************************************\n");
}

Expand Down
Loading

0 comments on commit a92dcf9

Please sign in to comment.