new post fragment

This commit is contained in:
Benedek László 2024-05-08 13:17:51 +02:00
parent 505a27df08
commit a3eb670595
18 changed files with 169 additions and 37 deletions

View File

@ -13,15 +13,15 @@
android:theme="@style/Theme.Quack"
tools:targetApi="31">
<activity
android:name=".SwitcherActivity"
android:name=".activity.SwitcherActivity"
android:exported="false" />
<activity
android:name=".RegisterActivity"
android:name=".activity.RegisterActivity"
android:exported="false"
android:label="@string/title_activity_register"
android:theme="@style/Theme.Quack" />
<activity
android:name=".MainActivity"
android:name=".activity.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -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) {

View File

@ -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 {

View File

@ -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());
}

View File

@ -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;

View File

@ -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());
}
}

View File

@ -1,4 +1,4 @@
package com.dowerx.quack;
package com.dowerx.quack.fragment;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<String, Void, Boolean> {
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<String, Object> 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();
}
}
}
}

View File

@ -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">
<TextView
android:id="@+id/logo"

View File

@ -5,7 +5,7 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
tools:context=".activity.RegisterActivity">
<TextView
android:id="@+id/logo"

View File

@ -5,7 +5,7 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SwitcherActivity">
tools:context=".activity.SwitcherActivity">
<FrameLayout
android:id="@+id/main_fragment_holder"

View File

@ -9,5 +9,5 @@
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".FeedFragment"
tools:context=".fragment.FeedFragment"
tools:listitem="@layout/fragment_post" />

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.NewPostFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/send_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/post_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/content"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileFragment">
tools:context=".fragment.ProfileFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"

View File

@ -14,4 +14,5 @@
<string name="placeholder_name">placeholder_name</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="post_btn">Post</string>
</resources>