Skip to content

Instantly share code, notes, and snippets.

@brunoazevedomendonca
Last active November 9, 2017 16:20
Show Gist options
  • Save brunoazevedomendonca/ec5bdd917a61c37ffaa1f99d07afde00 to your computer and use it in GitHub Desktop.
Save brunoazevedomendonca/ec5bdd917a61c37ffaa1f99d07afde00 to your computer and use it in GitHub Desktop.
Resolução Prova 1
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.localizator">
<application
android:allowBackup="true"
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=".main.MainActivity"
android:theme="@style/AppThemeNoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".add_address.AddAddressActivity" />
<activity android:name=".show_addresses.ShowAddressesActivity"/>
</application>
</manifest>
package com.example.android.localizator.add_address;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.localizator.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class AddAddressActivity extends AppCompatActivity implements AddAddressView {
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@BindView(R.id.edt_address)
TextView edtAddress;
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 4
AddAddressPresenter addAddressPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_address);
//Alterações necessárias para o Exercício 2
ButterKnife.bind(this);
//Alterações necessárias para o Exercício 4
addAddressPresenter = new AddAddressPresenter(this);
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@OnClick(R.id.btn_add)
void clickAdd(View view){
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 4
addAddressPresenter.addAddress(edtAddress.getText().toString());
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 4
@Override
public void showMessageError(String message) {
Toast.makeText(AddAddressActivity.this, message, Toast.LENGTH_SHORT).show();
}
@Override
//retorna o endereço para a MainActivity
public void returnAddress(String address) {
Intent resultIntent = new Intent();
resultIntent.putExtra("movie_name", address);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
//-------------------------------------------------------------------------------------------
}
package com.example.android.localizator.add_address;
//Alterações necessárias para o Exercício 4
public class AddAddressPresenter {
AddAddressView addAddressView;
AddAddressPresenter(AddAddressView addAddressView) {
this.addAddressView = addAddressView;
}
void addAddress(String address){
//verifica se há um endereço digitado
if (address.isEmpty()){
addAddressView.showMessageError("Digite o endereço que deseja adicionar");
}else{
addAddressView.returnAddress(address);
}
}
}
package com.example.android.localizator.add_address;
//Alterações necessárias para o Exercício 4
public interface AddAddressView {
void showMessageError(String message);
void returnAddress(String address);
}
package com.example.android.localizator.main;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.example.android.localizator.R;
import com.example.android.localizator.add_address.AddAddressActivity;
import com.example.android.localizator.show_addresses.ShowAddressesActivity;
import java.util.ArrayList;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity implements MainView {
//Alterações necessárias para o Exercício 4
MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//Alterações necessárias para o Exercício 4
mainPresenter = new MainPresenter(this);
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@OnClick(R.id.btn_add_address)
void clickAddAddress(View view){
//-------------------------------------------------------------------------------------------
//abre a activity para adicionar endereços
Intent openAddAddressActivity = new Intent(MainActivity.this, AddAddressActivity.class);
startActivityForResult(openAddAddressActivity, RC_ADD_ADDRESS);
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@OnClick(R.id.btn_show_addresses)
void clickShowAddresses(View view){
//-------------------------------------------------------------------------------------------
//abre a activity para exibir os endereços cadastrados
//Alterações necessárias para o Exercício 4
mainPresenter.verifyOpenShowAddressesActivity();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Alterações necessárias para o Exercício 4
mainPresenter.addNewAddress(requestCode, resultCode, data);
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 4
@Override
public void showMessageError(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void showAddresses(ArrayList<String> lstAddresses) {
//abre a ShowAddressActivity enviando a lista de endereços
Intent openShowAddressActivity = new Intent(MainActivity.this, ShowAddressesActivity.class);
openShowAddressActivity.putStringArrayListExtra("addresses_list", lstAddresses);
startActivity(openShowAddressActivity);
}
//-------------------------------------------------------------------------------------------
}
package com.example.android.localizator.main;
import android.app.Activity;
import android.content.Intent;
import java.util.ArrayList;
//Alterações necessárias para o Exercício 4
public class MainPresenter {
//lista de endereços
private ArrayList<String> lstAddresses = new ArrayList<>();
MainView mainView;
MainPresenter(MainView mainView){
this.mainView = mainView;
}
//verifica se há endereços cadastrados antes executar a ShowAddressesActivity
public void verifyOpenShowAddressesActivity() {
if(lstAddresses.size() <= 0){
mainView.showMessageError("Não há endereços cadastrados");
}else{
//abre a ShowAddressActivity enviando a lista de endereços
mainView.showAddresses(lstAddresses);
}
}
public void addNewAddress(int requestCode, int resultCode, Intent data) {
//captura o resultado da tela de cadastro de endereços e adiciona na lista
if(requestCode == MainView.RC_ADD_ADDRESS && resultCode == Activity.RESULT_OK) {
lstAddresses.add(data.getStringExtra("movie_name"));
}
}
}
package com.example.android.localizator.main;
import java.util.ArrayList;
//Alterações necessárias para o Exercício 4
public interface MainView {
//código utilizado para adicionar novos endereços
int RC_ADD_ADDRESS = 123;
void showMessageError(String message);
void showAddresses(ArrayList<String> lstAddresses);
}
package com.example.android.localizator.show_addresses;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.android.localizator.R;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class AddressesAdapter extends RecyclerView.Adapter<AddressesAdapter.ViewHolder>{
private List<String> addressesList;
//Alterações necessárias para o Exercício 3
private OnRecyclerViewClickListener onRecyclerViewClickListener;
//Construtor para receber a lista
AddressesAdapter(List<String> addressesList){
this.addressesList = addressesList;
}
//Infla o layout XML
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_list_item, parent, false);
return new ViewHolder(v);
}
//Seta os dados na lista
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tvAddress.setText(addressesList.get(position));
}
//Retorna o tamanho da lista
@Override
public int getItemCount() {
return addressesList.size();
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 3
public void setOnRecyclerViewClickListener(OnRecyclerViewClickListener onRecyclerViewClickListener) {
this.onRecyclerViewClickListener = onRecyclerViewClickListener;
}
//-------------------------------------------------------------------------------------------
//Mapeamento dos componentes da View
public class ViewHolder extends RecyclerView.ViewHolder{
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@BindView(R.id.tv_address)
TextView tvAddress;
//-------------------------------------------------------------------------------------------
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 3
@OnClick(R.id.address_item)
void onClickItem(View view){
if(onRecyclerViewClickListener != null)
onRecyclerViewClickListener.onClick(view, getAdapterPosition());
}
//-------------------------------------------------------------------------------------------
}
}
package com.example.android.localizator.show_addresses;
import android.view.View;
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 3
public interface OnRecyclerViewClickListener {
void onClick(View view, int position);
}
//-------------------------------------------------------------------------------------------
package com.example.android.localizator.show_addresses;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.example.android.localizator.R;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ShowAddressesActivity extends AppCompatActivity {
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 2
@BindView(R.id.rv_addresses)
RecyclerView rvAddresses;
//-------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_addresses);
ButterKnife.bind(this);
//captura a lista enviada pela MainActivity
final ArrayList<String> lstAddresses = getIntent().getStringArrayListExtra("addresses_list");
//instancia um AddressesAdapter passando a lista de endereços
AddressesAdapter addressesAdapter = new AddressesAdapter(lstAddresses);
//-------------------------------------------------------------------------------------------
//Alterações necessárias para o Exercício 3
addressesAdapter.setOnRecyclerViewClickListener(new OnRecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
String endereco = lstAddresses.get(position);
Intent openMap = new Intent(Intent.ACTION_VIEW);
openMap.setData(Uri.parse("geo:0,0?q=" + endereco));
if(openMap.resolveActivity(getPackageManager()) != null) {
startActivity(openMap);
}else {
Toast.makeText(ShowAddressesActivity.this, "Impossível abrir o recurso", Toast.LENGTH_LONG).show();
}
}
});
//-------------------------------------------------------------------------------------------
//seta o adapter no Recycler View
rvAddresses.setAdapter(addressesAdapter);
//cria o gerenciador de layouts
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//seta o gerenciador de layouts no Recycler View
rvAddresses.setLayoutManager(layoutManager);
}
}
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="#009688" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.localizator.add_address.AddAddressActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/address"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="@+id/edt_address"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/add"
android:layout_marginTop="32dp"
android:layout_gravity="center"
android:id="@+id/btn_add"/>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#536DFE"
tools:context="com.example.android.localizator.main.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="8dp"
android:background="@drawable/logo"
android:layout_gravity="center"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/add_address"
android:id="@+id/btn_add_address"
android:background="@drawable/shape_main_buttons"
android:layout_margin="16dp"
android:layout_gravity="center"
android:textColor="#ffffff"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/show_addresses"
android:id="@+id/btn_show_addresses"
android:background="@drawable/shape_main_buttons"
android:layout_margin="16dp"
android:layout_gravity="center"
android:textColor="#ffffff"/>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.localizator.show_addresses.ShowAddressesActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/rv_addresses"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/address_item">
<TextView
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rua Teste, 01, Cidade Teste - ET"
android:id="@+id/tv_address"/>
<View
android:layout_width="match_parent"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_height="1dp"
android:background="@color/colorPrimary"/>
</LinearLayout>
<resources>
<string name="app_name">Localizator</string>
<string name="add_address">Adicionar Endereço</string>
<string name="show_addresses">Mostrar Endereços</string>
<string name="address">Endereço</string>
<string name="add">Adicionar</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment