-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
985 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
|
||
defaultConfig { | ||
applicationId "by.wotcalculator.application" | ||
minSdkVersion 21 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
|
||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0' | ||
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0' | ||
//implementation 'com.google.dagger:dagger:2.27' | ||
//implementation 'com.google.dagger:dagger-android-support:2.27' | ||
implementation 'com.squareup.retrofit2:retrofit:2.7.2' | ||
implementation 'com.squareup.retrofit2:converter-gson:2.4.0' | ||
annotationProcessor 'androidx.lifecycle:lifecycle-common-java8:2.2.0' | ||
//annotationProcessor 'com.google.dagger:dagger-compiler:2.27' | ||
//annotationProcessor 'com.google.dagger:dagger-android-processor:2.27' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="by.wotcalculator.application"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".ui.activities.MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
55 changes: 55 additions & 0 deletions
55
app/src/main/java/by/wotcalculator/application/data/repositories/TankopediaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package by.wotcalculator.application.data.repositories; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.MutableLiveData; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import by.wotcalculator.application.domain.models.api.ResponseBody; | ||
import by.wotcalculator.application.domain.models.api.Tank; | ||
import by.wotcalculator.application.retrofit.RetrofitService; | ||
import by.wotcalculator.application.retrofit.TankopediaAPI; | ||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
|
||
public class TankopediaRepository { | ||
|
||
// TODO : Inject with Dagger | ||
private final TankopediaAPI tankopediaAPI; | ||
|
||
private final MutableLiveData<ArrayList<Tank>> tanksList = new MutableLiveData<>(); | ||
|
||
public TankopediaRepository() { | ||
tankopediaAPI = RetrofitService.getTankopediaApi(); | ||
} | ||
|
||
public MutableLiveData<ArrayList<Tank>> getTanksList() { | ||
Call<ResponseBody> request = tankopediaAPI.getAllTanks(); | ||
request.enqueue(new Callback<ResponseBody>() { | ||
|
||
@Override | ||
public void onResponse( | ||
@NonNull Call<ResponseBody> call, | ||
@NonNull Response<ResponseBody> response | ||
) { | ||
if (response.isSuccessful()) { | ||
ResponseBody responseBody = response.body(); | ||
if (responseBody != null) { | ||
HashMap<Integer, Tank> tanksHashMap = responseBody.getData(); | ||
ArrayList<Tank> tanks = new ArrayList<>(tanksHashMap.values()); | ||
tanksList.postValue(tanks); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<ResponseBody> call, Throwable t) { | ||
tanksList.postValue(null); | ||
} | ||
}); | ||
|
||
return this.tanksList; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/by/wotcalculator/application/data/viewmodels/TanksViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package by.wotcalculator.application.data.viewmodels; | ||
|
||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
import java.util.ArrayList; | ||
|
||
import by.wotcalculator.application.data.repositories.TankopediaRepository; | ||
import by.wotcalculator.application.domain.models.api.Tank; | ||
|
||
public class TanksViewModel extends ViewModel { | ||
|
||
// TODO : Inject with Dagger | ||
private TankopediaRepository tankopediaRepository; | ||
|
||
private MutableLiveData<ArrayList<Tank>> tanksList; | ||
|
||
public TanksViewModel() { | ||
tankopediaRepository = new TankopediaRepository(); | ||
tanksList = tankopediaRepository.getTanksList(); | ||
} | ||
|
||
public LiveData<ArrayList<Tank>> getLiveData() { | ||
return tanksList; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/by/wotcalculator/application/domain/models/Tank.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package by.wotcalculator.application.domain.models.api; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Tank { | ||
|
||
@SerializedName("name") | ||
private String name; | ||
|
||
@SerializedName("description") | ||
private String description; | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
app/src/main/java/by/wotcalculator/application/domain/models/api/Meta.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package by.wotcalculator.application.domain.models.api; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Meta { | ||
|
||
@SerializedName("count") | ||
private int count; | ||
|
||
@SerializedName("page_total") | ||
private int totalPages; | ||
|
||
@SerializedName("total") | ||
private int total; | ||
|
||
@SerializedName("limit") | ||
private int limit; | ||
|
||
@SerializedName("page") | ||
private int page; | ||
|
||
public int getCount() { | ||
return this.count; | ||
} | ||
|
||
public int getTotalPages() { | ||
return this.totalPages; | ||
} | ||
|
||
public int getTotal() { | ||
return this.total; | ||
} | ||
|
||
public int getLimit() { | ||
return this.limit; | ||
} | ||
|
||
public int getPage() { | ||
return this.page; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/by/wotcalculator/application/domain/models/api/ResponseBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package by.wotcalculator.application.domain.models.api; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import java.util.HashMap; | ||
|
||
public class ResponseBody { | ||
|
||
@SerializedName("status") | ||
private String status; | ||
|
||
@SerializedName("meta") | ||
private Meta meta; | ||
|
||
@SerializedName("data") | ||
private HashMap<Integer, Tank> data; | ||
|
||
public String getStatus() { | ||
return this.status; | ||
} | ||
|
||
public Meta getMeta() { | ||
return this.meta; | ||
} | ||
|
||
public HashMap<Integer, Tank> getData() { | ||
return this.data; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/by/wotcalculator/application/retrofit/RetrofitService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package by.wotcalculator.application.retrofit; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.HttpUrl; | ||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class RetrofitService { | ||
|
||
private static final String BASE_URL = "http://api.worldoftanks.ru/wot/"; | ||
|
||
private static final String API_APP_ID = "0b0439f85486bd0f964f5af92eb7b3af"; | ||
|
||
public static TankopediaAPI getTankopediaApi() { | ||
return getInstance().create(TankopediaAPI.class); | ||
} | ||
|
||
private static Retrofit getInstance() { | ||
OkHttpClient httpClient = new OkHttpClient.Builder() | ||
.addInterceptor(new ImplementAppIdInterceptor()) | ||
.build(); | ||
return new Retrofit.Builder() | ||
.client(httpClient) | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build(); | ||
} | ||
|
||
private static class ImplementAppIdInterceptor implements Interceptor { | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request original = chain.request(); | ||
HttpUrl originalHttpUrl = original.url(); | ||
HttpUrl url = originalHttpUrl | ||
.newBuilder() | ||
.addQueryParameter("application_id", API_APP_ID) | ||
.build(); | ||
Request.Builder requestBuilder = original | ||
.newBuilder() | ||
.url(url); | ||
Request request = requestBuilder.build(); | ||
return chain.proceed(request); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/by/wotcalculator/application/retrofit/TankopediaAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package by.wotcalculator.application.retrofit; | ||
|
||
import by.wotcalculator.application.domain.models.api.ResponseBody; | ||
import retrofit2.Call; | ||
import retrofit2.http.GET; | ||
|
||
public interface TankopediaAPI { | ||
@GET("encyclopedia/vehicles/?page_no=1&limit=10&fields=name,description") | ||
Call<ResponseBody> getAllTanks(); | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/by/wotcalculator/application/ui/activities/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package by.wotcalculator.application.ui.activities; | ||
|
||
import android.os.Bundle; | ||
import android.widget.ListView; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.lifecycle.ViewModelProvider; | ||
|
||
import by.wotcalculator.application.R; | ||
import by.wotcalculator.application.data.viewmodels.TanksViewModel; | ||
import by.wotcalculator.application.ui.adapters.TanksListAdapter; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
protected ListView tanksListView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
tanksListView = findViewById(R.id.tanks_list); | ||
|
||
TanksViewModel tanksViewModel = new ViewModelProvider(this).get(TanksViewModel.class); | ||
tanksViewModel.getLiveData().observe(this, tanks -> { | ||
if (tanks != null) { | ||
TanksListAdapter adapter = new TanksListAdapter(this, R.layout.tanks_list_item, tanks); | ||
tanksListView.setAdapter(adapter); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.