Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Firebase now #23

Merged
merged 6 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
plugins {
alias(libs.plugins.android.application)

// Add the Google services Gradle plugin
id("com.google.gms.google-services")
}


Expand Down Expand Up @@ -44,6 +47,26 @@ dependencies {
implementation(libs.annotation)
implementation(libs.lifecycle.livedata.ktx)
implementation(libs.lifecycle.viewmodel.ktx)

// Import the Firebase BoM
implementation(platform(libs.firebase.bom))
implementation("com.google.firebase:firebase-database")

// Firebase Firestore
implementation("com.google.firebase:firebase-firestore")
// Firebase Storage
implementation ("com.google.firebase:firebase-storage:21.0.1")

// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies
implementation("com.google.firebase:firebase-analytics")

// Latest Glide version
implementation("com.github.bumptech.glide:glide:4.16.0")
// Glide annotation processor
annotationProcessor("com.github.bumptech.glide:compiler:4.15.1")


testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
Expand Down
29 changes: 29 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"project_info": {
"project_number": "818416766946",
"project_id": "camlingo-app",
"storage_bucket": "camlingo-app.firebasestorage.app"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:818416766946:android:188d006520d98d125db3bb",
"android_client_info": {
"package_name": "com.example.camlingo"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyBA__3z-Y495cg0YibjIFa9G5nKwGWfugg"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
23 changes: 22 additions & 1 deletion app/src/main/java/com/example/camlingo/DailyQuestActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.example.camlingo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
Expand All @@ -14,13 +18,17 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.FileInputStream;
import java.util.Objects;

import model.MultipleChoiceQuestion;
import repository.QuestionRepository;

public class DailyQuestActivity extends AppCompatActivity {

private static final String TAG = "DailyQuestActivity";
private Button ClaimButton, StarQuestButton;

@Override
Expand All @@ -29,9 +37,17 @@ protected void onCreate(Bundle savedInstanceState) {
EdgeToEdge.enable(this);
setContentView(R.layout.activity_daily_quest);

Bitmap iconImg;

//find button by id
ClaimButton = findViewById(R.id.claim_button_DailyQuest);
StarQuestButton = findViewById(R.id.startQuest_button_DailyQuest);
ImageView treasureChest = findViewById(R.id.questHomeIcon);

Glide.with(treasureChest)
.load("https://firebasestorage.googleapis.com/v0/b/camlingo-app.firebasestorage.app/o/treasure-chest.png?alt=media&token=f2781c98-a752-49b4-836d-ca80e64fea52")
.into(treasureChest);



//"Claim Now" button click
Expand Down Expand Up @@ -64,4 +80,9 @@ public void onClick(View v) {
return insets;
});
}

public boolean fileExists(String fileName){
File file = getApplicationContext().getFileStreamPath(fileName);
return file.exists();
}
}
12 changes: 4 additions & 8 deletions app/src/main/java/com/example/camlingo/DailyQuestsTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ protected void onCreate(Bundle savedInstanceState) {
// get questions from the repository
QuestionRepository repository = new QuestionRepository(this);

// populate database with sample questions
if (repository.isTableEmpty()) {
repository.populateSampleQuestions();
Log.d("QuestionRepository", "Sample questions populated because the table was empty.");
} else {
Log.d("QuestionRepository", "Table already contains data.");
}

List<MultipleChoiceQuestion> questions = repository.getAllQuestions();
for(MultipleChoiceQuestion question : questions){
Log.i("FireBaseQuestionLoader", question.getType() + ", " + question.getMedia());
}


// pass question to fragment
ImageQuestionFragment fragment = ImageQuestionFragment.newInstance(questions);
QuestionFragment fragment = QuestionFragment.newInstance(questions);


// display the fragment
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/camlingo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import android.widget.PopupMenu;

import java.util.Objects;

import database.FireBaseQuestionLoader;

public class MainActivity extends AppCompatActivity {

private CardView continueLearningCard, dailyQuestsCard, leaderboardCard;
Expand Down Expand Up @@ -45,7 +48,18 @@ protected void onCreate(Bundle savedInstanceState) {
dailyQuestsCard = findViewById(R.id.daily_quests_card);
leaderboardCard = findViewById(R.id.leaderboard_card);

// daily question questions
FireBaseQuestionLoader qloader = new FireBaseQuestionLoader(this);
qloader.loadQuestions();

//Upload Button
upload_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, image_upload.class
));
}
});

// Set up the toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/com/example/camlingo/MediaLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.camlingo;
import android.util.Log;
import android.widget.ImageView;
import android.media.MediaPlayer;
import android.webkit.MimeTypeMap;

import com.bumptech.glide.Glide;

public class MediaLoader {
public void loadMedia(String mediaUrl, ImageView imageView){

// Use file extension to determine if its audio or image file
String fileExt = MimeTypeMap.getFileExtensionFromUrl(mediaUrl);

if (fileExt != null){
if (fileExt.equals("png")){
// load image using Glide
Glide.with(imageView.getContext())
.load(mediaUrl)
.into(imageView);
}else if (fileExt.equals("mp3") || fileExt.equals("wav")){
// Load audio
Log.i("MediaLoader", "testing audio");
}
}
}


}
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
package com.example.camlingo;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import model.MultipleChoiceQuestion;

public class ImageQuestionFragment extends Fragment {
public class QuestionFragment extends Fragment {
private Button checkBtn;
private Button continueBtn;
private Button finishBtn;
private Button playAudioButton;
private ProgressBar progressBar;
private RadioButton option1, option2, option3, option4;
private RadioGroup optionsGroup;
private TextView questionText;
private TextView progressText;
private ImageView questionImage;

private List<MultipleChoiceQuestion> questions;
private int currentQuestionIndex = 0;
private MultipleChoiceQuestion currentQuestion;

private int questionCount = 0;
private int score = 0;
private final double totalQuestions = 5.0;

public ImageQuestionFragment() {
public QuestionFragment() {
// Required empty public constructor
}

// Fragment receives questions
public static ImageQuestionFragment newInstance(List<MultipleChoiceQuestion> questions) {
ImageQuestionFragment fragment = new ImageQuestionFragment();
public static QuestionFragment newInstance(List<MultipleChoiceQuestion> questions) {
QuestionFragment fragment = new QuestionFragment();
Bundle args = new Bundle();
args.putSerializable("questions", new ArrayList<>(questions));
fragment.setArguments(args);
Expand All @@ -51,12 +62,15 @@ public static ImageQuestionFragment newInstance(List<MultipleChoiceQuestion> que
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_image_question, container, false);
View view = inflater.inflate(R.layout.fragment_question, container, false);

// Initialize views
checkBtn = view.findViewById(R.id.checkBtn);
progressBar = view.findViewById(R.id.progressBar);
continueBtn = view.findViewById(R.id.continueBtn);
playAudioButton = view.findViewById(R.id.playAudioButton);
questionText = view.findViewById(R.id.img_question_text);
progressText = view.findViewById(R.id.progressText);
questionImage = view.findViewById(R.id.question_img);
option1 = view.findViewById(R.id.img_option1);
option2 = view.findViewById(R.id.img_option2);
Expand Down Expand Up @@ -91,26 +105,40 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Toast.makeText(getActivity(), "Correct!", Toast.LENGTH_SHORT).show();
checkBtn.setVisibility(View.GONE);
continueBtn.setVisibility(View.VISIBLE);
score++;

}else{
checkBtn.setVisibility(View.GONE);
continueBtn.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), "Incorrect!", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), currentQuestion.getAnswer(), Toast.LENGTH_SHORT).show();
}
}
});

// Continue button click listener
continueBtn.setOnClickListener(v -> {
// if (currentQuestionIndex < questions.size() - 1) {
// currentQuestionIndex++;
// currentQuestion = updateQuestion();
// }
if(questionCount < 5){
if(questionCount < totalQuestions){
currentQuestionIndex++;
// Update progress bar
int progress = (int) (( questionCount / totalQuestions) * 100);
progressBar.setProgress(progress);

questionCount++;

currentQuestion = updateQuestion();
optionsGroup.clearCheck();
}else {

Log.i("QuestionFragment", "progress: " + progress);
}else if(questionCount == totalQuestions){
progressBar.setProgress(100);
continueBtn.setText("View results");
continueBtn.setBackgroundColor(Color.parseColor("#FF5722"));
questionCount++;
}
else {
progressBar.setVisibility(View.GONE);
String resultText = "Score: " + score + "/" + 5;
progressText.setText(resultText);
optionsGroup = getView().findViewById(R.id.optionsGroupImg);
questionText.setText(R.string.finished_quest);
optionsGroup.setVisibility(View.GONE);
Expand All @@ -121,6 +149,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
finishBtn = getView().findViewById(R.id.finishBtn);
finishBtn.setVisibility(View.VISIBLE);

questionImage.setVisibility(View.VISIBLE);
questionImage.setImageResource(R.drawable.baseline_check_24);

setupFinishButton(view); // close activity when finish button clicked
Expand All @@ -130,6 +159,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
return view;
}


private MultipleChoiceQuestion updateQuestion() {
// Get the current question
MultipleChoiceQuestion question = questions.get(currentQuestionIndex);
Expand All @@ -141,8 +171,21 @@ private MultipleChoiceQuestion updateQuestion() {
option3.setText(question.getOptions()[2]);
option4.setText(question.getOptions()[3]);

if(question.getType() == MultipleChoiceQuestion.QuestionType.VISUAL){
questionImage.setVisibility(View.VISIBLE);
Glide.with(questionImage)
.load(question.getMedia())
.into(questionImage);
}
else if(question.getType() == MultipleChoiceQuestion.QuestionType.CONTEXT){
questionImage.setVisibility(View.VISIBLE);
Glide.with(questionImage)
.load(question.getMedia())
.into(questionImage);
}

// Set the image for the question
questionImage.setImageResource(question.getMedia());
// questionImage.setImageResource(question.getMedia());

// Reset buttons for next question
checkBtn.setVisibility(View.VISIBLE);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/database/AppDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AppDatabaseHelper extends SQLiteOpenHelper {
COLUMN_QUESTION + " TEXT NOT NULL, " +
COLUMN_ANSWER + " TEXT NOT NULL, " +
COLUMN_OPTIONS + " TEXT NOT NULL, " +
COLUMN_MEDIA + " INTEGER NOT NULL" +
COLUMN_MEDIA + " TEXT NOT NULL" +
");";

public AppDatabaseHelper(Context context){
Expand Down
Loading