한윤범

posting function added

......@@ -36,6 +36,7 @@ dependencies {
compile 'com.google.firebase:firebase-crash:11.0.2'
compile 'com.firebaseui:firebase-ui-database:0.4.0'
compile 'com.android.support:design:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'
}
......
package com.example.user.firebaseauthdemo;
/**
* Created by HYB on 2017. 10. 13..
*/
public class Blog
{
private String title;
private String desc;
private String image;
public Blog(){
}
public Blog(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
package com.example.user.firebaseauthdemo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
/**
* Created by HYB on 2017. 10. 11..
......@@ -17,18 +27,79 @@ import android.view.ViewGroup;
public class Community extends Fragment {
private RecyclerView mBlogList;
private RecyclerView.LayoutManager layoutManager;
private DatabaseReference mDatabase;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.community, container, false);
View view = inflater.inflate(R.layout.community, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.community_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
......
......@@ -12,8 +12,12 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
......@@ -32,6 +36,8 @@ public class PostActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_READ_STORAGE = 100;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private FirebaseAuth firebaseAuth;
private ProgressDialog mProgress;
......@@ -51,6 +57,8 @@ public class PostActivity extends AppCompatActivity {
mSubmitBtn = (Button) findViewById(R.id.submitBtn);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
firebaseAuth = FirebaseAuth.getInstance();
mProgress = new ProgressDialog(this);
......@@ -75,8 +83,8 @@ public class PostActivity extends AppCompatActivity {
mProgress.setMessage("Posting to Blog ... ");
String title_val = mPostTitle.getText().toString().trim();
String desc_val = mPostDesc.getText().toString().trim();
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {
mProgress.show();
......@@ -86,7 +94,17 @@ public class PostActivity extends AppCompatActivity {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("image").setValue(downloadUrl.toString());
newPost.child("uid").setValue(firebaseAuth.getCurrentUser());
mProgress.dismiss();
startActivity(new Intent(PostActivity.this, Main2Activity.class));
Toast.makeText(PostActivity.this, "Success!", Toast.LENGTH_LONG).show();
}
});
}
......
......@@ -35,7 +35,7 @@ public class ProfileActivity extends AppCompatActivity implements View.OnClickLi
startActivity(new Intent(this,LoginActivity.class));
}
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference = FirebaseDatabase.getInstance().getReference().child("UserInfo");
editTextAddress = (EditText) findViewById(R.id.editTextAddress);
editTextName = (EditText)findViewById(R.id.editTextName);
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="@+id/post_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="15dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/add_btn" />
<TextView
android:id="@+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="post title in here" />
<TextView
android:id="@+id/post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="post description" />
</LinearLayout>
</android.support.v7.widget.CardView>
\ No newline at end of file
......@@ -4,4 +4,9 @@
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/blog_list"></android.support.v7.widget.RecyclerView>
</LinearLayout>
\ No newline at end of file
......