Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updateuser callback #152 #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion app/src/main/java/com/kanshu/kanshu/ApiHandler.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
package com.kanshu.kanshu;

import android.os.StrictMode;
import android.util.Base64;
import android.util.Log;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.ExecutionException;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class ApiHandler {


public static RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://kanshu-ds.herokuapp.com").setLogLevel(RestAdapter.LogLevel.FULL).build();
public static KanshuApi kanshuApi = restAdapter.create(KanshuApi.class);
}

public static JsonObject login(String username, String password) {
String credentials = username + ":" + password;
String authentication = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
JsonObject user = null;
try {
user = new LoginTask().execute(authentication).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//Log.i("ApiHandler",info.getUser().toString());

return user;

}
}

14 changes: 12 additions & 2 deletions app/src/main/java/com/kanshu/kanshu/KanshuApi.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.kanshu.kanshu;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kanshu.kanshu.model.SavedChars;

import org.json.JSONObject;

import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.Body;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
Expand All @@ -14,10 +20,14 @@

public interface KanshuApi {
@GET("/login")
public void login(@Header("Authorization")String hash, Callback<JsonObject> session);
JsonObject login(@Header("Authorization")String hash);

@POST("/createUser")
public void createUser(@Body SignupActivity.SignupPacket packet, Callback<String> callback);
public void createUser(@Body SignupPacket packet, Callback<String> callback);

//Content-Type should always be application/json
@POST("/updateUser")
public void updateUser(@Header("sessionid")String sessionID,@Header("Content-Type")String contentType, @Body SignupPacket packet, Callback<JsonObject> callback);

@POST("/deleteword")
public void deleteWord(@Header("sessionid")String token, @Body SavedChars.WordPacket wordPacket, Callback<String> callback);
Expand Down
41 changes: 21 additions & 20 deletions app/src/main/java/com/kanshu/kanshu/LoginActivity.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.kanshu.kanshu;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.kanshu.kanshu.model.User;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;



public class LoginActivity extends BaseActivity {

@Override
Expand Down Expand Up @@ -48,25 +51,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

public void Login(View v){
public void Login(View v) {
String username = ((EditText) findViewById(R.id.email)).getText().toString();
final String password = ((EditText) findViewById(R.id.password)).getText().toString();
final Intent loginIntent = new Intent(this, UserMetricsActivity.class);
JsonObject user = ApiHandler.login(username, password);
if(user != null) {
final SharedPreferences sharedPreferences = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);

final Intent loginIntent = new Intent(this,UserMetricsActivity.class);
String credentials = ((EditText)findViewById(R.id.email)).getText().toString() + ":" + ((EditText)findViewById(R.id.password)).getText().toString();
String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
ApiHandler.kanshuApi.login(string, new Callback<JsonObject>() {
@Override
public void success(JsonObject s, Response response) {
User userData = new User(((EditText)findViewById(R.id.email)).getText().toString(),"user");
userData.setSessionId(s.get("user").getAsJsonObject().get("sessionId").getAsString());
loginIntent.putExtra("user", userData);
startActivity(loginIntent);
}

@Override
public void failure(RetrofitError error) {
Log.i("LoginActivity", "EPIC FAIL!");
Log.i("LoginActivity", error.toString());
}
});
User userData = new User(user.get("email").toString(), user.get("userBio").toString());
userData.setSessionId(user.get("sessionId").toString().substring(1, user.get("sessionId").toString().length() -1));//remove the quotes around the sessionId
loginIntent.putExtra("user", userData);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sessionId", user.get("sessionId").toString().substring(1, user.get("sessionId").toString().length() - 1));//This gets stored so other activities can use it.(like the settingsAccount activity)
editor.putString("password", password);//This gets stored because there is no password field in the settings activity. It is however needed for the updateUser call.
editor.commit();
startActivity(loginIntent);
}
}
}

14 changes: 14 additions & 0 deletions app/src/main/java/com/kanshu/kanshu/LoginTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kanshu.kanshu;

import android.os.AsyncTask;

import com.google.gson.JsonObject;

public class LoginTask extends AsyncTask<String, Integer, JsonObject> {
@Override
protected JsonObject doInBackground(String... params) {
JsonObject userData = ApiHandler.kanshuApi.login(params[0]);

return userData.getAsJsonObject("user");
}
}
81 changes: 26 additions & 55 deletions app/src/main/java/com/kanshu/kanshu/SignupActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.kanshu.kanshu;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
Expand All @@ -27,17 +27,15 @@
import retrofit.client.Response;


public class SignupActivity extends BaseActivity implements AdapterView.OnItemSelectedListener{
public class SignupActivity extends BaseActivity {


//skip button
TextView skipTv;
Spinner signUpSpinner;
String selectedLevel;
int selectedLevelID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Spinner signUpSpinner;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
signUpSpinner = (Spinner) findViewById(R.id.spinner);
Expand Down Expand Up @@ -69,7 +67,6 @@ public View getDropDownView(int position, View convertView,
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
signUpSpinner.setAdapter(adapter);
signUpSpinner.setOnItemSelectedListener(this);
TextView kanshuText = (TextView) findViewById(R.id.mainheadline);
kanshuText.setText(R.string.kanshucaption, TextView.BufferType.SPANNABLE);
int startPos = getString(R.string.kanshucaption).indexOf("Kanshu");
Expand Down Expand Up @@ -120,60 +117,34 @@ public void onSelectLogInPage(View clicked) {
}

public void onSignUp(View clicked) {
final String username = ((EditText) findViewById(R.id.email)).getText().toString();
final String password = ((EditText) findViewById(R.id.password)).getText().toString();
final Intent signupIntent = new Intent(this, UserMetricsActivity.class);
ApiHandler.kanshuApi.createUser(new SignupPacket(((EditText) findViewById(R.id.password)).getText().toString(), ((EditText) findViewById(R.id.email)).getText().toString(), "Test user", "somewhere"), new Callback<String>() {
final int spinnerValue = (int)((Spinner) findViewById(R.id.spinner)).getSelectedItemId();
final SharedPreferences sharedPreferences = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
ApiHandler.kanshuApi.createUser(new SignupPacket(password, username,((EditText) findViewById(R.id.username)).getText().toString(), "Test user", "somewhere", spinnerValue), new Callback<String>() {
@Override

public void success(String s, Response response) {
//Log.i("SignupActivity", s);
String credentials = ((EditText)findViewById(R.id.email)).getText().toString() + ":" + ((EditText)findViewById(R.id.password)).getText().toString();
String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
ApiHandler.kanshuApi.login(string, new Callback<JsonObject>() {
@Override
public void success(JsonObject s, Response response) {
User userData = new User(((EditText) findViewById(R.id.username)).getText().toString(), selectedLevel);
userData.setSessionId(s.get("user").getAsJsonObject().get("sessionId").getAsString());
signupIntent.putExtra("user", userData);
startActivity(signupIntent);
}

@Override
public void failure(RetrofitError error) {
Log.i("SignupActivity -- Login", "Epic Fail!");
}
});
}

@Override
public void failure(RetrofitError error) {
Log.i("SignupActivity", "Epic Fail!");
}
});

}
JsonObject user = ApiHandler.login(username, password);
if (user != null) {//It should not be null but you never know.

User userData = new User(user.get("username").toString(), user.get("userBio").toString());
userData.setSessionId(user.get("sessionId").toString().substring(1,user.get("sessionId").toString().length() - 1));//remove the quotes around the session id
signupIntent.putExtra("user", userData);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("hsklevel", spinnerValue);
editor.putString("sessionId",user.get("sessionId").toString().substring(1,user.get("sessionId").toString().length() - 1));
editor.commit();
startActivity(signupIntent);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedLevelID = position;
selectedLevel = signUpSpinner.getItemAtPosition(position).toString();
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
@Override
public void failure(RetrofitError error) {

}
}
});

public class SignupPacket{
public String password;
public String email;
public String userBio;
public String country;

public SignupPacket(String password, String email, String userBio, String country){
this.password = password;
this.email = email;
this.userBio = userBio;
this.country = country;
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/kanshu/kanshu/SignupPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.kanshu.kanshu;

public class SignupPacket {

public String password;
public String email;
public String userBio;
public String profileImageUrl;
public String username;
public String country;
public int hsklevel;

public SignupPacket(String password, String email,String username, String userBio, String country, int hsklevel){
this.password = password;
this.email = email;
this.userBio = userBio;
this.profileImageUrl = "http://someurl.com/image.png"; //This can be replaced by the actual url once there is a field in the activity
this.username = username;
this.country = country;
this.hsklevel = hsklevel;
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/kanshu/kanshu/activity/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package com.kanshu.kanshu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.kanshu.kanshu.ApiHandler;
import com.kanshu.kanshu.R;
import com.kanshu.kanshu.SignupPacket;
import com.kanshu.kanshu.fragment.MoreSettingsFragment;
import com.kanshu.kanshu.fragment.MyAccountFragment;

import java.util.logging.Logger;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
* Activity for User account and app settings
* @author Victor Sima
Expand Down Expand Up @@ -63,4 +78,22 @@ public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
public void saveAccountInfo(View v) {
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String sessionId = sharedPreferences.getString("sessionId", "");
String password = sharedPreferences.getString("password", "password");
int hskLevel = sharedPreferences.getInt("hsklevel", 0);
ApiHandler.kanshuApi.updateUser(sessionId,"application/json", new SignupPacket(password, ((EditText) findViewById(R.id.emailField)).getText().toString(),((EditText) findViewById(R.id.user_name)).getText().toString(),((EditText)findViewById(R.id.userBio)).getText().toString(), ((EditText) findViewById((R.id.countryField))).getText().toString(), hskLevel), new Callback< JsonObject>(){

@Override
public void success(JsonObject jsonObject, Response response) {
Log.i("updateUser", "succes");
}

@Override
public void failure(RetrofitError error) {

}
});
}
}
Loading