diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..aa724b77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..0414f9d0 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +star-wars-wiki \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..61a9130c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..5cd135a0 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..0380d8d3 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..d5d35ec4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 00000000..797acea5 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index c0193b60..d56784ab 100644 --- a/README.md +++ b/README.md @@ -12,51 +12,51 @@ http://docs.starwarsfavorites.apiary.io/# ### Lista de Personagens -Para obter os personagens, sua aplicação deverá utilizar o recurso `people` da Swapi (documentação disponível no topo do documento). A aplicação deve exibir todos os 87 personagens e permitir pesquisar o personagem pelo nome. Sugerimos exibir as primeiras páginas enquanto carrega as outras, em um formato de scroll infinito. +Para obter os personagens, sua aplicação deverá utilizar o recurso `people` da Swapi (documentação disponível no topo do documento). A aplicação deve exibir todos os 87 personagens✔️ e permitir pesquisar o personagem pelo nome✔️. Sugerimos exibir as primeiras páginas enquanto carrega as outras, em um formato de scroll infinito✔️. -A lista de itens deve exibir as seguintes informações: +A lista de itens deve exibir as seguintes informações:✔️ + Nome [name] + Altura [height] + Genero [gender] + Peso [mass] -Os dados devem ser salvos em banco de dados local para acesso offline e atualizados sempre que a tela for aberta. +Os dados devem ser salvos em banco de dados local para acesso offline e atualizados sempre que a tela for aberta.✔️ ### Detalhes do Personagem Ao clicar em um item da lista o seu app deve mostrar as informações abaixo: -+ name -+ height -+ mass -+ hair_color -+ skin_color -+ eye_color -+ birth_year -+ gender -+ Nome do Planeta Natal ++ name✔️ ++ height✔️ ++ mass ✔️ ++ hair_color✔️ ++ skin_color✔️ ++ eye_color✔️ ++ birth_year✔️ ++ gender✔️ ++ Nome do Planeta Natal ✔️ + Nome da Espécie -A busca pelo nome do planeta e da espécie deve ser feita em paralelo. +A busca pelo nome do planeta✔️ e da espécie deve ser feita em paralelo. ### Favoritos -Na lista e nos detalhes deve ser possível adicionar e remover um personagem a sua lista de favoritos. Tambem deve ser possível filtrar quais personagens foram favoritados na lista principal. +Na lista e nos detalhes deve ser possível adicionar e remover um personagem a sua lista de favoritos✔️. Tambem deve ser possível filtrar quais personagens foram favoritados na lista principal✔️. ##### Adição e Remoção de Favoritos URL BASE: http://private-782d3-starwarsfavorites.apiary-mock.com/ -Ao adicionar um favorito a aplicação deve fazer um request para a api starwarsfavorites (documentação disponível no topo do documento). +Ao adicionar um favorito a aplicação deve fazer um request para a starWars starwarsfavorites✔️ (documentação disponível no topo do documento). A aplicação deve: -+ Exibir a mensagem de retorno da API em caso de sucesso ou erro. ++ Exibir a mensagem de retorno da API em caso de sucesso ou erro.✔️ + Reenviar a requisição da próxima vez que o app for aberto em caso de erro. -+ Salvar no banco de dados local quais personagens foram favoritados. -+ Tratar a remoção de favoritos apenas no banco de dados local. ++ Salvar no banco de dados local quais personagens foram favoritados.✔️ ++ Tratar a remoção de favoritos apenas no banco de dados local.✔️ -Em metade das requisições enviadas para a api starwarsfavorites a aplicação deve adicionar o header `Prefer` com o valor `status=400`. +Em metade das requisições enviadas para a starWars starwarsfavorites a aplicação deve adicionar o header `Prefer` com o valor `status=400`. -P.S.: O candidato deve escolher o ID. +P.S.: O candidato deve escolher o ID.✔️ --- #### LICENSE diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 00000000..bfcbba10 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.3" + + defaultConfig { + applicationId "br.com.star_wars_wiki" + minSdkVersion 16 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + 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 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.room:room-runtime:2.2.5' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + annotationProcessor 'androidx.room:room-compiler:2.2.5' + + implementation 'com.squareup.retrofit:retrofit:1.8.0' + implementation 'com.squareup.okhttp:okhttp:2.1.0' + implementation 'com.squareup.okhttp:okhttp-urlconnection:2.1.0' + + //Dependência para material search view + implementation 'com.miguelcatalan:materialsearchview:1.4.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/app/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/app/src/androidTest/java/br/com/star_wars_wiki/ExampleInstrumentedTest.java b/app/src/androidTest/java/br/com/star_wars_wiki/ExampleInstrumentedTest.java new file mode 100644 index 00000000..c229f711 --- /dev/null +++ b/app/src/androidTest/java/br/com/star_wars_wiki/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package br.com.star_wars_wiki; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("br.com.star_wars_wiki", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..981baba3 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/ic_chew-playstore.png b/app/src/main/ic_chew-playstore.png new file mode 100644 index 00000000..e5b7e2df Binary files /dev/null and b/app/src/main/ic_chew-playstore.png differ diff --git a/app/src/main/ic_darth-playstore.png b/app/src/main/ic_darth-playstore.png new file mode 100644 index 00000000..040cb2c5 Binary files /dev/null and b/app/src/main/ic_darth-playstore.png differ diff --git a/app/src/main/ic_favoritos-playstore.png b/app/src/main/ic_favoritos-playstore.png new file mode 100644 index 00000000..24cfdafa Binary files /dev/null and b/app/src/main/ic_favoritos-playstore.png differ diff --git a/app/src/main/ic_personagens-playstore.png b/app/src/main/ic_personagens-playstore.png new file mode 100644 index 00000000..923e8ad5 Binary files /dev/null and b/app/src/main/ic_personagens-playstore.png differ diff --git a/app/src/main/java/br/com/star_wars_wiki/ActFavorito.java b/app/src/main/java/br/com/star_wars_wiki/ActFavorito.java new file mode 100644 index 00000000..39b2eead --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/ActFavorito.java @@ -0,0 +1,64 @@ +package br.com.star_wars_wiki; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.Observer; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.os.Bundle; +import android.view.View; +import android.widget.ProgressBar; + +import java.util.List; + +import br.com.star_wars_wiki.adapter.FavoriteAdapter; +import br.com.star_wars_wiki.entity.Favorite; +import br.com.star_wars_wiki.view_model.FavoriteViewModel; + +public class ActFavorito extends AppCompatActivity { + + private ProgressBar progressBarFavorito; + private RecyclerView recyclerFavoritos; + private FavoriteViewModel favoriteViewModel; + private FavoriteAdapter favoriteAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.act_favoritos); + + Toolbar toolbar = findViewById(R.id.toolbar_simples); + toolbar.setTitle(R.string.act_favoritos); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + + progressBarFavorito = findViewById(R.id.progress_bar_favorite); + recyclerFavoritos = findViewById(R.id.recycler_favoritos); + + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); + recyclerFavoritos.setLayoutManager(layoutManager); + + recyclerFavoritos.setVisibility(View.GONE); + + favoriteViewModel = new FavoriteViewModel(getApplication()); + favoriteViewModel.getAllFavorites().observe(this, new Observer>() { + @Override + public void onChanged(List favorites) { + progressBarFavorito.setVisibility(View.GONE); + recyclerFavoritos.setVisibility(View.VISIBLE); + + favoriteAdapter = new FavoriteAdapter(favorites); + recyclerFavoritos.setAdapter(favoriteAdapter); + } + }); + + } + + @Override + public boolean onSupportNavigateUp() { + onBackPressed(); + return true; + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/ActHome.java b/app/src/main/java/br/com/star_wars_wiki/ActHome.java new file mode 100644 index 00000000..90691903 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/ActHome.java @@ -0,0 +1,43 @@ +package br.com.star_wars_wiki; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.ImageButton; + +import br.com.star_wars_wiki.network.StarWarsApi; + +public class ActHome extends AppCompatActivity { + + private ImageButton btnPersonagens; + private ImageButton btnFavoritos; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.act_main); + + //Configuração da Toolbar + Toolbar toolbar = findViewById(R.id.toolbar_simples); + toolbar.setTitle(R.string.act_home); + setSupportActionBar(toolbar); + + StarWarsApi.init(); + + btnPersonagens = findViewById(R.id.btnPersonagens); + btnFavoritos = findViewById(R.id.btnFavoritos); + } + + public void irTelaPersonagem(View view){ + Intent it = new Intent(this, ActListaPersonagem.class); + startActivity(it); + } + + public void itTelaFavorito(View view){ + Intent it = new Intent(this, ActFavorito.class); + startActivity(it); + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/ActListaPersonagem.java b/app/src/main/java/br/com/star_wars_wiki/ActListaPersonagem.java new file mode 100644 index 00000000..72e39514 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/ActListaPersonagem.java @@ -0,0 +1,192 @@ +package br.com.star_wars_wiki; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.content.Intent; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ProgressBar; + +import com.miguelcatalan.materialsearchview.MaterialSearchView; + +import java.util.ArrayList; +import java.util.List; + +import br.com.star_wars_wiki.adapter.PeopleAdapter; +import br.com.star_wars_wiki.database.converters.Converters; +import br.com.star_wars_wiki.entity.Favorite; +import br.com.star_wars_wiki.entity.People; +import br.com.star_wars_wiki.entity.SWModelList; +import br.com.star_wars_wiki.network.StarWarsApi; +import br.com.star_wars_wiki.utils.RecyclerItemClickListener; +import br.com.star_wars_wiki.view_model.FavoriteViewModel; +import br.com.star_wars_wiki.view_model.PeopleViewModel; +import retrofit.Callback; +import retrofit.RetrofitError; +import retrofit.client.Response; + +public class ActListaPersonagem extends AppCompatActivity { + + private MaterialSearchView searchView; + private ProgressBar progressBar; + private RecyclerView recyclerPeopleList; + private PeopleViewModel peopleViewModel; + private int page = 0; + private boolean next = true; + private int nextPage = 1; + private List peopleList = new ArrayList<>(); + private PeopleAdapter peopleAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.act_lista_personagens); + + peopleAdapter = new PeopleAdapter(peopleList, getBaseContext(), getApplication()); + + Toolbar toolbar = findViewById(R.id.toolbar_people); + toolbar.setTitle(R.string.act_personagens); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + + StarWarsApi.init(); + + progressBar = findViewById(R.id.progress_bar); + recyclerPeopleList = findViewById(R.id.lista_people); + searchView = findViewById(R.id.svApontamento); + + //Inicialização do ViewModel + peopleViewModel = new ViewModelProvider(this).get(PeopleViewModel.class); + peopleViewModel.getAllPeople().observe(this, new Observer>() { + @Override + public void onChanged(List peopleListRecebido) { + peopleList = peopleListRecebido; + peopleAdapter.setPeopleList(peopleList); + recyclerPeopleList.setAdapter(peopleAdapter); + if(peopleList.size() > 0){ + nextPage = peopleList.get(peopleList.size() - 1).getNextPage(); + } + if(peopleList.size() == 0){ + getAllPeople(); + } + } + }); + + //Configuração do RecyclerView + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); + recyclerPeopleList.setLayoutManager(layoutManager); + + //Listener do RecyclerView para detectar o fim da rolagem + recyclerPeopleList.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + if(!recyclerPeopleList.canScrollVertically(1)){ + getAllPeople(); + } + } + }); + + //Listener para o search view + searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() { + @Override + public void onSearchViewShown() { + + } + + @Override + public void onSearchViewClosed() { + peopleAdapter.setPeopleList(peopleList); + recyclerPeopleList.setAdapter(peopleAdapter); + } + }); + + searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + if( newText != null && !newText.isEmpty() ){ + searchPeople(newText.toLowerCase()); + } + return true; + } + }); + } + + @Override + public boolean onSupportNavigateUp() { + onBackPressed(); + return true; + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu, menu); + + //Configura botão de pesquisa + MenuItem item = menu.findItem(R.id.action_search); + searchView.setMenuItem(item); + + return super.onCreateOptionsMenu(menu); + } + + public void getAllPeople(){ + if(next && (progressBar.getVisibility() == View.GONE)){ + progressBar.setVisibility(View.VISIBLE); + StarWarsApi.getApi().getAllPeople(nextPage, new Callback>() { + @Override + public void success(SWModelList peopleSWModelList, Response response) { + peopleList = new ArrayList<>(); + peopleList.addAll(peopleSWModelList.results); + for(int i = 0; i < peopleList.size(); i++){//Fiz um mecanismo para capturar a página dos resultados, isso se fez necessário, pois quando a busca não é realizada completamente atrvés do scroll listener, existe um problema quando o usuário volta a tela inicial e começa a requisitar novos resultados + if(peopleSWModelList.next != null){ + String aux = peopleSWModelList.next; + peopleList.get(i).setNextPage(Integer.parseInt(String.valueOf(peopleSWModelList.next.charAt(aux.length() - 1)))); + } + } + peopleViewModel.insert(peopleList); + next = peopleSWModelList.hasMore(); + + progressBar.setVisibility(View.GONE); + } + + @Override + public void failure(RetrofitError error) { + + } + }); + } + + } + + public void searchPeople(String texto){ + List listaPeoplePesquisados = new ArrayList<>(); + + for ( int i=0; i< peopleList.size(); i++){ + People people = peopleList.get(i); + String nome = people.getName().toLowerCase(); + if( nome.contains( texto )){ + listaPeoplePesquisados.add(people); + } + } + + peopleAdapter = new PeopleAdapter(listaPeoplePesquisados, getBaseContext(), getApplication()); + recyclerPeopleList.setAdapter(peopleAdapter); + peopleAdapter.notifyDataSetChanged(); + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/ActPersonagem.java b/app/src/main/java/br/com/star_wars_wiki/ActPersonagem.java new file mode 100644 index 00000000..0f03973f --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/ActPersonagem.java @@ -0,0 +1,193 @@ +package br.com.star_wars_wiki; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.Observer; + +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import java.util.ArrayList; +import java.util.List; + +import br.com.star_wars_wiki.database.converters.Converters; +import br.com.star_wars_wiki.entity.People; +import br.com.star_wars_wiki.entity.Planet; +import br.com.star_wars_wiki.entity.ResponseFavorite; +import br.com.star_wars_wiki.entity.Specie; +import br.com.star_wars_wiki.network.StarWarsApi; +import br.com.star_wars_wiki.network.StarWarsFavoriteApi; +import br.com.star_wars_wiki.utils.APIConstants; +import br.com.star_wars_wiki.view_model.FavoriteViewModel; +import br.com.star_wars_wiki.view_model.PlanetViewModel; +import br.com.star_wars_wiki.view_model.SpecieViewModel; +import retrofit.Callback; +import retrofit.RetrofitError; +import retrofit.client.Response; + +public class ActPersonagem extends AppCompatActivity { + + private ProgressBar progressBarActPersonagem; + private Button btnAdicionaFavorito, btnRemoveFavorito; + private TextView txtName, txtHeight, txtMass, txtHairColor, txtSkinColor, txtEyeColor, txtBirthYear, txtGender, txtPlanet, txtSpecie; + private People people; + private Planet planetPeople; + private ArrayList species = new ArrayList<>(); + private PlanetViewModel planetViewModel; + private SpecieViewModel specieViewModel; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.act_personagem); + + Toolbar toolbar = findViewById(R.id.toolbar_simples); + toolbar.setTitle(R.string.act_info_personagens); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + + if(StarWarsApi.getApi() == null){ + StarWarsApi.init(); + } + + people = (People) getIntent().getSerializableExtra("people"); + + progressBarActPersonagem = findViewById(R.id.progress_bar_act_personagem); + btnAdicionaFavorito = findViewById(R.id.btn_add_favorito_info); + btnRemoveFavorito = findViewById(R.id.btn_remove_favorito_info); + txtName = findViewById(R.id.txt_name); + txtHeight = findViewById(R.id.txt_height); + txtMass = findViewById(R.id.txt_mass); + txtHairColor = findViewById(R.id.txt_hair_color); + txtSkinColor = findViewById(R.id.txt_skin_color); + txtEyeColor = findViewById(R.id.txt_eye_color); + txtBirthYear = findViewById(R.id.txt_birth_year); + txtGender = findViewById(R.id.txt_gender); + txtPlanet = findViewById(R.id.txt_planet); + //txtSpecie = findViewById(R.id.txt_specie); + + getPlanetAndSpecie(); + } + + @Override + public boolean onSupportNavigateUp() { + onBackPressed(); + return true; + } + + public void getPlanetAndSpecie(){ + progressBarActPersonagem.setVisibility(View.VISIBLE); + + String routePlanet = people.getHomeWorldUrl().replaceAll(APIConstants.BASE_URL, "");//Tira a parte da URL base +// ArrayList routeSpecieAux = people.getSpeciesUrls(); +// ArrayList routeSpecie = new ArrayList<>(); +// for(int i = 0; i < routeSpecieAux.size(); i++){ +// routeSpecie.add(i, routeSpecieAux.get(i).replaceAll(APIConstants.BASE_URL, "")); +// } + + StarWarsApi.getApi().getPlanet(routePlanet, new Callback() { + @Override + public void success(Planet planet, Response response) { + planetPeople = planet; + planetViewModel = new PlanetViewModel(getApplication()); + planetViewModel.insert(planet); + preencheCampos(); + //Não consegui implementar a listagem das espécies de um personagem, mas deixei aqui o código + //para mostrar que tentei fazer, mas não obtive sucesso. + //As linhas comentadas são referentes a requisição de listagem de espécies +// if(routeSpecie.size() > 0){ +// for(int i = 0; i < routeSpecie.size(); i++){ +// int finalI = i; +// StarWarsApi.getApi().getSpecies(routeSpecie.get(i), new Callback() { +// @Override +// public void success(Specie specie, Response response) { +// specieViewModel = new SpecieViewModel(getApplication()); +// specieViewModel.insert(specie); +// species.add(specie); +// if(finalI == routeSpecie.size() - 1){//Desabilita a progress bar apenas na última requisição +// preencheCampos(); +// } +// } +// +// @Override +// public void failure(RetrofitError error) { +// +// } +// }); +// } +// } + } + + @Override + public void failure(RetrofitError error) { + + } + }); + } + + public void preencheCampos(){ + planetViewModel.getAllPlanets().observe(this, new Observer>() { + @Override + public void onChanged(List planets) { + Planet planet = planetViewModel.getPlanet(planetPeople.getName()); + + txtName.setText(people.getName()); + txtHeight.setText(people.getHeight()); + txtMass.setText(people.getMass()); + txtHairColor.setText(people.getHairColor()); + txtSkinColor.setText(people.getSkinColor()); + txtEyeColor.setText(people.getEyeColor()); + txtBirthYear.setText(people.getBirthYear()); + txtGender.setText(people.getGender()); + txtPlanet.setText(planet.getName()); + +// if(species.size() > 0){ +// String specieAux = ""; +// for(Specie specie : species){ +// specieAux = specieAux + specie.getName() + "; "; +// } +// txtSpecie.setText(specieAux); +// }else{ +// txtSpecie.setText("Não informada"); +// } + + progressBarActPersonagem.setVisibility(View.GONE); + btnAdicionaFavorito.setVisibility(View.VISIBLE); + btnRemoveFavorito.setVisibility(View.VISIBLE); + } + }); + } + + public void adicionarFavorito(View view){ + FavoriteViewModel favoriteViewModel = new FavoriteViewModel(getApplication()); + favoriteViewModel.insert(Converters.convertePeopleToFavorite(people)); + StarWarsFavoriteApi.init(); + StarWarsFavoriteApi.getApi().setFavorite(people.getName(), new Callback() { + @Override + public void success(ResponseFavorite responseFavorite, Response response) { + if(responseFavorite.getStatus() != null){ + Toast.makeText(getBaseContext(), responseFavorite.getStatus() + ". " + responseFavorite.getMessage(), Toast.LENGTH_SHORT).show(); + }else{ + if(responseFavorite.getError() != null){ + Toast.makeText(getBaseContext(), responseFavorite.getError() + ". " + responseFavorite.getErrorMessage(), Toast.LENGTH_SHORT).show(); + } + } + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(getBaseContext(), "internal error. Only at the end do you realize the power of the Dark Side.", Toast.LENGTH_SHORT).show(); + } + }); + } + + public void removeFavorito(View view){ + FavoriteViewModel favoriteViewModel = new FavoriteViewModel(getApplication()); + favoriteViewModel.remove(Converters.convertePeopleToFavorite(people), getBaseContext()); + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/adapter/FavoriteAdapter.java b/app/src/main/java/br/com/star_wars_wiki/adapter/FavoriteAdapter.java new file mode 100644 index 00000000..b5ee0073 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/adapter/FavoriteAdapter.java @@ -0,0 +1,72 @@ +package br.com.star_wars_wiki.adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.List; + +import br.com.star_wars_wiki.R; +import br.com.star_wars_wiki.entity.Favorite; + +public class FavoriteAdapter extends RecyclerView.Adapter { + + private List favoriteList; + + public FavoriteAdapter(List favoriteList){ + this.favoriteList = favoriteList; + } + + @NonNull + @Override + public FavoriteAdapter.FavoriteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View itemLista = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.adapter_personagem, parent, false); + + return new FavoriteAdapter.FavoriteHolder(itemLista); + } + + @Override + public void onBindViewHolder(@NonNull FavoriteAdapter.FavoriteHolder holder, int position) { + Favorite favorite = favoriteList.get(position); + + holder.txtNome.setText(favorite.getName()); + holder.txtAltura.setText(favorite.getHeight()); + holder.txtGenero.setText(favorite.getGender()); + holder.txtPeso.setText(favorite.getMass()); + holder.btnAddFavorito.setVisibility(View.GONE); + holder.btnVisualizar.setVisibility(View.GONE); + holder.btnRemoveFavorito.setVisibility(View.GONE); + } + + @Override + public int getItemCount() { + return favoriteList.size(); + } + + public class FavoriteHolder extends RecyclerView.ViewHolder{ + + TextView txtNome, txtAltura, txtGenero, txtPeso; + Button btnAddFavorito, btnVisualizar, btnRemoveFavorito; + + public FavoriteHolder(@NonNull View itemView) { + super(itemView); + txtNome = itemView.findViewById(R.id.txt_nome); + txtAltura = itemView.findViewById(R.id.txt_altura); + txtGenero = itemView.findViewById(R.id.txt_genero); + txtPeso = itemView.findViewById(R.id.txt_peso); + btnAddFavorito = itemView.findViewById(R.id.btn_add_favoritos); + btnVisualizar = itemView.findViewById(R.id.btn_visualizar); + btnRemoveFavorito = itemView.findViewById(R.id.btn_remove_favorito); + } + } + + public void setFavoriteList(List favoriteList){ + this.favoriteList = favoriteList; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/adapter/PeopleAdapter.java b/app/src/main/java/br/com/star_wars_wiki/adapter/PeopleAdapter.java new file mode 100644 index 00000000..615faa1d --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/adapter/PeopleAdapter.java @@ -0,0 +1,134 @@ +package br.com.star_wars_wiki.adapter; + +import android.app.Application; +import android.content.Context; +import android.content.Intent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.Observer; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.List; + +import br.com.star_wars_wiki.ActListaPersonagem; +import br.com.star_wars_wiki.ActPersonagem; +import br.com.star_wars_wiki.R; +import br.com.star_wars_wiki.database.converters.Converters; +import br.com.star_wars_wiki.entity.Favorite; +import br.com.star_wars_wiki.entity.People; +import br.com.star_wars_wiki.entity.ResponseFavorite; +import br.com.star_wars_wiki.network.StarWarsFavoriteApi; +import br.com.star_wars_wiki.view_model.FavoriteViewModel; +import retrofit.Callback; +import retrofit.RetrofitError; +import retrofit.client.Response; + +public class PeopleAdapter extends RecyclerView.Adapter{ + + private List peopleList; + private Context context; + private Application application; + private FavoriteViewModel favoriteViewModel; + + public PeopleAdapter(List lista, Context context, Application application){ + this.context = context; + this.peopleList = lista; + this.application = application; + } + + @NonNull + @Override + public PeopleAdapter.PersonagemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View itemLista = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.adapter_personagem, parent, false); + + return new PersonagemHolder(itemLista); + } + + @Override + public void onBindViewHolder(@NonNull PeopleAdapter.PersonagemHolder holder, int position) { + People people = peopleList.get(position); + + holder.txtNome.setText(people.getName()); + holder.txtAltura.setText(people.getHeight()); + holder.txtGenero.setText(people.getGender()); + holder.txtPeso.setText(people.getMass()); + + holder.btnAddFavorito.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + StarWarsFavoriteApi.init(); + StarWarsFavoriteApi.getApi().setFavorite(people.getName(), new Callback() { + @Override + public void success(ResponseFavorite responseFavorite, Response response) { + favoriteViewModel = new FavoriteViewModel(application); + favoriteViewModel.insert(Converters.convertePeopleToFavorite(people)); + if(responseFavorite.getStatus() != null){ + Toast.makeText(context, responseFavorite.getStatus() + ". " + responseFavorite.getMessage(), Toast.LENGTH_SHORT).show(); + }else{ + if(responseFavorite.getError() != null){ + Toast.makeText(context, responseFavorite.getError() + ". " + responseFavorite.getErrorMessage(), Toast.LENGTH_SHORT).show(); + } + } + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(context, "internal error. Only at the end do you realize the power of the Dark Side.", Toast.LENGTH_SHORT).show(); + } + }); + } + }); + + holder.btnVisualizar.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent it = new Intent(context, ActPersonagem.class); + it.putExtra("people", peopleList.get(position)); + it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(it); + } + }); + + holder.btnRemoveFavorito.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + favoriteViewModel = new FavoriteViewModel(application); + favoriteViewModel.remove(Converters.convertePeopleToFavorite(people), context); + } + }); + } + + @Override + public int getItemCount() { + return this.peopleList.size(); + } + + public class PersonagemHolder extends RecyclerView.ViewHolder{ + + TextView txtNome, txtAltura, txtGenero, txtPeso; + Button btnAddFavorito, btnVisualizar, btnRemoveFavorito; + + public PersonagemHolder(@NonNull View itemView) { + super(itemView); + txtNome = itemView.findViewById(R.id.txt_nome); + txtAltura = itemView.findViewById(R.id.txt_altura); + txtGenero = itemView.findViewById(R.id.txt_genero); + txtPeso = itemView.findViewById(R.id.txt_peso); + btnAddFavorito = itemView.findViewById(R.id.btn_add_favoritos); + btnVisualizar = itemView.findViewById(R.id.btn_visualizar); + btnRemoveFavorito = itemView.findViewById(R.id.btn_remove_favorito); + } + } + + public void setPeopleList(List peopleList) { + this.peopleList = peopleList; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/RoomDatabase.java b/app/src/main/java/br/com/star_wars_wiki/database/RoomDatabase.java new file mode 100644 index 00000000..79d4f0be --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/RoomDatabase.java @@ -0,0 +1,31 @@ +package br.com.star_wars_wiki.database; + +import android.content.Context; + +import androidx.room.Database; +import androidx.room.Room; +import androidx.room.TypeConverters; + +import br.com.star_wars_wiki.database.converters.Converters; +import br.com.star_wars_wiki.entity.Favorite; +import br.com.star_wars_wiki.entity.People; +import br.com.star_wars_wiki.database.dao.*; +import br.com.star_wars_wiki.entity.Planet; +import br.com.star_wars_wiki.entity.Specie; + +@Database(entities = {People.class, Planet.class, Specie.class, Favorite.class}, version = 1) +@TypeConverters({Converters.class}) +public abstract class RoomDatabase extends androidx.room.RoomDatabase { + private static final String DATABASE_NAME="CVDatabase"; + private static RoomDatabase INSTANCE; + + public abstract PeopleDAO peopleDao(); + public abstract PlanetDAO planetDAO(); + public abstract SpeciesDAO speciesDAO(); + public abstract FavoriteDAO favoritesDAO(); + + public static RoomDatabase getInstance(Context context){ + INSTANCE = Room.databaseBuilder(context, RoomDatabase.class, DATABASE_NAME).build(); + return INSTANCE; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/converters/Converters.java b/app/src/main/java/br/com/star_wars_wiki/database/converters/Converters.java new file mode 100644 index 00000000..767602f2 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/converters/Converters.java @@ -0,0 +1,51 @@ +package br.com.star_wars_wiki.database.converters; + +import androidx.room.TypeConverter; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.ArrayList; + +import br.com.star_wars_wiki.entity.Favorite; +import br.com.star_wars_wiki.entity.People; + +public class Converters { + @TypeConverter + public static ArrayList fromString(String value) { + Type listType = new TypeToken>() {}.getType(); + return new Gson().fromJson(value, listType); + } + + @TypeConverter + public static String fromArrayList(ArrayList list) { + Gson gson = new Gson(); + String json = gson.toJson(list); + return json; + } + + public static Favorite convertePeopleToFavorite(People people){ + Favorite favorite = new Favorite(); + + favorite.setName(people.getName()); + favorite.setBirthYear(people.getBirthYear()); + favorite.setGender(people.getGender()); + favorite.setHairColor(people.getHairColor()); + favorite.setHeight(people.getHeight()); + favorite.setHomeWorldUrl(people.getHomeWorldUrl()); + favorite.setMass(people.getMass()); + favorite.setEyeColor(people.getEyeColor()); + favorite.setSkinColor(people.getSkinColor()); + favorite.setCreated(people.getCreated()); + favorite.setEdited(people.getEdited()); + favorite.setUrl(people.getUrl()); + favorite.setFilmsUrls(people.getFilmsUrls()); + favorite.setSpeciesUrls(people.getSpeciesUrls()); + favorite.setStarshipsUrls(people.getStarshipsUrls()); + favorite.setVehiclesUrls(people.getVehiclesUrls()); + favorite.setNextPage(people.getNextPage()); + + return favorite; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/dao/FavoriteDAO.java b/app/src/main/java/br/com/star_wars_wiki/database/dao/FavoriteDAO.java new file mode 100644 index 00000000..42e4b099 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/dao/FavoriteDAO.java @@ -0,0 +1,27 @@ +package br.com.star_wars_wiki.database.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +import br.com.star_wars_wiki.entity.Favorite; + +@Dao +public interface FavoriteDAO { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertFavorite(Favorite favorite); + + @Delete + void removeFavorite(Favorite favorite); + + @Query("SELECT * FROM Favorite WHERE name = :favoriteName") + Favorite getFavorite(String favoriteName); + + @Query("SELECT * FROM Favorite") + LiveData> getAllFavorites(); +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/dao/PeopleDAO.java b/app/src/main/java/br/com/star_wars_wiki/database/dao/PeopleDAO.java new file mode 100644 index 00000000..76c09782 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/dao/PeopleDAO.java @@ -0,0 +1,20 @@ +package br.com.star_wars_wiki.database.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +import br.com.star_wars_wiki.entity.People; + +@Dao +public interface PeopleDAO { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAllPeople(List list); + + @Query("SELECT * FROM People") + LiveData> getAllPeople(); +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/dao/PlanetDAO.java b/app/src/main/java/br/com/star_wars_wiki/database/dao/PlanetDAO.java new file mode 100644 index 00000000..6e730ac8 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/dao/PlanetDAO.java @@ -0,0 +1,20 @@ +package br.com.star_wars_wiki.database.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +import br.com.star_wars_wiki.entity.Planet; + +@Dao +public interface PlanetDAO { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(Planet planet); + + @Query("SELECT * FROM Planet") + LiveData> getAllPlanets(); +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/dao/SpeciesDAO.java b/app/src/main/java/br/com/star_wars_wiki/database/dao/SpeciesDAO.java new file mode 100644 index 00000000..0a43a612 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/dao/SpeciesDAO.java @@ -0,0 +1,20 @@ +package br.com.star_wars_wiki.database.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +import br.com.star_wars_wiki.entity.Specie; + +@Dao +public interface SpeciesDAO { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(Specie specie); + + @Query("SELECT * FROM Specie") + LiveData> getAllSpecies(); +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/repository/FavoriteRepo.java b/app/src/main/java/br/com/star_wars_wiki/database/repository/FavoriteRepo.java new file mode 100644 index 00000000..96be9058 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/repository/FavoriteRepo.java @@ -0,0 +1,81 @@ +package br.com.star_wars_wiki.database.repository; + +import android.app.Application; +import android.content.Context; +import android.os.AsyncTask; +import android.widget.Toast; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.RoomDatabase; +import br.com.star_wars_wiki.database.dao.FavoriteDAO; +import br.com.star_wars_wiki.entity.Favorite; + +public class FavoriteRepo { + private RoomDatabase database; + private LiveData> getAllFavorites; + + public FavoriteRepo(Application application){ + database = RoomDatabase.getInstance(application.getBaseContext()); + getAllFavorites = database.favoritesDAO().getAllFavorites(); + } + + public void insert(Favorite favorite){ + new InsertAsyncTask(database).execute(favorite); + } + + public LiveData> getAllFavorites(){ + return this.getAllFavorites; + } + + public void remove(Favorite favorite, Context context){ + new RemoveAsyncTask(database, context).execute(favorite); + } + + static class InsertAsyncTask extends AsyncTask{ + private FavoriteDAO favoriteDAO; + + InsertAsyncTask(RoomDatabase roomDatabase){ + favoriteDAO = roomDatabase.favoritesDAO(); + } + + @Override + protected Void doInBackground(Favorite... favorites) { + favoriteDAO.insertFavorite(favorites[0]); + return null; + } + } + + static class RemoveAsyncTask extends AsyncTask{ + private FavoriteDAO favoriteDAO; + private Context context; + private Favorite favorite; + + RemoveAsyncTask(RoomDatabase roomDatabase, Context context){ + favoriteDAO = roomDatabase.favoritesDAO(); + this.context = context; + } + + @Override + protected Void doInBackground(Favorite... favorites) { + favorite = favoriteDAO.getFavorite(favorites[0].getName()); + if(favorite != null){ + favoriteDAO.removeFavorite(favorites[0]); + } + return null; + } + + @Override + protected void onPostExecute(Void unused) { + super.onPostExecute(unused); + if(favorite != null){ + Toast.makeText(context, "Favorito removido com sucesso", Toast.LENGTH_SHORT).show(); + }else{ + Toast.makeText(context, "Desculpe, esse personagem não está na lista de favoritos", Toast.LENGTH_SHORT).show(); + } + + } + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/repository/PeopleRepo.java b/app/src/main/java/br/com/star_wars_wiki/database/repository/PeopleRepo.java new file mode 100644 index 00000000..859cb0c2 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/repository/PeopleRepo.java @@ -0,0 +1,45 @@ +package br.com.star_wars_wiki.database.repository; + +import android.app.Application; +import android.content.Context; +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.RoomDatabase; +import br.com.star_wars_wiki.database.dao.PeopleDAO; +import br.com.star_wars_wiki.entity.People; + +public class PeopleRepo { + private RoomDatabase database; + private LiveData> getAllPeople; + + public PeopleRepo(Application application){ + database = RoomDatabase.getInstance(application.getBaseContext()); + getAllPeople = database.peopleDao().getAllPeople(); + } + + public LiveData> getAllPeople(){ + return this.getAllPeople; + } + + public void insert(List peopleList){ + new InsertAsyncTask(database).execute(peopleList); + } + + static class InsertAsyncTask extends AsyncTask, Void, Void>{ + private PeopleDAO peopleDAO; + + InsertAsyncTask(RoomDatabase roomDatabase){ + peopleDAO = roomDatabase.peopleDao(); + } + + @Override + protected Void doInBackground(List... lists) { + peopleDAO.insertAllPeople(lists[0]); + return null; + } + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/repository/PlanetRepo.java b/app/src/main/java/br/com/star_wars_wiki/database/repository/PlanetRepo.java new file mode 100644 index 00000000..1d848b9a --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/repository/PlanetRepo.java @@ -0,0 +1,46 @@ +package br.com.star_wars_wiki.database.repository; + +import android.app.Application; +import android.app.AsyncNotedAppOp; +import android.os.AsyncTask; +import android.provider.ContactsContract; + +import androidx.lifecycle.LiveData; +import androidx.loader.content.AsyncTaskLoader; + +import java.util.List; + +import br.com.star_wars_wiki.database.RoomDatabase; +import br.com.star_wars_wiki.database.dao.PlanetDAO; +import br.com.star_wars_wiki.entity.Planet; + +public class PlanetRepo { + private RoomDatabase database; + private LiveData> getAllPlanets; + + public PlanetRepo(Application application){ + database = RoomDatabase.getInstance(application.getBaseContext()); + getAllPlanets = database.planetDAO().getAllPlanets(); + } + + public void insert(Planet planet){ + new InsertAsyncTask(database).execute(planet); + } + + static class InsertAsyncTask extends AsyncTask { + private PlanetDAO planetDAO; + + InsertAsyncTask(RoomDatabase roomDatabase){ + planetDAO = roomDatabase.planetDAO(); + } + @Override + protected Void doInBackground(Planet... planets) { + planetDAO.insert(planets[0]); + return null; + } + } + + public LiveData> getAllPlanets(){ + return this.getAllPlanets; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/database/repository/SpecieRepo.java b/app/src/main/java/br/com/star_wars_wiki/database/repository/SpecieRepo.java new file mode 100644 index 00000000..ca08e8f9 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/database/repository/SpecieRepo.java @@ -0,0 +1,46 @@ +package br.com.star_wars_wiki.database.repository; + +import android.app.Application; +import android.os.AsyncTask; +import android.widget.GridLayout; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.RoomDatabase; +import br.com.star_wars_wiki.database.dao.SpeciesDAO; +import br.com.star_wars_wiki.entity.Planet; +import br.com.star_wars_wiki.entity.Specie; + +public class SpecieRepo { + private RoomDatabase database; + private LiveData> getAllSpecies; + + public SpecieRepo(Application application){ + database = RoomDatabase.getInstance(application.getBaseContext()); + getAllSpecies = database.speciesDAO().getAllSpecies(); + } + + public void insert(Specie specie){ + new InsertAsyncTask(database).execute(specie); + } + + static class InsertAsyncTask extends AsyncTask { + private SpeciesDAO speciesDAO; + + InsertAsyncTask(RoomDatabase roomDatabase){ + speciesDAO = roomDatabase.speciesDAO(); + } + + @Override + protected Void doInBackground(Specie... specie) { + speciesDAO.insert(specie[0]); + return null; + } + } + + public LiveData> getAllSpecies(){ + return this.getAllSpecies; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Favorite.java b/app/src/main/java/br/com/star_wars_wiki/entity/Favorite.java new file mode 100644 index 00000000..00c92f58 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Favorite.java @@ -0,0 +1,212 @@ +package br.com.star_wars_wiki.entity; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; + +@Entity +public class Favorite { + //Não é uma boa prática colocar name como chave primária, mas para esse desafio acho aceitável + @PrimaryKey + @ColumnInfo(name = "name") + @NonNull + public String name; + + @ColumnInfo(name = "birth_year") + @SerializedName("birth_year") + public String birthYear; + + @ColumnInfo(name = "gender") + public String gender; + + @ColumnInfo(name = "hair_color") + @SerializedName("hair_color") + public String hairColor; + + @ColumnInfo(name = "height") + public String height; + + @ColumnInfo(name = "homeworld") + @SerializedName("homeworld") + public String homeWorldUrl; + + @ColumnInfo(name = "mass") + public String mass; + + @ColumnInfo(name = "eye_color") + @SerializedName("eye_color") + public String eyeColor; + + @ColumnInfo(name = "skin_color") + @SerializedName("skin_color") + public String skinColor; + + @ColumnInfo(name = "created") + public String created; + + @ColumnInfo(name = "edited") + public String edited; + + @ColumnInfo(name = "url") + public String url; + + @ColumnInfo(name = "films") + @SerializedName("films") + public ArrayList filmsUrls; + + @ColumnInfo(name = "species") + @SerializedName("species") + public ArrayList speciesUrls; + + @ColumnInfo(name = "starships") + @SerializedName("starships") + public ArrayList starshipsUrls; + + @ColumnInfo(name = "vehicles") + @SerializedName("vehicles") + public ArrayList vehiclesUrls; + + @ColumnInfo(name = "next_page") + int nextPage; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBirthYear() { + return birthYear; + } + + public void setBirthYear(String birthYear) { + this.birthYear = birthYear; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public String getHairColor() { + return hairColor; + } + + public void setHairColor(String hairColor) { + this.hairColor = hairColor; + } + + public String getHeight() { + return height; + } + + public void setHeight(String height) { + this.height = height; + } + + public String getHomeWorldUrl() { + return homeWorldUrl; + } + + public void setHomeWorldUrl(String homeWorldUrl) { + this.homeWorldUrl = homeWorldUrl; + } + + public String getMass() { + return mass; + } + + public void setMass(String mass) { + this.mass = mass; + } + + public String getSkinColor() { + return skinColor; + } + + public void setSkinColor(String skinColor) { + this.skinColor = skinColor; + } + + public String getCreated() { + return created; + } + + public void setCreated(String created) { + this.created = created; + } + + public String getEdited() { + return edited; + } + + public void setEdited(String edited) { + this.edited = edited; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public ArrayList getFilmsUrls() { + return filmsUrls; + } + + public void setFilmsUrls(ArrayList filmsUrls) { + this.filmsUrls = filmsUrls; + } + + public ArrayList getSpeciesUrls() { + return speciesUrls; + } + + public void setSpeciesUrls(ArrayList speciesUrls) { + this.speciesUrls = speciesUrls; + } + + public ArrayList getStarshipsUrls() { + return starshipsUrls; + } + + public void setStarshipsUrls(ArrayList starshipsUrls) { + this.starshipsUrls = starshipsUrls; + } + + public ArrayList getVehiclesUrls() { + return vehiclesUrls; + } + + public void setVehiclesUrls(ArrayList vehiclesUrls) { + this.vehiclesUrls = vehiclesUrls; + } + + public int getNextPage() { + return nextPage; + } + + public void setNextPage(int nextPage) { + this.nextPage = nextPage; + } + + public String getEyeColor() { + return eyeColor; + } + + public void setEyeColor(String eyeColor) { + this.eyeColor = eyeColor; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Film.java b/app/src/main/java/br/com/star_wars_wiki/entity/Film.java new file mode 100644 index 00000000..6505fb97 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Film.java @@ -0,0 +1,37 @@ +package br.com.star_wars_wiki.entity; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; +import java.util.ArrayList; + +public class Film implements Serializable { + public String title; + + @SerializedName("episode_id") + public int episodeId; + + @SerializedName("opening_crawl") + public String openingCrawl; + + public String director; + public String producer; + public String url; + public String created; + public String edited; + + @SerializedName("species") + public ArrayList speciesUrls; + + @SerializedName("starships") + public ArrayList starshipsUrls; + + @SerializedName("vehicles") + public ArrayList vehiclesUrls; + + @SerializedName("planets") + public ArrayList planetsUrls; + + @SerializedName("characters") + public ArrayList charactersUrls; +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/People.java b/app/src/main/java/br/com/star_wars_wiki/entity/People.java new file mode 100644 index 00000000..bc02fdd8 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/People.java @@ -0,0 +1,213 @@ +package br.com.star_wars_wiki.entity; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; +import java.util.ArrayList; + +@Entity +public class People implements Serializable { + //Não é uma boa prática colocar name como chave primária, mas para esse desafio acho aceitável + @PrimaryKey + @ColumnInfo(name = "name") + @NonNull + public String name; + + @ColumnInfo(name = "birth_year") + @SerializedName("birth_year") + public String birthYear; + + @ColumnInfo(name = "gender") + public String gender; + + @ColumnInfo(name = "hair_color") + @SerializedName("hair_color") + public String hairColor; + + @ColumnInfo(name = "height") + public String height; + + @ColumnInfo(name = "homeworld") + @SerializedName("homeworld") + public String homeWorldUrl; + + @ColumnInfo(name = "mass") + public String mass; + + @ColumnInfo(name = "eye_color") + @SerializedName("eye_color") + public String eyeColor; + + @ColumnInfo(name = "skin_color") + @SerializedName("skin_color") + public String skinColor; + + @ColumnInfo(name = "created") + public String created; + + @ColumnInfo(name = "edited") + public String edited; + + @ColumnInfo(name = "url") + public String url; + + @ColumnInfo(name = "films") + @SerializedName("films") + public ArrayList filmsUrls; + + @ColumnInfo(name = "species") + @SerializedName("species") + public ArrayList speciesUrls; + + @ColumnInfo(name = "starships") + @SerializedName("starships") + public ArrayList starshipsUrls; + + @ColumnInfo(name = "vehicles") + @SerializedName("vehicles") + public ArrayList vehiclesUrls; + + @ColumnInfo(name = "next_page") + int nextPage; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBirthYear() { + return birthYear; + } + + public void setBirthYear(String birthYear) { + this.birthYear = birthYear; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public String getHairColor() { + return hairColor; + } + + public void setHairColor(String hairColor) { + this.hairColor = hairColor; + } + + public String getHeight() { + return height; + } + + public void setHeight(String height) { + this.height = height; + } + + public String getHomeWorldUrl() { + return homeWorldUrl; + } + + public void setHomeWorldUrl(String homeWorldUrl) { + this.homeWorldUrl = homeWorldUrl; + } + + public String getMass() { + return mass; + } + + public void setMass(String mass) { + this.mass = mass; + } + + public String getSkinColor() { + return skinColor; + } + + public void setSkinColor(String skinColor) { + this.skinColor = skinColor; + } + + public String getCreated() { + return created; + } + + public void setCreated(String created) { + this.created = created; + } + + public String getEdited() { + return edited; + } + + public void setEdited(String edited) { + this.edited = edited; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public ArrayList getFilmsUrls() { + return filmsUrls; + } + + public void setFilmsUrls(ArrayList filmsUrls) { + this.filmsUrls = filmsUrls; + } + + public ArrayList getSpeciesUrls() { + return speciesUrls; + } + + public void setSpeciesUrls(ArrayList speciesUrls) { + this.speciesUrls = speciesUrls; + } + + public ArrayList getStarshipsUrls() { + return starshipsUrls; + } + + public void setStarshipsUrls(ArrayList starshipsUrls) { + this.starshipsUrls = starshipsUrls; + } + + public ArrayList getVehiclesUrls() { + return vehiclesUrls; + } + + public void setVehiclesUrls(ArrayList vehiclesUrls) { + this.vehiclesUrls = vehiclesUrls; + } + + public int getNextPage() { + return nextPage; + } + + public void setNextPage(int nextPage) { + this.nextPage = nextPage; + } + + public String getEyeColor() { + return eyeColor; + } + + public void setEyeColor(String eyeColor) { + this.eyeColor = eyeColor; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Planet.java b/app/src/main/java/br/com/star_wars_wiki/entity/Planet.java new file mode 100644 index 00000000..d62672bc --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Planet.java @@ -0,0 +1,153 @@ +package br.com.star_wars_wiki.entity; + +import androidx.annotation.NonNull; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; +import java.util.ArrayList; + +@Entity +public class Planet implements Serializable { + @PrimaryKey + @NonNull + public String name; + public String diameter; + public String gravity; + public String population; + public String climate; + public String terrain; + public String created; + public String edited; + public String url; + + @SerializedName("rotation_period") + public String rotationPeriod; + + @SerializedName("orbital_period") + public String orbitalPeriod; + + @SerializedName("surface_water") + public String surfaceWater; + + @SerializedName("residents") + public ArrayList residentsUrls; + + @SerializedName("films") + public ArrayList filmsUrls; + + @NonNull + public String getName() { + return name; + } + + public void setName(@NonNull String name) { + this.name = name; + } + + public String getDiameter() { + return diameter; + } + + public void setDiameter(String diameter) { + this.diameter = diameter; + } + + public String getGravity() { + return gravity; + } + + public void setGravity(String gravity) { + this.gravity = gravity; + } + + public String getPopulation() { + return population; + } + + public void setPopulation(String population) { + this.population = population; + } + + public String getClimate() { + return climate; + } + + public void setClimate(String climate) { + this.climate = climate; + } + + public String getTerrain() { + return terrain; + } + + public void setTerrain(String terrain) { + this.terrain = terrain; + } + + public String getCreated() { + return created; + } + + public void setCreated(String created) { + this.created = created; + } + + public String getEdited() { + return edited; + } + + public void setEdited(String edited) { + this.edited = edited; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getRotationPeriod() { + return rotationPeriod; + } + + public void setRotationPeriod(String rotationPeriod) { + this.rotationPeriod = rotationPeriod; + } + + public String getOrbitalPeriod() { + return orbitalPeriod; + } + + public void setOrbitalPeriod(String orbitalPeriod) { + this.orbitalPeriod = orbitalPeriod; + } + + public String getSurfaceWater() { + return surfaceWater; + } + + public void setSurfaceWater(String surfaceWater) { + this.surfaceWater = surfaceWater; + } + + public ArrayList getResidentsUrls() { + return residentsUrls; + } + + public void setResidentsUrls(ArrayList residentsUrls) { + this.residentsUrls = residentsUrls; + } + + public ArrayList getFilmsUrls() { + return filmsUrls; + } + + public void setFilmsUrls(ArrayList filmsUrls) { + this.filmsUrls = filmsUrls; + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/ResponseFavorite.java b/app/src/main/java/br/com/star_wars_wiki/entity/ResponseFavorite.java new file mode 100644 index 00000000..88150789 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/ResponseFavorite.java @@ -0,0 +1,43 @@ +package br.com.star_wars_wiki.entity; + +import com.google.gson.annotations.SerializedName; + +public class ResponseFavorite { + String status; + String message; + String error; + @SerializedName("error_message") + String errorMessage; + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Root.java b/app/src/main/java/br/com/star_wars_wiki/entity/Root.java new file mode 100644 index 00000000..b31b7037 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Root.java @@ -0,0 +1,20 @@ +package br.com.star_wars_wiki.entity; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Root implements Serializable { + @SerializedName("films") + public String filmsUrl; + @SerializedName("people") + public String peopleUrl; + @SerializedName("planets") + public String planetsUrl; + @SerializedName("species") + public String speciesUrl; + @SerializedName("starships") + public String starshipsUrl; + @SerializedName("vehicles") + public String vehiclesUrl; +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/SWModelList.java b/app/src/main/java/br/com/star_wars_wiki/entity/SWModelList.java new file mode 100644 index 00000000..53ab5f42 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/SWModelList.java @@ -0,0 +1,17 @@ +package br.com.star_wars_wiki.entity; + +import android.text.TextUtils; + +import java.io.Serializable; +import java.util.ArrayList; + +public class SWModelList implements Serializable { + public int count; + public String next; + public String previous; + public ArrayList results; + + public boolean hasMore() { + return !TextUtils.isEmpty(next); + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Specie.java b/app/src/main/java/br/com/star_wars_wiki/entity/Specie.java new file mode 100644 index 00000000..6acab4c6 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Specie.java @@ -0,0 +1,169 @@ +package br.com.star_wars_wiki.entity; + +import androidx.annotation.NonNull; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; +import java.util.ArrayList; + +@Entity +public class Specie implements Serializable { + @PrimaryKey + @NonNull + public String name; + public String classification; + public String designation; + + @SerializedName("average_height") + public String averageHeight; + + @SerializedName("average_lifespan") + public String averageLifespan; + + @SerializedName("eye_colors") + public String eyeColors; + + @SerializedName("hair_colors") + public String hairColors; + + @SerializedName("skin_colors") + public String skinColors; + + @SerializedName("homeworld") + public String homeWorld; + + public String language; + public String created; + public String edited; + public String url; + + @SerializedName("people") + public ArrayList peopleUrls; + + @SerializedName("films") + public ArrayList filmsUrls; + + @NonNull + public String getName() { + return name; + } + + public void setName(@NonNull String name) { + this.name = name; + } + + public String getClassification() { + return classification; + } + + public void setClassification(String classification) { + this.classification = classification; + } + + public String getDesignation() { + return designation; + } + + public void setDesignation(String designation) { + this.designation = designation; + } + + public String getAverageHeight() { + return averageHeight; + } + + public void setAverageHeight(String averageHeight) { + this.averageHeight = averageHeight; + } + + public String getAverageLifespan() { + return averageLifespan; + } + + public void setAverageLifespan(String averageLifespan) { + this.averageLifespan = averageLifespan; + } + + public String getEyeColors() { + return eyeColors; + } + + public void setEyeColors(String eyeColors) { + this.eyeColors = eyeColors; + } + + public String getHairColors() { + return hairColors; + } + + public void setHairColors(String hairColors) { + this.hairColors = hairColors; + } + + public String getSkinColors() { + return skinColors; + } + + public void setSkinColors(String skinColors) { + this.skinColors = skinColors; + } + + public String getHomeWorld() { + return homeWorld; + } + + public void setHomeWorld(String homeWorld) { + this.homeWorld = homeWorld; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getCreated() { + return created; + } + + public void setCreated(String created) { + this.created = created; + } + + public String getEdited() { + return edited; + } + + public void setEdited(String edited) { + this.edited = edited; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public ArrayList getPeopleUrls() { + return peopleUrls; + } + + public void setPeopleUrls(ArrayList peopleUrls) { + this.peopleUrls = peopleUrls; + } + + public ArrayList getFilmsUrls() { + return filmsUrls; + } + + public void setFilmsUrls(ArrayList filmsUrls) { + this.filmsUrls = filmsUrls; + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Starship.java b/app/src/main/java/br/com/star_wars_wiki/entity/Starship.java new file mode 100644 index 00000000..ddc3dfdb --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Starship.java @@ -0,0 +1,18 @@ +package br.com.star_wars_wiki.entity; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Starship extends Vehicle implements Serializable { + + @SerializedName("starship_class") + public String starshipClass; + + @SerializedName("hyperdrive_rating") + public String hyperdriveRating; + + @SerializedName("MGLT") + public String mglt; + +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Usuario.java b/app/src/main/java/br/com/star_wars_wiki/entity/Usuario.java new file mode 100644 index 00000000..be0c270e --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Usuario.java @@ -0,0 +1,7 @@ +package br.com.star_wars_wiki.entity; + +public class Usuario { + int id; + String name; + String login; +} diff --git a/app/src/main/java/br/com/star_wars_wiki/entity/Vehicle.java b/app/src/main/java/br/com/star_wars_wiki/entity/Vehicle.java new file mode 100644 index 00000000..bd3f4b83 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/entity/Vehicle.java @@ -0,0 +1,40 @@ +package br.com.star_wars_wiki.entity; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; +import java.util.ArrayList; + +public class Vehicle implements Serializable { + public String name; + public String model; + + @SerializedName("vehicle_class") + public String vehicleClass; + + public String manufacturer; + + @SerializedName("cost_in_credits") + public String costInCredits; + + public String length; + public String crew; + public String passengers; + + @SerializedName("max_atmosphering_speed") + public String maxAtmospheringSpeed; + + @SerializedName("cargo_capacity") + public String cargoCapacity; + + public String consumables; + public String created; + public String edited; + public String url; + + @SerializedName("pilots") + public ArrayList pilotsUrls; + + @SerializedName("films") + public ArrayList filmsUrls; +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/network/StarWars.java b/app/src/main/java/br/com/star_wars_wiki/network/StarWars.java new file mode 100644 index 00000000..045b9fbc --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/network/StarWars.java @@ -0,0 +1,66 @@ +package br.com.star_wars_wiki.network; + +import br.com.star_wars_wiki.entity.*; +import retrofit.Callback; +import retrofit.http.GET; +import retrofit.http.POST; +import retrofit.http.Path; +import retrofit.http.Query; + +public interface StarWars { + @GET("/") + public void getRootUrls(Callback callback); + + @GET("/people/") + public void getAllPeople(@Query("page") int page, + Callback> callback); + + @GET("/people/{id}/") + public void getPeople(@Path("id") int peopleId, + Callback callback); + + @GET("/films/") + public void getAllFilms(@Query("page") int page, + Callback> callback); + + @GET("/films/{id}/") + public void getFilm(@Path("id") int filmId, + Callback callback); + + @GET("/starships") + public void getAllStarships(@Query("page") int page, + Callback> callback); + + @GET("/starships/{id}/") + public void getStarship(@Path("id") int starshipId, + Callback callback); + + @GET("/vehicles/") + public void getAllVehicles(@Query("page") int page, + Callback> callback); + + @GET("/vehicles/{id}/") + public void getVehicle(@Path("id") int vehicleId, + Callback callback); + + @GET("/species/") + public void getAllSpecies(@Query("page") int page, + Callback> callback); + + @GET("/{route}") + public void getSpecies(@Path("route") String route, + Callback callback); + + @GET("/planets/") + public void getAllPlanets(@Query("page") int page, + Callback> callback); + + @GET("/{route}") + public void getPlanet(@Path("route") String route, + Callback callback); + + @POST("/favorite/{id}") + public void setFavorite(@Path("id") String id, + Callback callBack); + +} diff --git a/app/src/main/java/br/com/star_wars_wiki/network/StarWarsApi.java b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsApi.java new file mode 100644 index 00000000..9c76dad5 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsApi.java @@ -0,0 +1,31 @@ +package br.com.star_wars_wiki.network; + +import br.com.star_wars_wiki.utils.APIConstants; +import retrofit.RestAdapter; + +public class StarWarsApi { + + private StarWars mSwApi; + private static StarWarsApi sInstance; + + private StarWarsApi() { + final RestAdapter restAdapter = new RestAdapter.Builder() + .setClient(new StarWarsOkClient()) + .setEndpoint(APIConstants.BASE_URL) + .setLogLevel(RestAdapter.LogLevel.FULL) + .build(); + mSwApi = restAdapter.create(StarWars.class); + } + + public static void init() { + sInstance = new StarWarsApi(); + } + + public static StarWars getApi() { + return sInstance.mSwApi; + } + + public void setApi(StarWars starWarsApi) { + sInstance.mSwApi = starWarsApi; + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/network/StarWarsFavoriteApi.java b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsFavoriteApi.java new file mode 100644 index 00000000..3c14c221 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsFavoriteApi.java @@ -0,0 +1,29 @@ +package br.com.star_wars_wiki.network; + +import br.com.star_wars_wiki.utils.APIConstants; +import retrofit.RestAdapter; + +public class StarWarsFavoriteApi { + private StarWars mSwApi; + private static StarWarsFavoriteApi sInstance; + + private StarWarsFavoriteApi(){ + final RestAdapter restAdapter = new RestAdapter.Builder() + .setEndpoint(APIConstants.BASE_URL_FAVORITES) + .setLogLevel(RestAdapter.LogLevel.FULL) + .build(); + mSwApi = restAdapter.create(StarWars.class); + } + + public static void init(){ + sInstance = new StarWarsFavoriteApi(); + } + + public static StarWars getApi(){ + return sInstance.mSwApi; + } + + public void setApi(StarWars starWarsApi){ + sInstance.mSwApi = starWarsApi; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/network/StarWarsOkClient.java b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsOkClient.java new file mode 100644 index 00000000..aa9cff4e --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/network/StarWarsOkClient.java @@ -0,0 +1,22 @@ +package br.com.star_wars_wiki.network; + +import android.os.Build; +import retrofit.client.OkClient; +import retrofit.client.Request; + +import java.io.IOException; +import java.net.HttpURLConnection; + +public class StarWarsOkClient extends OkClient { + + public StarWarsOkClient() { + super(); + } + + @Override + protected HttpURLConnection openConnection(Request request) throws IOException { + HttpURLConnection connection = super.openConnection(request); + connection.setRequestProperty("User-Agent", "swapi-android-" + Build.VERSION.RELEASE); + return connection; + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/utils/APIConstants.java b/app/src/main/java/br/com/star_wars_wiki/utils/APIConstants.java new file mode 100644 index 00000000..c6ddbc63 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/utils/APIConstants.java @@ -0,0 +1,6 @@ +package br.com.star_wars_wiki.utils; + +public class APIConstants { + public static final String BASE_URL = "https://swapi.dev/api/"; + public static final String BASE_URL_FAVORITES = "http://private-782d3-starwarsfavorites.apiary-mock.com/"; +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/utils/RecyclerItemClickListener.java b/app/src/main/java/br/com/star_wars_wiki/utils/RecyclerItemClickListener.java new file mode 100644 index 00000000..6126c56e --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/utils/RecyclerItemClickListener.java @@ -0,0 +1,60 @@ +package br.com.star_wars_wiki.utils; + +import android.content.Context; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.View; +import android.widget.AdapterView; + +import androidx.recyclerview.widget.RecyclerView; + +public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { + + private OnItemClickListener mListener; + GestureDetector mGestureDetector; + + @Override + public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { + View childView = rv.findChildViewUnder(e.getX(), e.getY()); + if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { + mListener.onItemClick(childView, rv.getChildAdapterPosition(childView)); + return true; + } + return false; + } + + @Override + public void onTouchEvent(RecyclerView rv, MotionEvent e) { + + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + + } + + public interface OnItemClickListener extends AdapterView.OnItemClickListener { + public void onItemClick(View view, int position); + + public void onLongItemClick(View view, int position); + } + + public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { + mListener = listener; + mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onSingleTapUp(MotionEvent e) { + return true; + } + + @Override + public void onLongPress(MotionEvent e) { + View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); + if (child != null && mListener != null) { + mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child)); + } + } + }); + + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/utils/SWUtils.java b/app/src/main/java/br/com/star_wars_wiki/utils/SWUtils.java new file mode 100644 index 00000000..2ca4790a --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/utils/SWUtils.java @@ -0,0 +1,97 @@ +package br.com.star_wars_wiki.utils; + +import br.com.star_wars_wiki.entity.*; +import android.text.TextUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +/** + * Created by Oleur on 24/12/2014. + * Utility class for Star Wars API + */ +public class SWUtils { + + public enum SORT { + ASC, + DESC + } + + public static int compare(int lhs, int rhs) { + return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1); + } + + public static ArrayList sortPeopleByMass(ArrayList peopleList, final SORT sortBy) { + if (peopleList == null) { + return null; + } + Collections.sort(peopleList, new Comparator() { + @Override + public int compare(People lhs, People rhs) { + int heightLhs = Integer.parseInt(lhs.mass); + int heightRhs = Integer.parseInt(rhs.mass); + if (sortBy == SORT.ASC) { + return SWUtils.compare(heightLhs, heightRhs); + } else { + return SWUtils.compare(heightRhs, heightLhs); + } + } + }); + return peopleList; + } + + public static ArrayList sortPeopleByHeight(ArrayList peopleList, final SORT sortBy) { + if (peopleList == null) { + return null; + } + Collections.sort(peopleList, new Comparator() { + @Override + public int compare(People lhs, People rhs) { + int heightLhs = Integer.parseInt(lhs.height); + int heightRhs = Integer.parseInt(rhs.height); + if (sortBy == SORT.ASC) { + return SWUtils.compare(heightLhs, heightRhs); + } else { + return SWUtils.compare(heightRhs, heightLhs); + } + } + }); + return peopleList; + } + + public static boolean isEmpireVehicle(Vehicle vehicle) { + if (vehicle == null || TextUtils.isEmpty(vehicle.manufacturer)) { + return false; + } + return (vehicle.manufacturer.contains("Sienar") + || vehicle.manufacturer.contains("Kuat") + || vehicle.manufacturer.contains("Imperial") + || vehicle.manufacturer.contains("Aratech")); + } + + public static String filmUrlToFilmTitle(String filmUrl) { + int filmId; + try { + filmId = filmUrl.charAt(filmUrl.length()-2); + } catch (Exception e) { + return null; + } + switch (filmId) { + case 1: + return "Star Wars Episode IV A New Hope"; + case 2: + return "Star Wars Episode V The Empire Strikes Back"; + case 3: + return "Star Wars Episode VI Return of the Jedi"; + case 4: + return "Star Wars Episode I The Phantom Menace"; + case 5: + return "Star Wars Episode II Attack of the Clones"; + case 6: + return "Star Wars Episode III Revenge of the Sith"; + default: + return null; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/br/com/star_wars_wiki/view_model/FavoriteViewModel.java b/app/src/main/java/br/com/star_wars_wiki/view_model/FavoriteViewModel.java new file mode 100644 index 00000000..573826cd --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/view_model/FavoriteViewModel.java @@ -0,0 +1,36 @@ +package br.com.star_wars_wiki.view_model; + +import android.app.Application; +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.repository.FavoriteRepo; +import br.com.star_wars_wiki.entity.Favorite; + +public class FavoriteViewModel extends AndroidViewModel { + private FavoriteRepo favoriteRepo; + private LiveData> getAllFavorite; + + public FavoriteViewModel(@NonNull Application application) { + super(application); + favoriteRepo = new FavoriteRepo(application); + getAllFavorite = favoriteRepo.getAllFavorites(); + } + + public void insert(Favorite favorite){ + favoriteRepo.insert(favorite); + } + + public void remove(Favorite favorite, Context context){ + favoriteRepo.remove(favorite, context); + } + + public LiveData> getAllFavorites(){ + return this.getAllFavorite; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/view_model/PeopleViewModel.java b/app/src/main/java/br/com/star_wars_wiki/view_model/PeopleViewModel.java new file mode 100644 index 00000000..9db1a850 --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/view_model/PeopleViewModel.java @@ -0,0 +1,32 @@ +package br.com.star_wars_wiki.view_model; + +import android.app.Application; + +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; + +import java.util.List; + +import br.com.star_wars_wiki.database.dao.PeopleDAO; +import br.com.star_wars_wiki.database.repository.PeopleRepo; +import br.com.star_wars_wiki.entity.People; + +public class PeopleViewModel extends AndroidViewModel { + private PeopleRepo peopleRepo; + private LiveData> getAllPeople; + + public PeopleViewModel(Application application){ + super(application); + peopleRepo = new PeopleRepo(application); + getAllPeople = peopleRepo.getAllPeople(); + } + + public void insert(List peopleList){ + peopleRepo.insert(peopleList); + } + + public LiveData> getAllPeople(){ + return getAllPeople; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/view_model/PlanetViewModel.java b/app/src/main/java/br/com/star_wars_wiki/view_model/PlanetViewModel.java new file mode 100644 index 00000000..a0613d1f --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/view_model/PlanetViewModel.java @@ -0,0 +1,40 @@ +package br.com.star_wars_wiki.view_model; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.repository.PlanetRepo; +import br.com.star_wars_wiki.entity.Planet; + +public class PlanetViewModel extends AndroidViewModel { + private PlanetRepo planetRepo; + private LiveData> getAllPlanets; + + public PlanetViewModel(Application application) { + super(application); + planetRepo = new PlanetRepo(application); + getAllPlanets = planetRepo.getAllPlanets(); + } + + public void insert(Planet planet){ + planetRepo.insert(planet); + } + + public LiveData> getAllPlanets(){ + return this.getAllPlanets; + } + + public Planet getPlanet(String planetName){ + for(Planet planet : getAllPlanets.getValue()){ + if(planet.getName().equals(planetName)){ + return planet; + } + } + return null; + } +} diff --git a/app/src/main/java/br/com/star_wars_wiki/view_model/SpecieViewModel.java b/app/src/main/java/br/com/star_wars_wiki/view_model/SpecieViewModel.java new file mode 100644 index 00000000..fa0deaaa --- /dev/null +++ b/app/src/main/java/br/com/star_wars_wiki/view_model/SpecieViewModel.java @@ -0,0 +1,41 @@ +package br.com.star_wars_wiki.view_model; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; + +import java.util.List; + +import br.com.star_wars_wiki.database.repository.SpecieRepo; +import br.com.star_wars_wiki.entity.Planet; +import br.com.star_wars_wiki.entity.Specie; + +public class SpecieViewModel extends AndroidViewModel { + private SpecieRepo specieRepo; + private LiveData> getAllSpecies; + + public SpecieViewModel(Application application) { + super(application); + specieRepo = new SpecieRepo(application); + getAllSpecies = specieRepo.getAllSpecies(); + } + + public void insert(Specie specie){ + specieRepo.insert(specie); + } + + public LiveData> getAllSpecies(){ + return this.getAllSpecies(); + } + + public Specie getSpecie(String specieName){ + for(Specie specie : getAllSpecies.getValue()){ + if(specie.getName().equals(specieName)){ + return specie; + } + } + return null; + } +} diff --git a/app/src/main/res/drawable-v24/chewbacca.png b/app/src/main/res/drawable-v24/chewbacca.png new file mode 100644 index 00000000..0919ee63 Binary files /dev/null and b/app/src/main/res/drawable-v24/chewbacca.png differ diff --git a/app/src/main/res/drawable-v24/darth_vader.png b/app/src/main/res/drawable-v24/darth_vader.png new file mode 100644 index 00000000..d9c7d578 Binary files /dev/null and b/app/src/main/res/drawable-v24/darth_vader.png differ diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..2b068d11 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..07d5da9c --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/act_favoritos.xml b/app/src/main/res/layout/act_favoritos.xml new file mode 100644 index 00000000..095f55e7 --- /dev/null +++ b/app/src/main/res/layout/act_favoritos.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/act_lista_personagens.xml b/app/src/main/res/layout/act_lista_personagens.xml new file mode 100644 index 00000000..ca812162 --- /dev/null +++ b/app/src/main/res/layout/act_lista_personagens.xml @@ -0,0 +1,44 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/act_main.xml b/app/src/main/res/layout/act_main.xml new file mode 100644 index 00000000..3b9fffde --- /dev/null +++ b/app/src/main/res/layout/act_main.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/act_personagem.xml b/app/src/main/res/layout/act_personagem.xml new file mode 100644 index 00000000..44c4a13c --- /dev/null +++ b/app/src/main/res/layout/act_personagem.xml @@ -0,0 +1,348 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +