Commit 412b3e97 authored by Sajal Narang's avatar Sajal Narang

Implement logout functionality, fix #57

parent 834237cd
...@@ -262,7 +262,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -262,7 +262,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
bundle.putString(Constants.SESSION_ID, session.pref.getString(Constants.SESSION_ID, "Error")); bundle.putString(Constants.SESSION_ID, session.pref.getString(Constants.SESSION_ID, "Error"));
if (fragment instanceof MessMenuFragment) if (fragment instanceof MessMenuFragment)
bundle.putString(Constants.USER_HOSTEL, currentUser.getHostel()); bundle.putString(Constants.USER_HOSTEL, currentUser.getHostel());
if (fragment instanceof SettingsFragment) if (fragment instanceof SettingsFragment && session.isLoggedIn())
bundle.putString(Constants.USER_ID, currentUser.getUserID()); bundle.putString(Constants.USER_ID, currentUser.getUserID());
fragment.setArguments(bundle); fragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager(); FragmentManager manager = getSupportFragmentManager();
......
...@@ -17,7 +17,6 @@ public class SessionManager { ...@@ -17,7 +17,6 @@ public class SessionManager {
public SessionManager(Context context) { public SessionManager(Context context) {
this.context = context; this.context = context;
pref = context.getSharedPreferences(Constants.PREF_NAME, PRIVATE_MODE); pref = context.getSharedPreferences(Constants.PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
} }
public void checkLogin() { public void checkLogin() {
...@@ -34,6 +33,7 @@ public class SessionManager { ...@@ -34,6 +33,7 @@ public class SessionManager {
public void createLoginSession(String gcmId, User currentUser, String sessionID) { public void createLoginSession(String gcmId, User currentUser, String sessionID) {
Log.d("SessionManager", "GcmId being stored"); Log.d("SessionManager", "GcmId being stored");
editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN, true); editor.putBoolean(Constants.IS_LOGGED_IN, true);
editor.putString(Constants.GCM_ID, gcmId); editor.putString(Constants.GCM_ID, gcmId);
editor.putString(Constants.CURRENT_USER, currentUser.toString()); editor.putString(Constants.CURRENT_USER, currentUser.toString());
...@@ -48,4 +48,9 @@ public class SessionManager { ...@@ -48,4 +48,9 @@ public class SessionManager {
public boolean isLoggedIn() { public boolean isLoggedIn() {
return pref.getBoolean(Constants.IS_LOGGED_IN, false); return pref.getBoolean(Constants.IS_LOGGED_IN, false);
} }
public void logout() {
editor = pref.edit();
editor.clear().commit();
}
} }
...@@ -61,6 +61,9 @@ public interface RetrofitInterface { ...@@ -61,6 +61,9 @@ public interface RetrofitInterface {
@GET("news") @GET("news")
Call<List<NewsArticle>> getNews(@Header("Cookie") String sessionID); Call<List<NewsArticle>> getNews(@Header("Cookie") String sessionID);
@GET("logout")
Call<Void> logout(@Header("Cookie") String sessionID);
// @POST("getNotifications/") // @POST("getNotifications/")
// Call<NotificationsResponse> getNotifications(@Body NotificationsRequest notificationsRequest); // Call<NotificationsResponse> getNotifications(@Body NotificationsRequest notificationsRequest);
} }
package in.ac.iitb.gymkhana.iitbapp.fragment; package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import in.ac.iitb.gymkhana.iitbapp.Constants; import in.ac.iitb.gymkhana.iitbapp.Constants;
import in.ac.iitb.gymkhana.iitbapp.LoginActivity;
import in.ac.iitb.gymkhana.iitbapp.R; import in.ac.iitb.gymkhana.iitbapp.R;
import in.ac.iitb.gymkhana.iitbapp.SessionManager;
import in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface; import in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface;
import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator; import in.ac.iitb.gymkhana.iitbapp.api.ServiceGenerator;
import in.ac.iitb.gymkhana.iitbapp.data.User; import in.ac.iitb.gymkhana.iitbapp.data.User;
...@@ -41,6 +48,7 @@ public class SettingsFragment extends Fragment { ...@@ -41,6 +48,7 @@ public class SettingsFragment extends Fragment {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
Bundle bundle = getArguments(); Bundle bundle = getArguments();
String userID = bundle.getString(Constants.USER_ID); String userID = bundle.getString(Constants.USER_ID);
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
...@@ -66,5 +74,66 @@ public class SettingsFragment extends Fragment { ...@@ -66,5 +74,66 @@ public class SettingsFragment extends Fragment {
Picasso.with(getContext()).load(user.getUserProfilePictureUrl()).into(userProfilePictureImageView); Picasso.with(getContext()).load(user.getUserProfilePictureUrl()).into(userProfilePictureImageView);
userNameTextView.setText(user.getUserName()); userNameTextView.setText(user.getUserName());
Button updateProfileButton = getActivity().findViewById(R.id.settings_update_profile);
Button feedbackButton = getActivity().findViewById(R.id.settings_feedback);
Button aboutButton = getActivity().findViewById(R.id.settings_about);
Button logoutButton = getActivity().findViewById(R.id.settings_logout);
updateProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openWebURL("https://gymkhana.iitb.ac.in/sso/user");
}
});
feedbackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openWebURL("https://insti.app/feedback");
}
});
aboutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AboutFragment aboutFragment = new AboutFragment();
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
transaction.replace(R.id.framelayout_for_fragment, aboutFragment, aboutFragment.getTag());
transaction.addToBackStack(aboutFragment.getTag()).commit();
}
});
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.logout("sessionid=" + getArguments().getString(Constants.SESSION_ID)).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
SessionManager sessionManager = new SessionManager(getContext());
sessionManager.logout();
Intent intent = new Intent(getContext(), LoginActivity.class);
startActivity(intent);
getActivity().finish();
}
//Server Error
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
//Network Error
}
});
}
});
}
private void openWebURL(String URL) {
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(browse);
} }
} }
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
<Button <Button
android:id="@+id/settings_update_profile"
style="@style/Widget.AppCompat.Button.Borderless" style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -56,6 +57,7 @@ ...@@ -56,6 +57,7 @@
android:textSize="20sp" /> android:textSize="20sp" />
<Button <Button
android:id="@+id/settings_feedback"
style="@style/Widget.AppCompat.Button.Borderless" style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -68,6 +70,7 @@ ...@@ -68,6 +70,7 @@
android:textSize="20sp" /> android:textSize="20sp" />
<Button <Button
android:id="@+id/settings_about"
style="@style/Widget.AppCompat.Button.Borderless" style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -80,6 +83,7 @@ ...@@ -80,6 +83,7 @@
android:textSize="20sp" /> android:textSize="20sp" />
<Button <Button
android:id="@+id/settings_logout"
style="@style/Widget.AppCompat.Button.Borderless" style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment