Added automatic login - if user exists

This commit is contained in:
Sagi Dayan 2016-07-06 20:30:51 +03:00
parent f2b53bc1eb
commit 884e29a1ee
8 changed files with 146 additions and 67 deletions

View file

@ -1,6 +1,8 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
@ -111,6 +113,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
try { try {
if(!response.isNull("user")) { if(!response.isNull("user")) {
Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console
SharedPreferences sharedPref = self.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("UserId", response.getJSONObject("user").getString("id"));
editor.commit();
Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class); Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class);
Bundle loginUserBundle = new Bundle(); Bundle loginUserBundle = new Bundle();
loginUserBundle.putString("user_id", response.getJSONObject("user").getString("id")); loginUserBundle.putString("user_id", response.getJSONObject("user").getString("id"));

View file

@ -19,7 +19,7 @@ import android.view.MenuItem;
public class MainActivity extends AppCompatActivity public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener { implements NavigationView.OnNavigationItemSelectedListener {
public static final String LOCAL_DATA_TSN = "TSN_DATA_STORE";
protected final String TAG = "[TSN / MainActivity]"; protected final String TAG = "[TSN / MainActivity]";
protected String userId; protected String userId;
private GPSUtils gpsUtils; private GPSUtils gpsUtils;

View file

@ -1,8 +1,10 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.Manifest; import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -16,11 +18,14 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Switch; import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.android.volley.Response; import com.android.volley.Response;
@ -74,7 +79,7 @@ public class PersonalFragment extends Fragment {
listOfNotes = new ArrayList<>(); listOfNotes = new ArrayList<>();
noteListAdapter = new ListAdapter(getContext(), listOfNotes); noteListAdapter = new ListAdapter(getContext(), listOfNotes);
noteList.setAdapter(noteListAdapter); noteList.setAdapter(noteListAdapter);
// new HeavyWorker(this).execute(); noteList.setOnItemClickListener(new ItemClickedListener());
MainActivity.showLoadingDialog(getActivity(), "Fetching..", "getting your notes"); MainActivity.showLoadingDialog(getActivity(), "Fetching..", "getting your notes");
getAllNotes(); getAllNotes();
@ -87,7 +92,6 @@ public class PersonalFragment extends Fragment {
} }
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);
@ -138,16 +142,14 @@ public class PersonalFragment extends Fragment {
public void onClick(View v) { public void onClick(View v) {
//title too short //title too short
if (newTitle.getText().length() == 0) if (newTitle.getText().length() == 0) {
{
Toast toast = Toast.makeText(getActivity(), "Title too short.", Toast.LENGTH_LONG); Toast toast = Toast.makeText(getActivity(), "Title too short.", Toast.LENGTH_LONG);
toast.show(); toast.show();
return; return;
} }
//title too long //title too long
if (newTitle.getText().length() > 20) if (newTitle.getText().length() > 20) {
{
Toast toast = Toast.makeText(getActivity(), "Title too long.\n Use up to 20 notes.", Toast.LENGTH_LONG); Toast toast = Toast.makeText(getActivity(), "Title too long.\n Use up to 20 notes.", Toast.LENGTH_LONG);
toast.show(); toast.show();
return; return;
@ -202,21 +204,6 @@ public class PersonalFragment extends Fragment {
JSONObject noteObject = response.getJSONObject("note"); JSONObject noteObject = response.getJSONObject("note");
time.setTime(noteObject.getLong("created_at")); time.setTime(noteObject.getLong("created_at"));
addNoteFromJsonObj(noteObject, time); addNoteFromJsonObj(noteObject, time);
// Note addNote = new Note(
// noteObject.getString("id"),
// Float.parseFloat(noteObject.getJSONObject("location").getString("lat")),
// Float.parseFloat(noteObject.getJSONObject("location").getString("lng")),
// noteObject.getJSONObject("location").getString("address"),
// noteObject.getString("title"),
// noteObject.getString("body"),
// time.toString(),
// noteObject.getBoolean("is_public"),
// noteObject.getInt("likes"),
// jsonArrayToStringArray(noteObject.getJSONArray("tags"))
// );
//
// listOfNotes.add(addNote);
noteList.setAdapter(noteListAdapter); noteList.setAdapter(noteListAdapter);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage()); Log.e(TAG, "newNoteSuccess:" + e.getMessage());
@ -250,19 +237,6 @@ public class PersonalFragment extends Fragment {
time.setTime(noteObject.getLong("created_at")); time.setTime(noteObject.getLong("created_at"));
addNoteFromJsonObj(noteObject, time); addNoteFromJsonObj(noteObject, time);
// Note addNote = new Note(
// noteObject.getString("id"),
// Float.parseFloat(noteObject.getJSONObject("location").getString("lat")),
// Float.parseFloat(noteObject.getJSONObject("location").getString("lng")),
// noteObject.getJSONObject("location").getString("address"),
// noteObject.getString("title"),
// noteObject.getString("body"),
// time.toString(),
// noteObject.getBoolean("is_public"),
// noteObject.getInt("likes"),
// jsonArrayToStringArray(noteObject.getJSONArray("tags"))
// );
// listOfNotes.add(addNote);
} }
noteList.setAdapter(noteListAdapter); noteList.setAdapter(noteListAdapter);
} catch (Exception e) { } catch (Exception e) {
@ -293,22 +267,26 @@ public class PersonalFragment extends Fragment {
}; };
private ArrayList<String> jsonArrayToStringArray(JSONArray jArray) { private ArrayList<String> jsonArrayToStringArray(JSONArray jArray) {
ArrayList<String> stringArray = new ArrayList<String>(); ArrayList<String> stringArray = new ArrayList<String>();
for(int i = 0, count = jArray.length(); i< count; i++) for (int i = 0, count = jArray.length(); i < count; i++) {
{
try { try {
JSONObject jsonObject = jArray.getJSONObject(i); JSONObject jsonObject = jArray.getJSONObject(i);
stringArray.add(jsonObject.toString()); stringArray.add(jsonObject.toString());
} } catch (JSONException e) {
catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return stringArray; return stringArray;
} }
Response.Listener<JSONObject> deleteNoteSuccessListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "deleteNoteSuccessListener: " + response.toString());
}
};
private void addNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException { private void addNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException {
Note addNote = new Note( Note addNote = new Note(
noteObject.getString("id"), noteObject.getString("id"),
@ -326,4 +304,87 @@ public class PersonalFragment extends Fragment {
} }
// click on listView item
class ItemClickedListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//create and configure dialog
final Note note = listOfNotes.get(position);
final Dialog noteViewDialog = new Dialog(getActivity());
noteViewDialog.setContentView(R.layout.note_display_full);
noteViewDialog.setTitle("You wrote...");
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(noteViewDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
noteViewDialog.show();
// dialog.getWindow().setAttributes(lp);
//get note_view_full layout elements
final TextView title = (TextView) noteViewDialog.findViewById(R.id.ndf_title_textview);
final TextView body = (TextView) noteViewDialog.findViewById(R.id.ndf_body_textview);
final TextView time = (TextView) noteViewDialog.findViewById(R.id.ndf_time_textview);
final TextView location = (TextView) noteViewDialog.findViewById(R.id.ndf_address_textview);
final TextView likes = (TextView) noteViewDialog.findViewById(R.id.ndf_likes_textview);
final TextView tags = (TextView) noteViewDialog.findViewById(R.id.ndf_tags_textview);
final TextView permission = (TextView) noteViewDialog.findViewById(R.id.ndf_permission_textview);
final ImageButton deleteBtn = (ImageButton) noteViewDialog.findViewById(R.id.ndf_delete_imagebutton);
title.setText(note.getTitle());
body.setText(note.getBody());
time.setText(note.getTimestamp());
location.setText("Tags: " + note.getAddress());
likes.setText("Likes: " + note.getLikes());
tags.setText(note.getTags().toString());
permission.setText("Permission: " + (note.isPublic() ? "Public" : "Private"));
deleteBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Put up the Yes/No message box
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setTitle("Delete Note")
.setMessage("Are you sure you want to delete the note?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(getActivity(), "Item Deleted!",
Toast.LENGTH_SHORT).show();
//TODO send delete
JSONObject delNote = new JSONObject();
try {
delNote.put("uid", userId);
delNote.put("nid", note.getId());
VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, deleteNoteSuccessListener, genericErrorListener);
listOfNotes.remove(position);
} catch (JSONException e) {
Toast.makeText(getActivity(), "Something went wrong.\n Failed to delete note...", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
noteList.setAdapter(noteListAdapter);
noteViewDialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(getActivity(), "Note still here!",
Toast.LENGTH_SHORT).show();
noteViewDialog.dismiss();
}
}) //Do nothing on no
.show();
}
});
}
}
} }

View file

@ -1,6 +1,8 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.graphics.drawable.DrawableWrapper; import android.support.v7.graphics.drawable.DrawableWrapper;
@ -31,6 +33,9 @@ public class SplashActivity extends AppCompatActivity {
} else { } else {
background.setImageDrawable( getResources().getDrawable(rand_splash())); background.setImageDrawable( getResources().getDrawable(rand_splash()));
} }
SharedPreferences sharedPref = this.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE);
final String userId = sharedPref.getString("UserId", null);
Thread timerThread = new Thread(){ Thread timerThread = new Thread(){
public void run(){ public void run(){
try{ try{
@ -38,7 +43,15 @@ public class SplashActivity extends AppCompatActivity {
}catch(InterruptedException e){ }catch(InterruptedException e){
e.printStackTrace(); e.printStackTrace();
}finally{ }finally{
if(userId == null){
startActivity(new Intent(SplashActivity.this, LoginActivity.class)); startActivity(new Intent(SplashActivity.this, LoginActivity.class));
}else{
Intent personalSpaceActivity = new Intent(SplashActivity.this, MainActivity.class);
Bundle loginUserBundle = new Bundle();
loginUserBundle.putString("user_id", userId);
personalSpaceActivity.putExtras(loginUserBundle);
startActivity(personalSpaceActivity);
}
finish(); finish();
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View file

@ -4,13 +4,6 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:weightSum="1">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -30,6 +23,12 @@
android:textColor="#c5c4c4" android:textColor="#c5c4c4"
android:hint="Title" /> android:hint="Title" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/scrollView"
android:layout_weight="0.5">
<EditText <EditText
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -38,6 +37,7 @@
android:id="@+id/nvf_note_content" android:id="@+id/nvf_note_content"
android:hint="Note Body" android:hint="Note Body"
android:layout_weight="0.24" /> android:layout_weight="0.24" />
</ScrollView>
<Switch <Switch
android:id="@+id/nvf_note_permission" android:id="@+id/nvf_note_permission"
@ -76,6 +76,5 @@
</LinearLayout> </LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>