commit 5b6919931fd7168143639f40455ea8d9fd357d43 Author: 230415 <230415@epvc.pt> Date: Thu Apr 16 10:35:39 2026 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /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 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..ca16a99 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /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 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..96f777f --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,46 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace = "com.example.apiaula" + compileSdk = 36 + + defaultConfig { + applicationId = "com.example.apiaula" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } +} + +dependencies { + + implementation(libs.appcompat) + implementation(libs.material) + implementation(libs.activity) + implementation(libs.constraintlayout) + testImplementation(libs.junit) + androidTestImplementation(libs.ext.junit) + androidTestImplementation(libs.espresso.core) + implementation ("com.google.code.gson:gson:2.9.1") + implementation ("com.squareup.retrofit2:retrofit:2.9.0") + implementation ("com.squareup.retrofit2:converter-gson:2.9.0") +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /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/com/example/apiaula/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/apiaula/ExampleInstrumentedTest.java new file mode 100644 index 0000000..7ea8cab --- /dev/null +++ b/app/src/androidTest/java/com/example/apiaula/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.apiaula; + +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("com.example.apiaula", 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 0000000..164a142 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/apiaula/HistoryActivity.java b/app/src/main/java/com/example/apiaula/HistoryActivity.java new file mode 100644 index 0000000..3c8dd68 --- /dev/null +++ b/app/src/main/java/com/example/apiaula/HistoryActivity.java @@ -0,0 +1,59 @@ +package com.example.apiaula; + +import android.os.Bundle; +import android.view.View; +import android.widget.ImageButton; +import android.widget.TextView; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +public class HistoryActivity extends AppCompatActivity { + + private RecyclerView recyclerView; + private TextView tvEmptyState; + private JokeAdapter adapter; + private ArrayList history; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_history); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main_history), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + recyclerView = findViewById(R.id.recyclerViewHistory); + tvEmptyState = findViewById(R.id.tvEmptyState); + ImageButton btnBack = findViewById(R.id.btnBack); + + btnBack.setOnClickListener(v -> finish()); + + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + + // Pegar os dados da intent + if (getIntent() != null && getIntent().hasExtra("history")) { + history = (ArrayList) getIntent().getSerializableExtra("history"); + } + + if (history == null || history.isEmpty()) { + tvEmptyState.setVisibility(View.VISIBLE); + recyclerView.setVisibility(View.GONE); + } else { + tvEmptyState.setVisibility(View.GONE); + recyclerView.setVisibility(View.VISIBLE); + adapter = new JokeAdapter(history); + recyclerView.setAdapter(adapter); + } + } +} diff --git a/app/src/main/java/com/example/apiaula/Joke.java b/app/src/main/java/com/example/apiaula/Joke.java new file mode 100644 index 0000000..0e3db4e --- /dev/null +++ b/app/src/main/java/com/example/apiaula/Joke.java @@ -0,0 +1,27 @@ +package com.example.apiaula; + +import java.io.Serializable; + +public class Joke implements Serializable { + private boolean error; + private String category; + private String type; // "single" or "twopart" + private String joke; // when type is single + private String setup; // when type is twopart + private String delivery; // when type is twopart + + public boolean isError() { return error; } + public String getCategory() { return category; } + public String getType() { return type; } + public String getJoke() { return joke; } + public String getSetup() { return setup; } + public String getDelivery() { return delivery; } + + public String getDisplayText() { + if ("single".equals(type)) { + return joke; + } else { + return setup + "\n" + delivery; + } + } +} diff --git a/app/src/main/java/com/example/apiaula/JokeAdapter.java b/app/src/main/java/com/example/apiaula/JokeAdapter.java new file mode 100644 index 0000000..900fbf3 --- /dev/null +++ b/app/src/main/java/com/example/apiaula/JokeAdapter.java @@ -0,0 +1,51 @@ +package com.example.apiaula; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.List; + +public class JokeAdapter extends RecyclerView.Adapter { + + private List jokes; + + public JokeAdapter(List jokes) { + this.jokes = jokes; + } + + @NonNull + @Override + public JokeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_joke, parent, false); + return new JokeViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull JokeViewHolder holder, int position) { + Joke joke = jokes.get(position); + holder.tvJokeText.setText(joke.getDisplayText()); + holder.tvCategory.setText("Categoria: " + joke.getCategory()); + } + + @Override + public int getItemCount() { + return jokes != null ? jokes.size() : 0; + } + + static class JokeViewHolder extends RecyclerView.ViewHolder { + TextView tvJokeText; + TextView tvCategory; + + public JokeViewHolder(@NonNull View itemView) { + super(itemView); + tvJokeText = itemView.findViewById(R.id.tvJokeText); + tvCategory = itemView.findViewById(R.id.tvCategory); + } + } +} diff --git a/app/src/main/java/com/example/apiaula/JokeApi.java b/app/src/main/java/com/example/apiaula/JokeApi.java new file mode 100644 index 0000000..69bd317 --- /dev/null +++ b/app/src/main/java/com/example/apiaula/JokeApi.java @@ -0,0 +1,9 @@ +package com.example.apiaula; + +import retrofit2.Call; +import retrofit2.http.GET; + +public interface JokeApi { + @GET("joke/Any?lang=pt") + Call getAnyJoke(); +} diff --git a/app/src/main/java/com/example/apiaula/MainActivity.java b/app/src/main/java/com/example/apiaula/MainActivity.java new file mode 100644 index 0000000..f7fc681 --- /dev/null +++ b/app/src/main/java/com/example/apiaula/MainActivity.java @@ -0,0 +1,113 @@ +package com.example.apiaula; + +import android.content.Intent; +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 androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +import java.util.ArrayList; + +public class MainActivity extends AppCompatActivity { + + private TextView tvJokeOrSetup; + private TextView tvDelivery; + private Button btnGetJoke; + private Button btnHistory; + private ProgressBar progressBar; + private JokeApi jokeApi; + private ArrayList jokeHistory; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_main); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + tvJokeOrSetup = findViewById(R.id.tvJokeOrSetup); + tvDelivery = findViewById(R.id.tvDelivery); + btnGetJoke = findViewById(R.id.btnGetJoke); + btnHistory = findViewById(R.id.btnHistory); + progressBar = findViewById(R.id.progressBar); + + jokeHistory = new ArrayList<>(); + + // Inicializar Retrofit + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://v2.jokeapi.dev/") + .addConverterFactory(GsonConverterFactory.create()) + .build(); + + jokeApi = retrofit.create(JokeApi.class); + + btnGetJoke.setOnClickListener(v -> fetchJoke()); + + btnHistory.setOnClickListener(v -> { + Intent intent = new Intent(MainActivity.this, HistoryActivity.class); + intent.putExtra("history", jokeHistory); + startActivity(intent); + }); + } + + private void fetchJoke() { + progressBar.setVisibility(View.VISIBLE); + btnGetJoke.setEnabled(false); + tvJokeOrSetup.setText(""); + tvDelivery.setText(""); + + jokeApi.getAnyJoke().enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + progressBar.setVisibility(View.GONE); + btnGetJoke.setEnabled(true); + + if (response.isSuccessful() && response.body() != null) { + Joke joke = response.body(); + if (joke.isError()) { + Toast.makeText(MainActivity.this, "Erro retornado pela API", Toast.LENGTH_SHORT).show(); + return; + } + + jokeHistory.add(joke); + + if ("single".equals(joke.getType())) { + tvJokeOrSetup.setText(joke.getJoke()); + tvDelivery.setVisibility(View.GONE); + } else if ("twopart".equals(joke.getType())) { + tvJokeOrSetup.setText(joke.getSetup()); + tvDelivery.setText(joke.getDelivery()); + tvDelivery.setVisibility(View.VISIBLE); + } + } else { + Toast.makeText(MainActivity.this, "Erro na resposta", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + progressBar.setVisibility(View.GONE); + btnGetJoke.setEnabled(true); + Toast.makeText(MainActivity.this, "Falha na requisição: " + t.getMessage(), Toast.LENGTH_SHORT).show(); + } + }); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_category_chip.xml b/app/src/main/res/drawable/bg_category_chip.xml new file mode 100644 index 0000000..33a4c0a --- /dev/null +++ b/app/src/main/res/drawable/bg_category_chip.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_back.xml b/app/src/main/res/drawable/ic_back.xml new file mode 100644 index 0000000..19f6bb9 --- /dev/null +++ b/app/src/main/res/drawable/ic_back.xml @@ -0,0 +1,10 @@ + + + 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 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_history.xml b/app/src/main/res/layout/activity_history.xml new file mode 100644 index 0000000..d9a7082 --- /dev/null +++ b/app/src/main/res/layout/activity_history.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..0340de0 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_joke.xml b/app/src/main/res/layout/item_joke.xml new file mode 100644 index 0000000..f89457a --- /dev/null +++ b/app/src/main/res/layout/item_joke.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..dd9115c --- /dev/null +++ b/app/src/main/res/values-night/themes.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..c8524cd --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..dcc1652 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + ApiAula + \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..d24afc6 --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,9 @@ + + + + +