app-mobile/app/src/main/java/monnethic/mobile/user/UserAccountActivity.java
2019-04-13 22:07:48 +02:00

153 lines
5.4 KiB
Java

package monnethic.mobile.user;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.monnthic.monnethicmobile.R;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import monnethic.mobile.database.User;
import monnethic.mobile.restApi.Config;
import monnethic.mobile.restApi.HttpCallHandler;
import monnethic.mobile.restApi.WalletApiHandler;
import monnethic.mobile.transaction.MakePayementActivity;
import monnethic.mobile.transaction.ReceivePayementActivity;
public class UserAccountActivity extends AppCompatActivity {
private TextView balance;
private String wallet_hash;
private String user_hash;
private String user_password;
private String session_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_account);
balance = findViewById(R.id.balanceView);
TextView userHashView = findViewById(R.id.userHash);
Button buttonPayement = findViewById(R.id.buttonPayement);
Button buttonReceive = findViewById(R.id.buttonReceive);
Button buttonSettings = findViewById(R.id.buttonSettings);
Button buttonRefresh = findViewById(R.id.buttonRefreshBalance);
Intent intent = getIntent();
user_hash = intent.getStringExtra("USER_HASH");
user_password = intent.getStringExtra("USER_PWD");
session_id = intent.getStringExtra("SESSION_ID");
wallet_hash = intent.getStringExtra("WALLET_HASH");
String wallet_type = intent.getStringExtra("WALLET_TYPE");
String wallet_balance = intent.getStringExtra("WALLET_BALANCE");
userHashView.setText(wallet_hash);
balance.setText(wallet_balance);
buttonPayement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendPayement();
}
});
buttonReceive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
receivePayement();
}
});
buttonSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
settings();
}
});
buttonRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new GetUserBalanceTask().execute(wallet_hash);
}
});
Log.i("UserAccountActivity", "user_hash : "+user_hash);
Log.i("UserAccountActivity", "user_password : "+user_password);
Log.i("UserAccountActivity", "session_id : "+session_id);
Log.i("UserAccountActivity", "wallet_hash : "+wallet_hash);
String[] params = {wallet_hash};
new GetUserBalanceTask().execute(params);
}
public void sendPayement(){
Intent payementIntent = new Intent(UserAccountActivity.this, MakePayementActivity.class);
payementIntent.putExtra("USER_HASH",user_hash);
payementIntent.putExtra("USER_PWD",user_password);
payementIntent.putExtra("WALLET_HASH",wallet_hash);
payementIntent.putExtra("SESSION_ID",session_id);
UserAccountActivity.this.startActivity(payementIntent);
}
public void receivePayement(){
Intent receivePayementIntent = new Intent(UserAccountActivity.this, ReceivePayementActivity.class);
receivePayementIntent.putExtra("USER_HASH",user_hash);
receivePayementIntent.putExtra("USER_PWD",user_password);
receivePayementIntent.putExtra("WALLET_HASH",wallet_hash);
receivePayementIntent.putExtra("SESSION_ID",session_id);
UserAccountActivity.this.startActivity(receivePayementIntent);
}
public void settings(){
Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
}
public class GetUserBalanceTask extends AsyncTask<String,String,Double>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Double b) {
if(b!=null){
balance.setText(String.valueOf(b));
}else{
balance.setText("0");
}
}
@Override
protected Double doInBackground(String... params) {
try{
WalletApiHandler walletApiHandler = new WalletApiHandler();
Double newBalance = walletApiHandler.getWalletBalance(params);
if(newBalance!=null){
return newBalance;
}else{
return null;
}
} catch (Exception e){
e.printStackTrace();
return null;
}
}
}
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
Toast.makeText(getApplicationContext(), "onStop called", Toast.LENGTH_LONG).show();
}
}