Skip to content

Commit 37b5d15

Browse files
- Payout HTTP Request
1 parent a1f74eb commit 37b5d15

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

app/src/main/java/com/simcoder/uber/CustomerMapActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public void onMapReady(GoogleMap googleMap) {
489489
checkLocationPermission();
490490
}
491491
}
492-
492+
493493
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
494494
mMap.setMyLocationEnabled(true);
495495
}

app/src/main/java/com/simcoder/uber/DriverMapActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ public void onLocationResult(LocationResult locationResult) {
398398
if(!customerId.equals("") && mLastLocation!=null && location != null){
399399
rideDistance += mLastLocation.distanceTo(location)/1000;
400400
}
401+
mLastLocation = location;
401402

402403

403404
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());

app/src/main/java/com/simcoder/uber/HistoryActivity.java

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.simcoder.uber;
22

33
import android.annotation.SuppressLint;
4+
import android.app.ProgressDialog;
5+
import android.support.design.widget.Snackbar;
46
import android.support.v7.app.AppCompatActivity;
57
import android.os.Bundle;
68
import android.support.v7.widget.LinearLayoutManager;
79
import android.support.v7.widget.RecyclerView;
810
import android.text.format.DateFormat;
11+
import android.util.Log;
912
import android.view.View;
13+
import android.widget.Button;
14+
import android.widget.EditText;
1015
import android.widget.TextView;
1116

1217
import com.google.firebase.auth.FirebaseAuth;
@@ -18,10 +23,22 @@
1823
import com.simcoder.uber.historyRecyclerView.HistoryAdapter;
1924
import com.simcoder.uber.historyRecyclerView.HistoryObject;
2025

26+
import org.json.JSONException;
27+
import org.json.JSONObject;
28+
29+
import java.io.IOException;
2130
import java.util.ArrayList;
2231
import java.util.Calendar;
2332
import java.util.Locale;
2433

34+
import okhttp3.Call;
35+
import okhttp3.Callback;
36+
import okhttp3.MediaType;
37+
import okhttp3.OkHttpClient;
38+
import okhttp3.Request;
39+
import okhttp3.RequestBody;
40+
import okhttp3.Response;
41+
2542

2643
public class HistoryActivity extends AppCompatActivity {
2744
private String customerOrDriver, userId;
@@ -34,12 +51,18 @@ public class HistoryActivity extends AppCompatActivity {
3451

3552
private Double Balance = 0.0;
3653

54+
private Button mPayout;
55+
56+
private EditText mPayoutEmail;
57+
3758
@Override
3859
protected void onCreate(Bundle savedInstanceState) {
3960
super.onCreate(savedInstanceState);
4061
setContentView(R.layout.activity_history);
4162

4263
mBalance = findViewById(R.id.balance);
64+
mPayout = findViewById(R.id.payout);
65+
mPayoutEmail = findViewById(R.id.payoutEmail);
4366

4467
mHistoryRecyclerView = (RecyclerView) findViewById(R.id.historyRecyclerView);
4568
mHistoryRecyclerView.setNestedScrollingEnabled(false);
@@ -56,7 +79,16 @@ protected void onCreate(Bundle savedInstanceState) {
5679

5780
if(customerOrDriver.equals("Drivers")){
5881
mBalance.setVisibility(View.VISIBLE);
82+
mPayout.setVisibility(View.VISIBLE);
83+
mPayoutEmail.setVisibility(View.VISIBLE);
5984
}
85+
86+
mPayout.setOnClickListener(new View.OnClickListener() {
87+
@Override
88+
public void onClick(View view) {
89+
payoutRequest();
90+
}
91+
});
6092
}
6193

6294
private void getUserHistoryIds() {
@@ -94,8 +126,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
94126

95127
if(dataSnapshot.child("customerPaid").getValue() != null && dataSnapshot.child("driverPaidOut").getValue() == null){
96128
if(dataSnapshot.child("distance").getValue() != null){
97-
distance = dataSnapshot.child("distance").getValue().toString();
98-
ridePrice = (Double.valueOf(distance) * 0.4);
129+
ridePrice = Double.valueOf(dataSnapshot.child("price").getValue().toString());
99130
Balance += ridePrice;
100131
mBalance.setText("Balance: " + String.valueOf(Balance) + " $");
101132
}
@@ -124,4 +155,70 @@ private String getDate(Long time) {
124155
private ArrayList<HistoryObject> getDataSetHistory() {
125156
return resultsHistory;
126157
}
158+
159+
160+
161+
162+
public static final MediaType MEDIA_TYPE = MediaType.parse("application/json");
163+
ProgressDialog progress;
164+
private void payoutRequest() {
165+
progress = new ProgressDialog(this);
166+
progress.setTitle("Processing your payout");
167+
progress.setMessage("Please Wait...");
168+
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
169+
progress.show();
170+
171+
final OkHttpClient client = new OkHttpClient();
172+
JSONObject postData = new JSONObject();
173+
try {
174+
postData.put("uid", FirebaseAuth.getInstance().getCurrentUser().getUid());
175+
postData.put("email", mPayoutEmail.getText());
176+
} catch (JSONException e) {
177+
e.printStackTrace();
178+
}
179+
180+
RequestBody body = RequestBody.create(MEDIA_TYPE,
181+
postData.toString());
182+
183+
final Request request = new Request.Builder()
184+
.url("https://us-central1-uberapp-408c8.cloudfunctions.net/payout")
185+
.post(body)
186+
.addHeader("Content-Type", "application/json")
187+
.addHeader("Authorization", "Your Token")
188+
.addHeader("cache-control", "no-cache")
189+
.build();
190+
client.newCall(request).enqueue(new Callback() {
191+
@Override
192+
public void onFailure(Call call, IOException e) {
193+
String mMessage = e.getMessage().toString();
194+
Log.w("failure Response", mMessage);
195+
progress.dismiss();
196+
}
197+
198+
@Override
199+
public void onResponse(Call call, Response response)
200+
throws IOException {
201+
202+
int responseCode = response.code();
203+
204+
205+
if (response.isSuccessful())
206+
switch (responseCode) {
207+
case 200:
208+
Snackbar.make(findViewById(R.id.layout), "Payout Successful!", Snackbar.LENGTH_LONG).show();
209+
break;
210+
case 501:
211+
Snackbar.make(findViewById(R.id.layout), "Error: no payout available", Snackbar.LENGTH_LONG).show();
212+
break;
213+
default:
214+
Snackbar.make(findViewById(R.id.layout), "Error: couldn't complete the transaction", Snackbar.LENGTH_LONG).show();
215+
break;
216+
}
217+
else
218+
Snackbar.make(findViewById(R.id.layout), "Error: couldn't complete the transaction", Snackbar.LENGTH_LONG).show();
219+
220+
progress.dismiss();
221+
}
222+
});
223+
}
127224
}

app/src/main/res/layout/activity_history.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
android:layout_height="match_parent"
88
tools:context="com.simcoder.uber.HistoryActivity"
99
android:fitsSystemWindows="true"
10-
android:orientation="vertical">
10+
android:orientation="vertical"
11+
android:id="@+id/layout">
1112
<LinearLayout
1213
android:layout_width="match_parent"
1314
android:layout_height="wrap_content"
@@ -16,9 +17,21 @@
1617
android:padding="20sp"
1718
android:layout_width="match_parent"
1819
android:layout_height="wrap_content"
19-
android:text="Balance: 1000"
20+
android:text="Balance: 0"
2021
android:id="@+id/balance"
2122
android:visibility="gone"/>
23+
<EditText
24+
android:visibility="gone"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:id="@+id/payoutEmail"
28+
android:hint="paypal email"/>
29+
<Button
30+
android:visibility="gone"
31+
android:text="payout"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:id="@+id/payout"/>
2235
</LinearLayout>
2336

2437
<android.support.v4.widget.NestedScrollView

0 commit comments

Comments
 (0)