Skip to content

Commit

Permalink
Aspects in fragment, fixes #94 #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Santner committed Oct 26, 2016
1 parent 9d22e1f commit b9f7b32
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.data.DiasporaPodList;
import com.github.dfa.diaspora_android.data.PodUserProfile;
import com.github.dfa.diaspora_android.fragment.AspectListFragment;
import com.github.dfa.diaspora_android.fragment.BrowserFragment;
import com.github.dfa.diaspora_android.fragment.CustomFragment;
import com.github.dfa.diaspora_android.fragment.DiasporaStreamFragment;
Expand Down Expand Up @@ -286,6 +287,10 @@ protected CustomFragment getFragment(String fragmentTag) {
HashtagListFragment hlf = new HashtagListFragment();
fm.beginTransaction().add(hlf, fragmentTag).commit();
return hlf;
case AspectListFragment.TAG:
AspectListFragment alf = new AspectListFragment();
fm.beginTransaction().add(alf, fragmentTag).commit();
return alf;
case PodSelectionFragment.TAG:
PodSelectionFragment psf = new PodSelectionFragment();
fm.beginTransaction().add(psf, fragmentTag).commit();
Expand Down Expand Up @@ -987,14 +992,7 @@ public boolean onNavigationItemSelected(MenuItem item) {

//TODO: Replace with fragment
case R.id.nav_aspects: {
DiasporaStreamFragment stream = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
if (WebHelper.isOnline(MainActivity.this)) {
openDiasporaUrl(DiasporaUrlHelper.URL_BLANK);
WebHelper.showAspectList(stream.getWebView(), app);
setTitle(R.string.aspects);
} else {
snackbarNoInternet.show();
}
showFragment(getFragment(AspectListFragment.TAG));
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void setPodAspects(PodAspect[] aspects) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects);
}

public PodAspect[] getPodAspects() {
public PodAspect[] getAspects() {
String[] s = getStringArray(prefPod, R.string.pref_key__podprofile_aspects);
PodAspect[] aspects = new PodAspect[s.length];
for (int i = 0; i < aspects.length; i++) {
Expand All @@ -224,6 +224,14 @@ public void setFollowedTagsFavs(List<String> values) {
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags_favs, values.toArray(new String[values.size()]));
}

public String[] getAspectFavs() {
return getStringArray(prefPod, R.string.pref_key__podprofile_aspects_favs);
}

public void setAspectFavs(List<String> values) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects_favs, values.toArray(new String[values.size()]));
}

public int getUnreadMessageCount() {
return getInt(prefPod, R.string.pref_key__podprofile_unread_message_count, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PodUserProfile(App app) {
avatarUrl = appSettings.getAvatarUrl();
guid = appSettings.getProfileId();
name = appSettings.getName();
podAspects = appSettings.getPodAspects();
podAspects = appSettings.getAspects();
followedTags = appSettings.getFollowedTags();
notificationCount = appSettings.getNotificationCount();
unreadMessagesCount = appSettings.getUnreadMessageCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
This file is part of the Diaspora for Android.
Diaspora for Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Diaspora for Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the Diaspora for Android.
If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.dfa.diaspora_android.fragment;

import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.activity.MainActivity;
import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.data.PodAspect;
import com.github.dfa.diaspora_android.listener.OnSomethingClickListener;
import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
import com.github.dfa.diaspora_android.util.Helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Fragment that shows a list of the Aspects
*/
public class AspectListFragment extends ThemedFragment implements OnSomethingClickListener<Object> {

public static final String TAG = "com.github.dfa.diaspora_android.AspectListFragment";

protected RecyclerView followedAspectsRecyclerView;
protected App app;
protected DiasporaUrlHelper urls;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.recycler_list__fragment, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
followedAspectsRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_list__recycler_view);
app = (App) getActivity().getApplication();
AppSettings appSettings = app.getSettings();
urls = new DiasporaUrlHelper(appSettings);

followedAspectsRecyclerView.setHasFixedSize(true);
followedAspectsRecyclerView.setNestedScrollingEnabled(false);

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
followedAspectsRecyclerView.setLayoutManager(layoutManager);

final FollowedTagsAdapter adapter = new FollowedTagsAdapter(appSettings, this);
followedAspectsRecyclerView.setAdapter(adapter);

//Set window title
getActivity().setTitle(R.string.nav_aspects);
}

@Override
public String getFragmentTag() {
return TAG;
}

@Override
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
/* Nothing to do */
}

@Override
public boolean onBackPressed() {
return false;
}

@Override
public void onSomethingClicked(Object null1, Integer null2, String aspectId) {
((MainActivity) getActivity()).openDiasporaUrl(urls.getAspectUrl(aspectId));
}

@Override
protected void applyColorToViews() {
followedAspectsRecyclerView.invalidate();
}

public static class FollowedTagsAdapter extends RecyclerView.Adapter<FollowedTagsAdapter.ViewHolder> {
private AppSettings appSettings;
private PodAspect[] followedAspectList;
private List<String> followedAspectFavsList;
private OnSomethingClickListener<Object> aspectClickedListener;

static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.recycler_view__list_item__text)
public TextView title;
@BindView(R.id.recycler_view__list_item__favourite)
AppCompatImageView favouriteImage;
@BindView(R.id.recycler_view__list_item__root)
RelativeLayout root;

ViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}


FollowedTagsAdapter(AppSettings appSettings, OnSomethingClickListener<Object> aspectClickedListener) {
this.appSettings = appSettings;
this.followedAspectList = appSettings.getAspects();
this.followedAspectFavsList = new ArrayList<>(Arrays.asList(appSettings.getAspectFavs()));
this.aspectClickedListener = aspectClickedListener;
}

@Override
public int getItemCount() {
return followedAspectList.length;
}

@Override
public FollowedTagsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view__list_item, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// Alternating colors
final Context c = holder.root.getContext();
final String aspect = followedAspectList[position].name;
holder.title.setText(aspect);
if (position % 2 == 1) {
holder.root.setBackgroundColor(Helpers.getColorFromRessource(c, R.color.md_grey_300));
}

// Favourite (Star) Image
applyFavouriteImage(holder.favouriteImage, isAspectFaved(aspect));

// Click on fav button
holder.favouriteImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isAspectFaved(aspect)) {
followedAspectFavsList.remove(followedAspectFavsList.indexOf(aspect));
} else {
followedAspectFavsList.add(aspect);
}
appSettings.setFollowedTagsFavs(followedAspectFavsList);
applyFavouriteImage(holder.favouriteImage, isAspectFaved(aspect));
}
});

holder.root.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
aspectClickedListener.onSomethingClicked(null, null, followedAspectList[position].id+"");
}
});
}

private boolean isAspectFaved(String tag) {
return followedAspectFavsList.contains(tag);
}

private void applyFavouriteImage(AppCompatImageView imageView, boolean isFaved) {
imageView.setImageResource(isFaved ? R.drawable.ic_star_filled_48px : R.drawable.ic_star_border_black_48px);
imageView.setColorFilter(isFaved ? appSettings.getAccentColor() : 0, PorterDuff.Mode.SRC_ATOP);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@
import butterknife.ButterKnife;

/**
* Fragment that shows a list of the Hashtags the user follows
* Created by vanitas on 29.09.16.
* Fragment that shows a list of the HashTags the user follows
*/

public class HashtagListFragment extends ThemedFragment implements OnSomethingClickListener<Object> {

public static final String TAG = "com.github.dfa.diaspora_android.HashtagListFragment";
Expand All @@ -64,13 +62,13 @@ public class HashtagListFragment extends ThemedFragment implements OnSomethingCl
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.hashtag_list__fragment, container, false);
return inflater.inflate(R.layout.recycler_list__fragment, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
followedTagsRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_followed_tags__recycler_view);
followedTagsRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_list__recycler_view);
app = (App) getActivity().getApplication();
AppSettings appSettings = app.getSettings();
urls = new DiasporaUrlHelper(appSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class DiasporaUrlHelper {
public static final String SUBURL_COMMENTED = "/commented";
public static final String SUBURL_MENTIONS = "/mentions";
public static final String SUBURL_PUBLIC = "/public";
public static final String SUBURL_ASPECT = "/aspects?a_ids[]=";
public static final String SUBURL_TOGGLE_MOBILE = "/mobile/toggle";
public static final String SUBURL_SEARCH_TAGS = "/tags/";
public static final String SUBURL_SEARCH_PEOPLE = "/people.mobile?q=";
Expand Down Expand Up @@ -144,6 +145,11 @@ public String getProfileUrl(long profileId) {
return getPodUrl() + SUBURL_PEOPLE + profileId;
}


public String getAspectUrl(String aspectId) {
return getPodUrl() + SUBURL_ASPECT + aspectId;
}

/**
* Return a url that points to the activities feed of the currently registered diaspora account
*
Expand Down Expand Up @@ -263,7 +269,6 @@ public String getManageContactsUrl() {
return getPodUrl() + SUBURL_MANAGE_CONTACTS;
}


public String getSuburlNotificationsAlsoCommentedUrl() {
return getPodUrl() + SUBURL_NOTIFICATIONS_ALSO_COMMENTED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,52 +110,4 @@ public static void shareTextIntoWebView(final WebView webView, String sharedText
" }" +
"})();");
}

public static void showAspectList(final WebView wv, final App app) {
wv.stopLoading();
PodUserProfile profile = app.getPodUserProfile();
StringBuilder sb = new StringBuilder();

sb.append("<html><body style='margin-top: 25px; margin-left:auto;margin-right:auto; font-size: 400%;'>");

// Content
for (PodAspect aspect : profile.getAspects()) {
sb.append("<span style='margin-left: 30px; '></span>&raquo; &nbsp;");
sb.append(aspect.toHtmlLink(app));
sb.append("<hr style='height:5px;' />");
}

// End
sb.append("</body></html>");
wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null);
}

public static void showFollowedTagsList(final WebView wv, final App app) {
wv.stopLoading();
PodUserProfile profile = app.getPodUserProfile();
StringBuilder sb = new StringBuilder();

sb.append("<html><body style='margin-top: 25px; margin-left:auto;margin-right:auto; font-size: 400%;'>");

// Content
AppSettings appSettings = app.getSettings();
String pod0BaseUrl = appSettings.getPod().getPodUrl().getBaseUrl();
sb.append("<span style='margin-left: 30px; '></span>&raquo; &nbsp;");
sb.append(String.format(Locale.getDefault(),
"<a href='%s/followed_tags' style='color: #000000; text-decoration: none;'><b>%s</b></a>",
pod0BaseUrl, app.getString(R.string.all_tags)));
sb.append("<hr style='height:5px;' />");
for (String tag : profile.getFollowedTags()) {
sb.append("<span style='margin-left: 30px; '></span>&raquo; &nbsp;");
sb.append(String.format(Locale.getDefault(),
"<a href='%s/tags/%s' style='color: #000000; text-decoration: none;'>#%s</a>",
pod0BaseUrl, tag, tag));
sb.append("<hr style='height:5px;' />");
}

// End
sb.append("</body></html>");
wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_followed_tags__recycler_view"
android:id="@+id/fragment_list__recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
Expand Down

0 comments on commit b9f7b32

Please sign in to comment.