This commit is contained in:
Benedek László 2024-05-05 16:36:15 +02:00
parent 1928c1f20e
commit 46c9b7a1f3
8 changed files with 179 additions and 58 deletions

View File

@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
id 'com.google.gms.google-services'
}
android {
@ -9,7 +10,7 @@ android {
defaultConfig {
applicationId "com.dowerx.quack"
minSdk 21
minSdk 23
targetSdk 34
versionCode 1
versionName "1.0"
@ -69,4 +70,8 @@ dependencies {
androidTestImplementation libs.ui.test.junit4
debugImplementation libs.ui.tooling
debugImplementation libs.ui.test.manifest
implementation platform('com.google.firebase:firebase-bom:33.0.0')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore:25.0.0'
}

View File

@ -2,41 +2,36 @@ package com.dowerx.quack;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.dowerx.quack.model.Post;
import com.dowerx.quack.service.PostService;
/**
* A fragment representing a list of Items.
*/
public class FeedFragment extends Fragment {
import java.util.List;
public class FeedFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Post>> {
private RecyclerView recyclerView;
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public FeedFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static FeedFragment newInstance(int columnCount) {
public static FeedFragment newInstance() {
FeedFragment fragment = new FeedFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@ -45,9 +40,7 @@ public class FeedFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
LoaderManager.getInstance(this).initLoader(1, null, this);
}
@Override
@ -55,18 +48,31 @@ public class FeedFragment extends Fragment {
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_feed_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
// TODO: get posts
recyclerView.setAdapter(new PostRecyclerViewAdapter(Post.ITEMS));
}
return view;
}
@NonNull
@Override
public Loader<List<Post>> onCreateLoader(int id, @Nullable Bundle args) {
Log.d(this.getClass().getName(), "onCreateLoader");
return new PostService.GetFeed(getContext());
}
@Override
public void onLoadFinished(@NonNull Loader<List<Post>> loader, List<Post> data) {
Log.d(this.getClass().getName(), "onLoadFinished");
Log.d(this.getClass().getName(), data.toString());
recyclerView.setAdapter(new PostRecyclerViewAdapter(data));
}
@Override
public void onLoaderReset(@NonNull androidx.loader.content.Loader loader) {
}
}

View File

@ -3,6 +3,8 @@ package com.dowerx.quack;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
@ -10,8 +12,12 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth fbs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -22,11 +28,27 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
fbs = FirebaseAuth.getInstance();
}
public void login(View view) {
// TODO: login
String email = String.valueOf(((EditText)findViewById(R.id.email)).getText());
String password = String.valueOf(((EditText)findViewById(R.id.password)).getText());
if (email.isEmpty() || password.isEmpty())
return;
fbs.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
startActivity(new Intent(this, SwitcherActivity.class));
} else {
Toast toast = new Toast(this);
toast.setText("login failed");
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
});
}
public void redirect_register(View view) {

View File

@ -3,6 +3,8 @@ package com.dowerx.quack;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
@ -10,8 +12,12 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.firebase.auth.FirebaseAuth;
public class RegisterActivity extends AppCompatActivity {
private FirebaseAuth fbs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -22,10 +28,40 @@ public class RegisterActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
fbs = FirebaseAuth.getInstance();
}
public void register(View view) {
// TODO: register
String email = String.valueOf(((EditText)findViewById(R.id.email)).getText());
String password = String.valueOf(((EditText)findViewById(R.id.password)).getText());
String rePassword = String.valueOf(((EditText)findViewById(R.id.repeat_password)).getText());
if (email.isEmpty() || password.isEmpty() || rePassword.isEmpty())
return;
if (!password.equals(rePassword)) {
Toast toast = new Toast(this);
toast.setText("passwords don't match");
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
return;
}
fbs.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
Toast toast = new Toast(this);
toast.setText("succesful register");
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
startActivity(new Intent(this, MainActivity.class));
} else {
Toast toast = new Toast(this);
toast.setText("login failed");
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
});
}
public void redirect_login(View view) {

View File

@ -6,27 +6,6 @@ import java.util.Arrays;
import java.util.List;
public class Post {
public static final List<Post> ITEMS;
static {
ITEMS = Arrays.asList(
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 1", "exmaple@example.com", "random_id"), "some content", null, 69, false, "random post id"),
new Post(new User("username 2", "exmaple@example.com", "random_id2"), "some content 2", null, 69, false, "random post id2")
);
}
public User getUser() {
return user;
}

View File

@ -0,0 +1,4 @@
package com.dowerx.quack.service;
public class AuthService {
}

View File

@ -0,0 +1,67 @@
package com.dowerx.quack.service;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.AsyncTaskLoader;
import com.dowerx.quack.model.Post;
import com.dowerx.quack.model.User;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
public class PostService {
public static class GetFeed extends AsyncTaskLoader<List<Post>> {
public GetFeed(@NonNull Context context) {
super(context);
}
@Nullable
@Override
public List<Post> loadInBackground() {
List<Post> posts = new LinkedList<>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
try {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").get())) {
DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get());
String username = user.get("username", String.class);
String userid = user.get("id", String.class);
posts.add(new Post(
new User(username, "", userid),
post.get("content", String.class),
post.get("image", String.class),
0,
false,
post.getId()
));
}
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
return posts;
}
@Override
protected void onStartLoading() {
forceLoad();
}
}
}

View File

@ -2,4 +2,6 @@
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
id 'com.google.gms.google-services' version '4.4.1' apply false
}