mirror of
https://github.com/gsantner/dandelion
synced 2024-11-22 04:12:08 +01:00
Extract and show aspects
This commit is contained in:
parent
85038e90f2
commit
3a49a83896
7 changed files with 194 additions and 21 deletions
|
@ -10,6 +10,7 @@ import android.webkit.WebSettings;
|
|||
import android.webkit.WebView;
|
||||
|
||||
import com.github.dfa.diaspora_android.data.AppSettings;
|
||||
import com.github.dfa.diaspora_android.data.PodUserProfile;
|
||||
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
||||
import com.github.dfa.diaspora_android.util.AvatarImageLoader;
|
||||
|
||||
|
@ -22,6 +23,7 @@ public class App extends Application {
|
|||
private AppSettings appSettings;
|
||||
private AvatarImageLoader avatarImageLoader;
|
||||
private CookieManager cookieManager;
|
||||
private PodUserProfile podUserProfile;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
|
@ -29,6 +31,7 @@ public class App extends Application {
|
|||
final Context c = getApplicationContext();
|
||||
appSettings = new AppSettings(c);
|
||||
avatarImageLoader = new AvatarImageLoader(c);
|
||||
podUserProfile = new PodUserProfile(this);
|
||||
|
||||
|
||||
// Get cookie manager
|
||||
|
@ -61,6 +64,10 @@ public class App extends Application {
|
|||
}
|
||||
}
|
||||
|
||||
public PodUserProfile getPodUserProfile(){
|
||||
return podUserProfile;
|
||||
}
|
||||
|
||||
public AppSettings getSettings() {
|
||||
return appSettings;
|
||||
}
|
||||
|
|
|
@ -172,7 +172,9 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
app = (App) getApplication();
|
||||
appSettings = app.getSettings();
|
||||
podUserProfile = new PodUserProfile(app, uiHandler, this);
|
||||
podUserProfile = app.getPodUserProfile();
|
||||
podUserProfile.setCallbackHandler(uiHandler);
|
||||
podUserProfile.setListener(this);
|
||||
|
||||
this.registerForContextMenu(webView);
|
||||
webView.setParentActivity(this);
|
||||
|
@ -245,7 +247,7 @@ public class MainActivity extends AppCompatActivity
|
|||
}
|
||||
|
||||
if (progress > 60) {
|
||||
Helpers.hideTopBar(wv);
|
||||
Helpers.applyDiasporaMobileSiteChanges(wv);
|
||||
}
|
||||
|
||||
if (progress == 100) {
|
||||
|
@ -872,7 +874,8 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
case R.id.nav_aspects: {
|
||||
if (Helpers.isOnline(MainActivity.this)) {
|
||||
webView.loadUrl("https://" + podDomain + "/aspects");
|
||||
// webView.loadUrl("https://" + podDomain + "/aspects");
|
||||
Helpers.showAspectList(webView, app);
|
||||
} else {
|
||||
Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
|
|
|
@ -132,7 +132,7 @@ public class ShareActivity extends MainActivity {
|
|||
}
|
||||
|
||||
if (progress > 60) {
|
||||
Helpers.hideTopBar(wv);
|
||||
Helpers.applyDiasporaMobileSiteChanges(wv);
|
||||
}
|
||||
|
||||
if (progress == 100) {
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package com.github.dfa.diaspora_android.data;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
/**
|
||||
* Created by gsantner on 20.03.16. Part of Diaspora for Android.
|
||||
*/
|
||||
|
@ -17,6 +21,10 @@ public class AppSettings {
|
|||
prefPod = this.context.getSharedPreferences("pod0", Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public Context getApplicationContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public void clearPodSettings() {
|
||||
prefPod.edit().clear().apply();
|
||||
}
|
||||
|
@ -37,18 +45,18 @@ public class AppSettings {
|
|||
pref.edit().putBoolean(key, value).apply();
|
||||
}
|
||||
|
||||
private void setStringArray(SharedPreferences pref, String key, String[] values){
|
||||
private void setStringArray(SharedPreferences pref, String key, Object[] values) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(String value : values){
|
||||
for (Object value : values) {
|
||||
sb.append("%%%");
|
||||
sb.append(value);
|
||||
sb.append(value.toString());
|
||||
}
|
||||
setString(pref,key,sb.toString().replaceFirst("%%%",""));
|
||||
setString(pref, key, sb.toString().replaceFirst("%%%", ""));
|
||||
}
|
||||
|
||||
private String[] getStringArray(SharedPreferences pref, String key){
|
||||
String value = pref.getString(key,"%%%");
|
||||
if (value.equals("%%%")){
|
||||
private String[] getStringArray(SharedPreferences pref, String key) {
|
||||
String value = pref.getString(key, "%%%");
|
||||
if (value.equals("%%%")) {
|
||||
return new String[0];
|
||||
}
|
||||
return value.split("%%%");
|
||||
|
@ -65,6 +73,7 @@ public class AppSettings {
|
|||
private static final String PODUSERPROFILE_NAME = "podUserProfile_name";
|
||||
private static final String PODUSERPROFILE_ID = "podUserProfile_guid";
|
||||
private static final String PODDOMAIN = "podDomain";
|
||||
private static final String PODUSERPROFILE_ASPECTS = "podUserProfile_aspects";
|
||||
}
|
||||
|
||||
|
||||
|
@ -76,7 +85,7 @@ public class AppSettings {
|
|||
}
|
||||
|
||||
public void setProfileId(String profileId) {
|
||||
setString(prefPod, PREF.PODUSERPROFILE_ID,profileId);
|
||||
setString(prefPod, PREF.PODUSERPROFILE_ID, profileId);
|
||||
}
|
||||
|
||||
|
||||
|
@ -121,15 +130,28 @@ public class AppSettings {
|
|||
setString(prefPod, PREF.PODDOMAIN, podDomain);
|
||||
}
|
||||
|
||||
public boolean hasPodDomain(){
|
||||
public boolean hasPodDomain() {
|
||||
return !prefPod.getString(PREF.PODDOMAIN, "").equals("");
|
||||
}
|
||||
|
||||
public String[] getPreviousPodlist(){
|
||||
public String[] getPreviousPodlist() {
|
||||
return getStringArray(prefApp, PREF.PREVIOUS_PODLIST);
|
||||
}
|
||||
|
||||
public void setPreviousPodlist(String[] pods){
|
||||
public void setPreviousPodlist(String[] pods) {
|
||||
setStringArray(prefApp, PREF.PREVIOUS_PODLIST, pods);
|
||||
}
|
||||
|
||||
public void setPodAspects(PodAspect[] aspects) {
|
||||
setStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS, aspects);
|
||||
}
|
||||
|
||||
public PodAspect[] getPodAspects() {
|
||||
String[] s= getStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS);
|
||||
PodAspect[] aspects = new PodAspect[s.length];
|
||||
for(int i=0; i < aspects.length; i++){
|
||||
aspects[i] = new PodAspect(s[i]);
|
||||
}
|
||||
return aspects;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package com.github.dfa.diaspora_android.data;
|
||||
|
||||
import android.support.v4.content.ContextCompat;
|
||||
|
||||
import com.github.dfa.diaspora_android.App;
|
||||
import com.github.dfa.diaspora_android.R;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by gregor on 05.06.16.
|
||||
*/
|
||||
public class PodAspect {
|
||||
public long id;
|
||||
public String name;
|
||||
public boolean selected;
|
||||
|
||||
public PodAspect(long id, String name, boolean selected) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
|
||||
public PodAspect(String shareabletext) {
|
||||
// fromShareAbleText
|
||||
String[] str = shareabletext.split("%");
|
||||
selected = Integer.parseInt(str[0]) == 1;
|
||||
id = Long.parseLong(str[1]);
|
||||
name = shareabletext.substring(shareabletext.indexOf(str[1]) + str[1].length() + 1);
|
||||
}
|
||||
|
||||
public PodAspect(JSONObject json) throws JSONException {
|
||||
if (json.has("id")) {
|
||||
id = json.getLong("id");
|
||||
}
|
||||
if (json.has("name")) {
|
||||
name = json.getString("name");
|
||||
}
|
||||
if (json.has("selected")) {
|
||||
selected = json.getBoolean("selected");
|
||||
}
|
||||
}
|
||||
|
||||
public String toJsonString() {
|
||||
JSONObject j = new JSONObject();
|
||||
try {
|
||||
j.put("id", id);
|
||||
j.put("name", name);
|
||||
j.put("selected", selected);
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
return j.toString();
|
||||
}
|
||||
|
||||
public String toHtmlLink(final App app) {
|
||||
final AppSettings appSettings = app.getSettings();
|
||||
return String.format(Locale.getDefault(),
|
||||
"<a href='https://%s/aspects?a_ids[]=%d' style='color: #000000; text-decoration: none;'>%s</a>",
|
||||
appSettings.getPodDomain(), id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toShareAbleText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof PodAspect) {
|
||||
return ((PodAspect) o).id == id;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toShareAbleText() {
|
||||
return String.format(Locale.getDefault(), "%d%%%d%%%s", selected ? 1 : 0, id, name);
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import android.util.Log;
|
|||
import com.github.dfa.diaspora_android.App;
|
||||
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
@ -26,6 +27,7 @@ public class PodUserProfile {
|
|||
private String avatarUrl;
|
||||
private String guid;
|
||||
private String name;
|
||||
private PodAspect[] podAspects;
|
||||
private int notificationCount;
|
||||
private int unreadMessagesCount;
|
||||
|
||||
|
@ -37,6 +39,7 @@ public class PodUserProfile {
|
|||
avatarUrl = appSettings.getAvatarUrl();
|
||||
guid = appSettings.getProfileId();
|
||||
name = appSettings.getName();
|
||||
podAspects = appSettings.getPodAspects();
|
||||
}
|
||||
|
||||
public PodUserProfile(App app, Handler callbackHandler, WebUserProfileChangedListener listener) {
|
||||
|
@ -83,7 +86,14 @@ public class PodUserProfile {
|
|||
|
||||
// Unread message count
|
||||
if (json.has("unread_messages_count") && loadUnreadMessagesCount(json.getInt("unread_messages_count"))) {
|
||||
appSettings.setPodAspects(podAspects);
|
||||
}
|
||||
|
||||
// Aspect
|
||||
if (json.has("aspects") && loadAspects(json.getJSONArray("aspects"))) {
|
||||
appSettings.setPodAspects(podAspects);
|
||||
}
|
||||
|
||||
isWebUserProfileLoaded = true;
|
||||
} catch (JSONException e) {
|
||||
Log.d(App.TAG, e.getMessage());
|
||||
|
@ -117,6 +127,10 @@ public class PodUserProfile {
|
|||
return unreadMessagesCount;
|
||||
}
|
||||
|
||||
public PodAspect[] getAspects() {
|
||||
return podAspects;
|
||||
}
|
||||
|
||||
/*
|
||||
* Private property setters
|
||||
*/
|
||||
|
@ -173,6 +187,14 @@ public class PodUserProfile {
|
|||
return false;
|
||||
}
|
||||
|
||||
private boolean loadAspects(final JSONArray jsonAspects) throws JSONException {
|
||||
podAspects = new PodAspect[jsonAspects.length()];
|
||||
for (int i = 0; i < jsonAspects.length(); i++) {
|
||||
podAspects[i] = new PodAspect(jsonAspects.getJSONObject(i));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean loadUnreadMessagesCount(final int unreadMessagesCount) {
|
||||
if (this.unreadMessagesCount != unreadMessagesCount) {
|
||||
this.unreadMessagesCount = unreadMessagesCount;
|
||||
|
@ -188,6 +210,22 @@ public class PodUserProfile {
|
|||
return false;
|
||||
}
|
||||
|
||||
public Handler getCallbackHandler() {
|
||||
return callbackHandler;
|
||||
}
|
||||
|
||||
public void setCallbackHandler(Handler callbackHandler) {
|
||||
this.callbackHandler = callbackHandler;
|
||||
}
|
||||
|
||||
public WebUserProfileChangedListener getListener() {
|
||||
return listener;
|
||||
}
|
||||
|
||||
public void setListener(WebUserProfileChangedListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/*
|
||||
* Not implemented / not needed yet:
|
||||
* string "diasporaAddress"
|
||||
|
@ -195,10 +233,6 @@ public class PodUserProfile {
|
|||
* boolean "admin"
|
||||
* int "following_count"
|
||||
* boolean "moderator"
|
||||
* array "aspects"
|
||||
* int "id"
|
||||
* string "name"
|
||||
* boolean "selected"
|
||||
*
|
||||
* array "services"
|
||||
* ? ?
|
||||
|
|
|
@ -28,8 +28,10 @@ import android.net.NetworkInfo;
|
|||
import android.support.v4.content.ContextCompat;
|
||||
import android.webkit.WebView;
|
||||
|
||||
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.PodAspect;
|
||||
import com.github.dfa.diaspora_android.data.PodUserProfile;
|
||||
|
||||
public class Helpers {
|
||||
|
||||
|
@ -49,7 +51,7 @@ public class Helpers {
|
|||
}
|
||||
}
|
||||
|
||||
public static void hideTopBar(final WebView wv) {
|
||||
public static void applyDiasporaMobileSiteChanges(final WebView wv) {
|
||||
wv.loadUrl("javascript: ( function() {" +
|
||||
" document.documentElement.style.paddingBottom = '260px';" +
|
||||
" document.getElementById('main').style.paddingTop = '5px';" +
|
||||
|
@ -81,6 +83,7 @@ public class Helpers {
|
|||
}
|
||||
|
||||
public static void getUserProfile(final WebView wv) {
|
||||
// aspects":[{"id":124934,"name":"Friends","selected":true},{"id":124937,"name":"Liked me","selected":false},{"id":124938,"name":"Follow","selected":false},{"id":128327,"name":"Nur ich","selected":false}]
|
||||
wv.loadUrl("javascript: ( function() {" +
|
||||
" if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined') {" +
|
||||
" var userProfile = JSON.stringify(gon.user);" +
|
||||
|
@ -88,4 +91,25 @@ public class Helpers {
|
|||
" } " +
|
||||
"})();");
|
||||
}
|
||||
|
||||
public static void showAspectList(final WebView wv, final App app) {
|
||||
wv.stopLoading();
|
||||
PodUserProfile profile = app.getPodUserProfile();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int intColor = ContextCompat.getColor(app, R.color.colorAccent);
|
||||
String strColor = String.format("#%06X", (0xFFFFFF & intColor));
|
||||
|
||||
sb.append("<html><body style='width:80%; margin-left:auto;margin-right:auto; font-size: 400%;'><center><b>");
|
||||
sb.append(String.format("<h1 style='color: %s; text-shadow: 4px 4px 12px #000000;'>%s</h1>", strColor, app.getString(R.string.jb_aspects)));
|
||||
sb.append("</b></center>");
|
||||
// Content
|
||||
for (PodAspect aspect : profile.getAspects()) {
|
||||
sb.append("» ");
|
||||
sb.append(aspect.toHtmlLink(app));
|
||||
sb.append("</br></br>");
|
||||
}
|
||||
// End
|
||||
sb.append("</body></html>");
|
||||
wv.loadData(sb.toString(), "text/html", "UTF-16");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue