From dd02c2c281bd265ce2a6aa5f8e54d6a89a3ad094 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 7 Dec 2016 21:40:11 +0200 Subject: [PATCH] OkHttp3 Client & Picasso Integration Default avatar fix in drawer for users without avatar --- app/build.gradle | 1 + .../thmmy/mthmmy/activities/BaseActivity.java | 22 ++++++++++++++----- .../mthmmy/activities/LoginActivity.java | 4 ++-- .../thmmy/mthmmy/session/SessionManager.java | 19 +++++++++++++--- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0cf9686f..65bfd3b2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,6 +34,7 @@ dependencies { compile 'com.android.support:recyclerview-v7:25.0.1' compile 'com.squareup.okhttp3:okhttp:3.5.0' compile 'com.squareup.picasso:picasso:2.5.2' + compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' compile 'org.jsoup:jsoup:1.10.1' compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0' compile('com.mikepenz:materialdrawer:5.8.0@aar') { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java index 7a9fec1d..92c3110c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -9,6 +9,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; @@ -18,6 +19,7 @@ import android.widget.Toast; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; +import com.jakewharton.picasso.OkHttp3Downloader; import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.materialdrawer.AccountHeader; @@ -43,6 +45,7 @@ public class BaseActivity extends AppCompatActivity { // Client & Cookies protected static OkHttpClient client; + protected static Picasso picasso; private static PersistentCookieJar cookieJar; private static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; @@ -72,7 +75,11 @@ public class BaseActivity extends AppCompatActivity .cookieJar(cookieJar) .build(); sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs); - //initialize and create the image loader logic + picasso = new Picasso.Builder(BaseActivity.this) + .downloader(new OkHttp3Downloader(client)) + .build(); + Picasso.setSingletonInstance(picasso); // all following Picasso (with Picasso.with(Context context) requests will use this Picasso object + //initialize and create the image loader logic TODO move this to a singleton BaseApplication obj DrawerImageLoader.init(new AbstractDrawerImageLoader() { @Override public void set(ImageView imageView, Uri uri, Drawable placeholder) { @@ -86,13 +93,14 @@ public class BaseActivity extends AppCompatActivity @Override public Drawable placeholder(Context ctx, String tag) { if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) { - return new IconicsDrawable(ctx, FontAwesome.Icon.faw_user_circle); + return new IconicsDrawable(ctx).icon(FontAwesome.Icon.faw_user) + .paddingDp(10) + .color(ContextCompat.getColor(ctx, R.color.primary_light)) + .backgroundColor(ContextCompat.getColor(ctx, R.color.primary)); } return super.placeholder(ctx, tag); } - - }); init = true; } @@ -238,7 +246,11 @@ public class BaseActivity extends AppCompatActivity if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest { loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login - profileDrawerItem.withName(sessionManager.getUsername()).withIcon(FontAwesome.Icon.faw_user_circle); + profileDrawerItem.withName(sessionManager.getUsername()).withIcon(new IconicsDrawable(this) + .icon(FontAwesome.Icon.faw_user) + .paddingDp(10) + .color(ContextCompat.getColor(this, R.color.primary_light)) + .backgroundColor(ContextCompat.getColor(this, R.color.primary))); } else { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java index 63834b02..054f95cb 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java @@ -3,7 +3,6 @@ package gr.thmmy.mthmmy.activities; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; -import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; @@ -14,6 +13,7 @@ import android.widget.Toast; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.main.MainActivity; +import mthmmy.utils.Report; import static gr.thmmy.mthmmy.session.SessionManager.CONNECTION_ERROR; import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION; @@ -54,7 +54,7 @@ public class LoginActivity extends BaseActivity { btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { - Log.d(TAG, "Login"); + Report.d(TAG, "Login"); //Get username and password strings username = inputUsername.getText().toString().trim(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 6a58c704..4b437903 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -61,7 +61,8 @@ public class SessionManager //Shared Preferences & its keys private SharedPreferences sharedPrefs; public static final String USERNAME = "Username"; - public static final String AVATAR_LINK = "AvatarUrl"; + public static final String AVATAR_LINK = "AvatarLink"; + public static final String HAS_AVATAR = "HasAvatar"; public static final String LOGOUT_LINK = "LogoutLink"; public static final String LOGIN_STATUS = "IsLoggedIn"; @@ -124,7 +125,15 @@ public class SessionManager //Edit SharedPreferences, save session's data sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); - sharedPrefs.edit().putString(AVATAR_LINK, extractAvatarLink(document)).apply(); + String avatar = extractAvatarLink(document); + if (avatar!=null) + { + sharedPrefs.edit().putBoolean(HAS_AVATAR,true).apply(); + sharedPrefs.edit().putString(AVATAR_LINK, extractAvatarLink(document)).apply(); + } + else + sharedPrefs.edit().putBoolean(HAS_AVATAR,false).apply(); + sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply(); return SUCCESS; @@ -249,7 +258,11 @@ public class SessionManager } public String getAvatarLink() { - return sharedPrefs.getString(AVATAR_LINK, "AvatarUrl"); + return sharedPrefs.getString(AVATAR_LINK, "AvatarLink"); + } + + public boolean hasAvatar() { + return sharedPrefs.getBoolean(HAS_AVATAR, false); } public int getLogStatus() {