From a3eb670595b135be4c5c16613279bcb3f6e68fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Wed, 8 May 2024 13:17:51 +0200 Subject: [PATCH] new post fragment --- app/src/main/AndroidManifest.xml | 6 +-- .../quack/{ => activity}/MainActivity.java | 22 +------- .../{ => activity}/RegisterActivity.java | 4 +- .../{ => activity}/SwitcherActivity.java | 8 ++- .../quack/{ => fragment}/FeedFragment.java | 3 +- .../quack/fragment/NewPostFragment.java | 51 +++++++++++++++++++ .../PostRecyclerViewAdapter.java | 2 +- .../{ => fragment}/ProfileFeedFragment.java | 3 +- .../quack/{ => fragment}/ProfileFragment.java | 3 +- .../com/dowerx/quack/service/AuthService.java | 4 +- .../com/dowerx/quack/service/PostService.java | 50 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/activity_register.xml | 2 +- app/src/main/res/layout/activity_switcher.xml | 2 +- .../main/res/layout/fragment_feed_list.xml | 2 +- app/src/main/res/layout/fragment_new_post.xml | 39 ++++++++++++++ app/src/main/res/layout/fragment_profile.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 18 files changed, 169 insertions(+), 37 deletions(-) rename app/src/main/java/com/dowerx/quack/{ => activity}/MainActivity.java (68%) rename app/src/main/java/com/dowerx/quack/{ => activity}/RegisterActivity.java (97%) rename app/src/main/java/com/dowerx/quack/{ => activity}/SwitcherActivity.java (86%) rename app/src/main/java/com/dowerx/quack/{ => fragment}/FeedFragment.java (97%) create mode 100644 app/src/main/java/com/dowerx/quack/fragment/NewPostFragment.java rename app/src/main/java/com/dowerx/quack/{ => fragment}/PostRecyclerViewAdapter.java (98%) rename app/src/main/java/com/dowerx/quack/{ => fragment}/ProfileFeedFragment.java (97%) rename app/src/main/java/com/dowerx/quack/{ => fragment}/ProfileFragment.java (97%) create mode 100644 app/src/main/res/layout/fragment_new_post.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b919e3d..33a7970 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,15 +13,15 @@ android:theme="@style/Theme.Quack" tools:targetApi="31"> diff --git a/app/src/main/java/com/dowerx/quack/MainActivity.java b/app/src/main/java/com/dowerx/quack/activity/MainActivity.java similarity index 68% rename from app/src/main/java/com/dowerx/quack/MainActivity.java rename to app/src/main/java/com/dowerx/quack/activity/MainActivity.java index b3f4968..4c498d8 100644 --- a/app/src/main/java/com/dowerx/quack/MainActivity.java +++ b/app/src/main/java/com/dowerx/quack/activity/MainActivity.java @@ -1,10 +1,9 @@ -package com.dowerx.quack; +package com.dowerx.quack.activity; 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; @@ -12,15 +11,10 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.dowerx.quack.R; import com.dowerx.quack.service.AuthService; -import com.google.firebase.auth.FirebaseAuth; - -import java.util.concurrent.ExecutionException; public class MainActivity extends AppCompatActivity { - -// private FirebaseAuth fbs; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -31,8 +25,6 @@ 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) { @@ -44,16 +36,6 @@ public class MainActivity extends AppCompatActivity { AuthService.Login loginTask = new AuthService.Login(this); loginTask.execute(email, password); -// 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) { diff --git a/app/src/main/java/com/dowerx/quack/RegisterActivity.java b/app/src/main/java/com/dowerx/quack/activity/RegisterActivity.java similarity index 97% rename from app/src/main/java/com/dowerx/quack/RegisterActivity.java rename to app/src/main/java/com/dowerx/quack/activity/RegisterActivity.java index 123f3ac..2d06940 100644 --- a/app/src/main/java/com/dowerx/quack/RegisterActivity.java +++ b/app/src/main/java/com/dowerx/quack/activity/RegisterActivity.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.activity; import android.content.Intent; import android.os.Bundle; @@ -12,8 +12,8 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.dowerx.quack.R; import com.dowerx.quack.service.AuthService; -import com.google.firebase.auth.FirebaseAuth; public class RegisterActivity extends AppCompatActivity { diff --git a/app/src/main/java/com/dowerx/quack/SwitcherActivity.java b/app/src/main/java/com/dowerx/quack/activity/SwitcherActivity.java similarity index 86% rename from app/src/main/java/com/dowerx/quack/SwitcherActivity.java rename to app/src/main/java/com/dowerx/quack/activity/SwitcherActivity.java index 57d98dc..adda24f 100644 --- a/app/src/main/java/com/dowerx/quack/SwitcherActivity.java +++ b/app/src/main/java/com/dowerx/quack/activity/SwitcherActivity.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.activity; import android.os.Bundle; import android.view.MenuItem; @@ -11,6 +11,10 @@ import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import androidx.fragment.app.Fragment; +import com.dowerx.quack.fragment.FeedFragment; +import com.dowerx.quack.fragment.NewPostFragment; +import com.dowerx.quack.fragment.ProfileFragment; +import com.dowerx.quack.R; import com.google.android.material.navigation.NavigationBarView; public class SwitcherActivity extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener, NavigationBarView.OnItemReselectedListener { @@ -41,6 +45,8 @@ public class SwitcherActivity extends AppCompatActivity implements NavigationBar public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { if (menuItem.getItemId() == R.id.nav_feed) { loadFragment(new FeedFragment()); + } else if (menuItem.getItemId() == R.id.nav_post) { + loadFragment(new NewPostFragment()); } else if (menuItem.getItemId() == R.id.nav_profile) { loadFragment(new ProfileFragment()); } diff --git a/app/src/main/java/com/dowerx/quack/FeedFragment.java b/app/src/main/java/com/dowerx/quack/fragment/FeedFragment.java similarity index 97% rename from app/src/main/java/com/dowerx/quack/FeedFragment.java rename to app/src/main/java/com/dowerx/quack/fragment/FeedFragment.java index 4091634..d52e252 100644 --- a/app/src/main/java/com/dowerx/quack/FeedFragment.java +++ b/app/src/main/java/com/dowerx/quack/fragment/FeedFragment.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.fragment; import android.content.Context; import android.os.Bundle; @@ -15,6 +15,7 @@ import androidx.loader.content.Loader; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.dowerx.quack.R; import com.dowerx.quack.model.Post; import com.dowerx.quack.service.PostService; diff --git a/app/src/main/java/com/dowerx/quack/fragment/NewPostFragment.java b/app/src/main/java/com/dowerx/quack/fragment/NewPostFragment.java new file mode 100644 index 0000000..659fb1a --- /dev/null +++ b/app/src/main/java/com/dowerx/quack/fragment/NewPostFragment.java @@ -0,0 +1,51 @@ +package com.dowerx.quack.fragment; + +import android.os.AsyncTask; +import android.os.Bundle; + +import androidx.fragment.app.Fragment; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; + +import com.dowerx.quack.R; +import com.dowerx.quack.service.PostService; + +public class NewPostFragment extends Fragment { + + EditText content; + + public NewPostFragment() { + // Required empty public constructor + } + + public static NewPostFragment newInstance() { + NewPostFragment fragment = new NewPostFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_new_post, container, false); + content = view.findViewById(R.id.content); + view.findViewById(R.id.send_post).setOnClickListener(this::send_post); + return view; + } + + public void send_post(View view) { + PostService.NewPost task = new PostService.NewPost(this.getActivity()); + task.execute(content.getText().toString()); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/dowerx/quack/PostRecyclerViewAdapter.java b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java similarity index 98% rename from app/src/main/java/com/dowerx/quack/PostRecyclerViewAdapter.java rename to app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java index b1c26a5..44daa2c 100644 --- a/app/src/main/java/com/dowerx/quack/PostRecyclerViewAdapter.java +++ b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.fragment; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; diff --git a/app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java b/app/src/main/java/com/dowerx/quack/fragment/ProfileFeedFragment.java similarity index 97% rename from app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java rename to app/src/main/java/com/dowerx/quack/fragment/ProfileFeedFragment.java index b804521..f43e490 100644 --- a/app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java +++ b/app/src/main/java/com/dowerx/quack/fragment/ProfileFeedFragment.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.fragment; import android.content.Context; import android.os.Bundle; @@ -15,6 +15,7 @@ import androidx.loader.content.Loader; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.dowerx.quack.R; import com.dowerx.quack.model.Post; import com.dowerx.quack.service.PostService; diff --git a/app/src/main/java/com/dowerx/quack/ProfileFragment.java b/app/src/main/java/com/dowerx/quack/fragment/ProfileFragment.java similarity index 97% rename from app/src/main/java/com/dowerx/quack/ProfileFragment.java rename to app/src/main/java/com/dowerx/quack/fragment/ProfileFragment.java index 02fca0f..c17749c 100644 --- a/app/src/main/java/com/dowerx/quack/ProfileFragment.java +++ b/app/src/main/java/com/dowerx/quack/fragment/ProfileFragment.java @@ -1,4 +1,4 @@ -package com.dowerx.quack; +package com.dowerx.quack.fragment; import android.os.Bundle; import android.view.LayoutInflater; @@ -12,6 +12,7 @@ import androidx.fragment.app.Fragment; import androidx.loader.app.LoaderManager; import androidx.loader.content.Loader; +import com.dowerx.quack.R; import com.dowerx.quack.model.User; import com.dowerx.quack.service.AuthService; import com.dowerx.quack.service.UserService; diff --git a/app/src/main/java/com/dowerx/quack/service/AuthService.java b/app/src/main/java/com/dowerx/quack/service/AuthService.java index 6589811..006c613 100644 --- a/app/src/main/java/com/dowerx/quack/service/AuthService.java +++ b/app/src/main/java/com/dowerx/quack/service/AuthService.java @@ -5,8 +5,8 @@ import android.content.Intent; import android.os.AsyncTask; import android.widget.Toast; -import com.dowerx.quack.MainActivity; -import com.dowerx.quack.SwitcherActivity; +import com.dowerx.quack.activity.MainActivity; +import com.dowerx.quack.activity.SwitcherActivity; import com.google.android.gms.tasks.Tasks; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index 38d3579..a83e093 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -1,6 +1,9 @@ package com.dowerx.quack.service; +import android.app.Activity; import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; import android.util.Log; import android.widget.Toast; @@ -8,10 +11,12 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.loader.content.AsyncTaskLoader; +import com.dowerx.quack.R; 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.auth.FirebaseAuth; import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.Filter; @@ -21,6 +26,7 @@ import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QuerySnapshot; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutionException; @@ -110,4 +116,48 @@ public class PostService { forceLoad(); } } + + public static class NewPost extends AsyncTask { + + Activity activity; + + public NewPost(Activity activity) { + this.activity = activity; + } + + @Override + protected Boolean doInBackground(String... strings) { + FirebaseFirestore db = FirebaseFirestore.getInstance(); + FirebaseAuth fba = FirebaseAuth.getInstance(); + + Log.d(this.getClass().getName(), "before adding post"); + + try { + DocumentSnapshot user = Tasks.await(db.collection("user").where(Filter.equalTo("id", fba.getUid())).get()).getDocuments().get(0); + HashMap post = new HashMap<>(); + post.put("content", strings[0]); + post.put("image", ""); + post.put("user", user.getReference()); + Tasks.await(db.collection("post").add(post)); + + Log.d(this.getClass().getName(), "after adding post"); + + } catch (ExecutionException | InterruptedException e) { + return false; + } + return true; + } + + @Override + protected void onPostExecute(Boolean result) { + if (result) { + activity.findViewById(R.id.nav_profile).performClick(); + } else { + Toast toast = new Toast(activity); + toast.setDuration(Toast.LENGTH_LONG); + toast.setText("failed to send post"); + toast.show(); + } + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b4bb5ba..e92460c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".activity.MainActivity"> + tools:context=".activity.RegisterActivity"> + tools:context=".activity.SwitcherActivity"> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_new_post.xml b/app/src/main/res/layout/fragment_new_post.xml new file mode 100644 index 0000000..6986343 --- /dev/null +++ b/app/src/main/res/layout/fragment_new_post.xml @@ -0,0 +1,39 @@ + + + + + + + +