diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java index 332bae19..3614e32c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java @@ -36,7 +36,7 @@ public class AboutActivity extends BaseActivity @Override protected void onResume() { - super.onResume(); drawer.setSelection(ABOUT_ID); + super.onResume(); } } 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 560a10a9..7a9fec1d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -1,14 +1,18 @@ package gr.thmmy.mthmmy.activities; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; +import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; +import android.widget.ImageView; import android.widget.Toast; import com.franmontiel.persistentcookiejar.PersistentCookieJar; @@ -24,6 +28,9 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; import com.mikepenz.materialdrawer.model.ProfileDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IProfile; +import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader; +import com.mikepenz.materialdrawer.util.DrawerImageLoader; +import com.squareup.picasso.Picasso; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.main.MainActivity; @@ -65,10 +72,38 @@ public class BaseActivity extends AppCompatActivity .cookieJar(cookieJar) .build(); sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs); + //initialize and create the image loader logic + DrawerImageLoader.init(new AbstractDrawerImageLoader() { + @Override + public void set(ImageView imageView, Uri uri, Drawable placeholder) { + Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView); + } + @Override + public void cancel(ImageView imageView) { + Picasso.with(imageView.getContext()).cancelRequest(imageView); + } + + @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 super.placeholder(ctx, tag); + } + + + }); init = true; } } + @Override + protected void onResume() { + super.onResume(); + updateDrawer(); + } + @Override protected void onPause() { super.onPause(); @@ -123,7 +158,7 @@ public class BaseActivity extends AppCompatActivity aboutItem = new PrimaryDrawerItem().withIdentifier(ABOUT_ID).withName(R.string.about).withIcon(aboutIcon); //Profile - profileDrawerItem = new ProfileDrawerItem().withName(sessionManager.getUsername()); //TODO: set profile picture + profileDrawerItem = new ProfileDrawerItem().withName(sessionManager.getUsername()); //AccountHeader accountHeader = new AccountHeaderBuilder() @@ -201,11 +236,15 @@ public class BaseActivity extends AppCompatActivity if(drawer!=null) { 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); + } else + { loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout - - profileDrawerItem.withName(sessionManager.getUsername()); //TODO: set profile picture + profileDrawerItem.withName(sessionManager.getUsername()).withIcon(sessionManager.getAvatarLink()); + } accountHeader.updateProfile(profileDrawerItem); drawer.updateItem(loginLogoutItem); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index c20709cb..4f30b797 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -60,9 +60,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF @Override protected void onResume() { - super.onResume(); drawer.setSelection(HOME_ID); - updateDrawer(); + super.onResume(); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index d98b248e..e2a27d8b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -180,8 +180,8 @@ public class TopicActivity extends BaseActivity { @Override protected void onResume() { - super.onResume(); drawer.setSelection(-1); + super.onResume(); } @Override 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 c3aa9e18..6a58c704 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -1,7 +1,6 @@ package gr.thmmy.mthmmy.session; import android.content.SharedPreferences; -import android.os.Environment; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; @@ -11,7 +10,6 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import java.util.List; @@ -63,6 +61,7 @@ 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 LOGOUT_LINK = "LogoutLink"; public static final String LOGIN_STATUS = "IsLoggedIn"; @@ -125,6 +124,7 @@ 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(); sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply(); return SUCCESS; @@ -248,6 +248,10 @@ public class SessionManager return sharedPrefs.getString(USERNAME, "Username"); } + public String getAvatarLink() { + return sharedPrefs.getString(AVATAR_LINK, "AvatarUrl"); + } + public int getLogStatus() { return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); } @@ -299,70 +303,22 @@ public class SessionManager return matcher.group(1); } } + Report.w(TAG,"Extracting username failed!"); return null; } - /*private File getUserAvatar(String image_url, final String package_name, final String image_name) { - final File[] returnImage = {null}; - - ImageController.getInstance().getImageLoader().get(image_url, new ImageLoader.ImageListener() { - @Override - public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) { - Bitmap bitmap = imageContainer.getBitmap(); //The image - //Create a file to store it - File pictureFile = getOutputMediaFile(package_name, image_name); - - if (pictureFile == null) { - Report.d(TAG, - "Error creating media file, check storage permissions: "); - return; - } - try { - //Initialize an output stream - FileOutputStream fos = new FileOutputStream(pictureFile); - //Store image to file - bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); - fos.close(); - } catch (FileNotFoundException e) { - Report.d(TAG, "File not found"); - } catch (IOException e) { - Report.d(TAG, "Error accessing file")); - } - returnImage[0] = pictureFile; - } + private String extractAvatarLink(Document doc) + { + if (doc != null) { + Elements avatar = doc.select("#ava img"); - @Override - public void onErrorResponse(VolleyError volleyError) { - } - }); - return returnImage[0]; - }*/ - - /** Create a File for saving an image or video */ - private File getOutputMediaFile(String packageName, String imageName){ - // To be safe, you should check that the SDCard is mounted - // using Environment.getExternalStorageState() before doing this. - File mediaStorageDir = new File(Environment.getExternalStorageDirectory() - + "/Android/data/" - + packageName - + "/Files"); - - // This location works best if you want the created images to be shared - // between applications and persist after your app has been uninstalled. - - // Create the storage directory if it does not exist - if (! mediaStorageDir.exists()){ - if (! mediaStorageDir.mkdirs()){ - return null; + if (avatar.size() == 1) { + return avatar.attr("src"); } } - // Create a media file name - File mediaFile; - String mImageName = imageName + ".jpg"; - mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); - return mediaFile; + Report.w(TAG,"Extracting avatar's link failed!"); + return null; } - //----------------------------------OTHER FUNCTIONS END----------------------------------------- } \ No newline at end of file