Browse Source

OkHttp3 Client & Picasso Integration

Default avatar fix in drawer for users without avatar
pull/24/head
Ezerous 8 years ago
parent
commit
dd02c2c281
  1. 1
      app/build.gradle
  2. 22
      app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java
  4. 17
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

1
app/build.gradle

@ -34,6 +34,7 @@ dependencies {
compile 'com.android.support:recyclerview-v7:25.0.1' compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.squareup.okhttp3:okhttp:3.5.0' compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.picasso:picasso:2.5.2' 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 'org.jsoup:jsoup:1.10.1'
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0' compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'
compile('com.mikepenz:materialdrawer:5.8.0@aar') { compile('com.mikepenz:materialdrawer:5.8.0@aar') {

22
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.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
@ -18,6 +19,7 @@ import android.widget.Toast;
import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.materialdrawer.AccountHeader; import com.mikepenz.materialdrawer.AccountHeader;
@ -43,6 +45,7 @@ public class BaseActivity extends AppCompatActivity
{ {
// Client & Cookies // Client & Cookies
protected static OkHttpClient client; protected static OkHttpClient client;
protected static Picasso picasso;
private static PersistentCookieJar cookieJar; private static PersistentCookieJar cookieJar;
private static SharedPrefsCookiePersistor sharedPrefsCookiePersistor; private static SharedPrefsCookiePersistor sharedPrefsCookiePersistor;
@ -72,7 +75,11 @@ public class BaseActivity extends AppCompatActivity
.cookieJar(cookieJar) .cookieJar(cookieJar)
.build(); .build();
sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs); 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() { DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override @Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) { public void set(ImageView imageView, Uri uri, Drawable placeholder) {
@ -86,13 +93,14 @@ public class BaseActivity extends AppCompatActivity
@Override @Override
public Drawable placeholder(Context ctx, String tag) { public Drawable placeholder(Context ctx, String tag) {
if (DrawerImageLoader.Tags.PROFILE.name().equals(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); return super.placeholder(ctx, tag);
} }
}); });
init = true; init = true;
} }
@ -238,7 +246,11 @@ public class BaseActivity extends AppCompatActivity
if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest
{ {
loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login 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 else
{ {

4
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.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
@ -14,6 +13,7 @@ import android.widget.Toast;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.main.MainActivity; 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.CONNECTION_ERROR;
import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION; import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION;
@ -54,7 +54,7 @@ public class LoginActivity extends BaseActivity {
btnLogin.setOnClickListener(new View.OnClickListener() { btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) { public void onClick(View view) {
Log.d(TAG, "Login"); Report.d(TAG, "Login");
//Get username and password strings //Get username and password strings
username = inputUsername.getText().toString().trim(); username = inputUsername.getText().toString().trim();

17
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -61,7 +61,8 @@ public class SessionManager
//Shared Preferences & its keys //Shared Preferences & its keys
private SharedPreferences sharedPrefs; private SharedPreferences sharedPrefs;
public static final String USERNAME = "Username"; 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 LOGOUT_LINK = "LogoutLink";
public static final String LOGIN_STATUS = "IsLoggedIn"; public static final String LOGIN_STATUS = "IsLoggedIn";
@ -124,7 +125,15 @@ public class SessionManager
//Edit SharedPreferences, save session's data //Edit SharedPreferences, save session's data
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply(); sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply();
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply();
String avatar = extractAvatarLink(document);
if (avatar!=null)
{
sharedPrefs.edit().putBoolean(HAS_AVATAR,true).apply();
sharedPrefs.edit().putString(AVATAR_LINK, extractAvatarLink(document)).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(); sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply();
return SUCCESS; return SUCCESS;
@ -249,7 +258,11 @@ public class SessionManager
} }
public String getAvatarLink() { 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() { public int getLogStatus() {

Loading…
Cancel
Save