Skip to content

Commit

Permalink
Added first version of app
Browse files Browse the repository at this point in the history
  • Loading branch information
Lezhni committed Dec 11, 2022
1 parent 3884488 commit 35d924e
Show file tree
Hide file tree
Showing 40 changed files with 985 additions and 0 deletions.
Empty file removed README.md
Empty file.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
45 changes: 45 additions & 0 deletions app/build.gradle
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()
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
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
22 changes: 22 additions & 0 deletions app/src/main/AndroidManifest.xml
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>
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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);
}
}
}
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();
}
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);
}
});
}
}
Loading

0 comments on commit 35d924e

Please sign in to comment.