Skip to content

Commit d3307af

Browse files
committed
add the java version
1 parent c52976a commit d3307af

File tree

13 files changed

+303
-47
lines changed

13 files changed

+303
-47
lines changed

.idea/misc.xml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
2-
* Kotlin Script
2+
* Java Script
33
*
44
* @author Nguyen Truong Thinh
55
* Contact me: nguyentruongthinhvn2020@gmail.com || +84393280504
66
* */
7+
78
plugins {
89
id 'com.android.application'
910
id 'org.jetbrains.kotlin.android'
@@ -46,9 +47,6 @@ android {
4647
sourceCompatibility JavaVersion.VERSION_1_8
4748
targetCompatibility JavaVersion.VERSION_1_8
4849
}
49-
kotlinOptions {
50-
jvmTarget = '1.8'
51-
}
5250

5351
buildFeatures {
5452
viewBinding true
@@ -62,14 +60,16 @@ android {
6260

6361
dependencies {
6462

65-
implementation 'androidx.core:core-ktx:1.7.0'
66-
implementation 'androidx.appcompat:appcompat:1.4.1'
63+
implementation 'androidx.appcompat:appcompat:1.4.2'
6764
implementation "com.google.android.material:material:$material_version"
6865
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
66+
implementation 'androidx.cardview:cardview:1.0.0'
67+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
6968
testImplementation 'junit:junit:4.13.2'
7069
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
7170
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
7271

72+
7373
// Navigation
7474
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
7575
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.forever.bee.akgraphql;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.forever.bee.akgraphql", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.forever.bee.akgraphql">
5-
65
<uses-permission android:name="android.permission.INTERNET" />
7-
86
<application
97
android:allowBackup="true"
108
android:dataExtractionRules="@xml/data_extraction_rules"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.forever.bee.akgraphql;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.os.Bundle;
6+
7+
import com.forever.bee.akgraphql.databinding.ActivityMainBinding;
8+
9+
public class MainActivity extends AppCompatActivity {
10+
11+
private ActivityMainBinding binding;
12+
13+
@Override
14+
protected void onCreate(Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
17+
binding = ActivityMainBinding.inflate(getLayoutInflater());
18+
setContentView(binding.getRoot());
19+
}
20+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.forever.bee.akgraphql.ui;
2+
3+
import android.annotation.SuppressLint;
4+
import android.util.Log;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.TextView;
9+
import androidx.annotation.NonNull;
10+
import androidx.recyclerview.widget.RecyclerView;
11+
import com.forever.bee.akgraphql.GetAllTrips;
12+
import com.forever.bee.akgraphql.R;
13+
import java.text.DateFormat;
14+
import java.text.ParseException;
15+
import java.text.SimpleDateFormat;
16+
import java.util.Date;
17+
import java.util.List;
18+
19+
public class TripsAdapter extends RecyclerView.Adapter<TripsAdapter.TripsViewHolder> {
20+
private final List<GetAllTrips.AllTrip> trips;
21+
private final LayoutInflater inflater;
22+
private final DateFormat dateFormat;
23+
24+
protected TripsAdapter(List<GetAllTrips.AllTrip> trips,
25+
LayoutInflater inflater, DateFormat dateFormat) {
26+
this.trips = trips;
27+
this. inflater = inflater;
28+
this.dateFormat = dateFormat;
29+
}
30+
31+
@SuppressLint("SimpleDateFormat")
32+
private static final SimpleDateFormat ISO861 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
33+
34+
@NonNull
35+
@Override
36+
public TripsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
37+
return (new TripsViewHolder(inflater.inflate(R.layout.trip_item, parent, false), dateFormat));
38+
}
39+
40+
@Override
41+
public void onBindViewHolder(@NonNull TripsViewHolder holder, int position) {
42+
holder.bind(trips.get(position));
43+
}
44+
45+
@Override
46+
public int getItemCount() {
47+
return trips.size();
48+
}
49+
50+
protected static class TripsViewHolder extends RecyclerView.ViewHolder {
51+
private final TextView title;
52+
private final TextView startTime;
53+
private final TextView intro;
54+
private final DateFormat dateFormat;
55+
56+
TripsViewHolder(View itemView, DateFormat dateFormat) {
57+
super(itemView);
58+
59+
title = (TextView)itemView.findViewById(R.id.title);
60+
startTime = (TextView)itemView.findViewById(R.id.start_time);
61+
intro = (TextView)itemView.findViewById(R.id.intro);
62+
this.dateFormat = dateFormat;
63+
}
64+
65+
void bind(GetAllTrips.AllTrip trip) {
66+
try {
67+
Date parsedStartTime = ISO861.parse(trip.startTime());
68+
69+
title.setText(trip.title());
70+
71+
if (parsedStartTime != null) {
72+
startTime.setText(dateFormat.format(parsedStartTime.toString()));
73+
}
74+
75+
intro.setText(R.string.kotlin_and_graphql);
76+
77+
} catch (ParseException e) {
78+
Log.e(getClass().getSimpleName(), "Exception parsing "+trip.startTime(), e);
79+
}
80+
}
81+
}
82+
}
83+
84+
85+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.forever.bee.akgraphql.ui;
2+
3+
import android.os.Bundle;
4+
import android.util.Log;
5+
import android.view.View;
6+
import android.widget.Toast;
7+
import androidx.annotation.NonNull;
8+
import androidx.annotation.Nullable;
9+
import androidx.recyclerview.widget.DividerItemDecoration;
10+
import androidx.recyclerview.widget.LinearLayoutManager;
11+
import androidx.recyclerview.widget.RecyclerView;
12+
import com.apollographql.apollo.ApolloClient;
13+
import com.apollographql.apollo.api.Response;
14+
import com.apollographql.apollo.rx2.Rx2Apollo;
15+
import com.forever.bee.akgraphql.BuildConfig;
16+
import com.forever.bee.akgraphql.GetAllTrips;
17+
import java.util.Objects;
18+
import io.reactivex.Observable;
19+
import io.reactivex.android.schedulers.AndroidSchedulers;
20+
import io.reactivex.disposables.Disposable;
21+
import io.reactivex.schedulers.Schedulers;
22+
import okhttp3.OkHttpClient;
23+
24+
/**
25+
* Build the [TripsFragment] class
26+
*/
27+
public class TripsFragment extends RecyclerViewFragment {
28+
//An instance of ApolloClient, this is a wrapper around an OkHttpClient that knows about Apollo-Android
29+
private final ApolloClient apolloClient = ApolloClient.builder()
30+
.okHttpClient(new OkHttpClient())
31+
.serverUrl(BuildConfig.GRAPHQL_API_KEY)
32+
.build();
33+
// An instance of RxJava Observable chains
34+
private Observable<GetAllTrips.Data> observable;
35+
private Disposable sub;
36+
37+
38+
@Override
39+
public void onCreate(@Nullable Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
// Setting up RxJava Observable chains using the Rx2Apollo helper class
42+
observable = Rx2Apollo.from(apolloClient.query(new GetAllTrips()).watcher())
43+
.subscribeOn(Schedulers.io())
44+
.map(this::getAllTripsFields)
45+
.cache()
46+
.observeOn(AndroidSchedulers.mainThread());
47+
48+
}
49+
50+
51+
@Override
52+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
53+
super.onViewCreated(view, savedInstanceState);
54+
55+
setLayoutManager(new LinearLayoutManager(getActivity()));
56+
57+
getRecyclerView()
58+
.addItemDecoration(new DividerItemDecoration(getActivity(),
59+
LinearLayoutManager.VERTICAL));
60+
61+
unsub();
62+
63+
sub = observable.subscribe(
64+
s -> setAdapter(buildRVAdapter(s)),
65+
error -> {
66+
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
67+
Log.e(getClass().getSimpleName(), "Exception processing request",
68+
error);
69+
}
70+
);
71+
}
72+
73+
@Override
74+
public void onDestroy() {
75+
unsub();
76+
super.onDestroy();
77+
}
78+
79+
private void unsub() {
80+
if (sub != null && !sub.isDisposed()) {
81+
sub.dispose();
82+
}
83+
}
84+
85+
private GetAllTrips.Data getAllTripsFields(Response<GetAllTrips.Data> response) {
86+
if (response.hasErrors()) {
87+
throw new RuntimeException(Objects.requireNonNull(response.getErrors()).get(0).getMessage());
88+
}
89+
90+
return response.getData();
91+
}
92+
93+
private RecyclerView.Adapter buildRVAdapter(GetAllTrips.Data response) {
94+
return (new TripsAdapter(response.allTrips(), getActivity().getLayoutInflater(),
95+
android.text.format.DateFormat.getDateFormat(getActivity())));
96+
}
97+
}

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

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
6-
android:orientation="vertical"
6+
android:layout_margin="8dp"
77
android:background="?attr/selectableItemBackground"
8-
android:padding="12dp">
8+
android:elevation="8dp"
9+
android:orientation="vertical"
10+
android:padding="4dp"
11+
app:cardCornerRadius="5dp">
912

10-
<TextView
11-
android:id="@+id/title"
12-
android:layout_width="wrap_content"
13+
<LinearLayout
14+
android:layout_width="match_parent"
1315
android:layout_height="wrap_content"
14-
android:layout_marginStart="8dp"
15-
android:layout_marginEnd="8dp"
16-
android:text="@string/app_name"
17-
android:textColor="@color/material_on_surface_emphasis_high_type"
18-
android:textSize="18sp" />
16+
android:orientation="vertical">
1917

20-
<TextView
21-
android:id="@+id/start_time"
22-
android:layout_width="wrap_content"
23-
android:layout_height="wrap_content"
24-
android:layout_marginStart="8dp"
25-
android:layout_marginTop="8dp"
26-
android:layout_marginEnd="8dp"
27-
android:text="@string/app_name"
28-
android:textColor="@color/material_on_surface_emphasis_medium"
29-
android:textSize="14sp" />
18+
<TextView
19+
android:id="@+id/start_time"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_marginStart="8dp"
23+
android:layout_marginTop="8dp"
24+
android:layout_marginEnd="8dp"
25+
android:text="@string/app_name"
26+
android:textColor="@color/material_on_surface_emphasis_medium"
27+
android:textSize="14sp" />
3028

31-
<TextView
32-
android:id="@+id/intro"
33-
android:layout_width="wrap_content"
34-
android:layout_height="wrap_content"
35-
android:layout_gravity="center|center_horizontal"
36-
android:text="@string/kotlin_and_graphql"
37-
android:textColor="@color/design_default_color_on_secondary"
38-
android:textSize="12sp" />
39-
</LinearLayout>
29+
<TextView
30+
android:id="@+id/title"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_marginStart="8dp"
34+
android:layout_marginTop="8dp"
35+
android:layout_marginEnd="8dp"
36+
android:text="@string/app_name"
37+
android:textColor="@color/material_on_surface_emphasis_high_type"
38+
android:textSize="18sp" />
39+
40+
<TextView
41+
android:id="@+id/intro"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:layout_gravity="center|center_horizontal"
45+
android:layout_marginTop="16dp"
46+
android:text="@string/kotlin_and_graphql"
47+
android:textColor="@color/design_default_color_on_secondary"
48+
android:textSize="12sp" />
49+
</LinearLayout>
50+
51+
</androidx.cardview.widget.CardView>

0 commit comments

Comments
 (0)