한윤범

posting function added

...@@ -36,6 +36,7 @@ dependencies { ...@@ -36,6 +36,7 @@ dependencies {
36 compile 'com.google.firebase:firebase-crash:11.0.2' 36 compile 'com.google.firebase:firebase-crash:11.0.2'
37 compile 'com.firebaseui:firebase-ui-database:0.4.0' 37 compile 'com.firebaseui:firebase-ui-database:0.4.0'
38 compile 'com.android.support:design:25.3.1' 38 compile 'com.android.support:design:25.3.1'
39 + compile 'com.squareup.picasso:picasso:2.5.2'
39 testCompile 'junit:junit:4.12' 40 testCompile 'junit:junit:4.12'
40 } 41 }
41 42
......
1 +package com.example.user.firebaseauthdemo;
2 +
3 +/**
4 + * Created by HYB on 2017. 10. 13..
5 + */
6 +
7 +public class Blog
8 +{
9 + private String title;
10 + private String desc;
11 + private String image;
12 +
13 + public Blog(){
14 +
15 + }
16 +
17 + public Blog(String title, String desc, String image) {
18 + this.title = title;
19 + this.desc = desc;
20 + this.image = image;
21 + }
22 +
23 + public String getDesc() {
24 + return desc;
25 + }
26 +
27 + public void setDesc(String desc) {
28 + this.desc = desc;
29 + }
30 +
31 + public String getImage() {
32 + return image;
33 + }
34 +
35 + public void setImage(String image) {
36 + this.image = image;
37 + }
38 +
39 + public String getTitle() {
40 + return title;
41 + }
42 +
43 + public void setTitle(String title) {
44 + this.title = title;
45 + }
46 +
47 +}
1 package com.example.user.firebaseauthdemo; 1 package com.example.user.firebaseauthdemo;
2 2
3 +import android.content.Context;
3 import android.content.Intent; 4 import android.content.Intent;
4 import android.os.Bundle; 5 import android.os.Bundle;
5 import android.support.annotation.Nullable; 6 import android.support.annotation.Nullable;
6 import android.support.v4.app.Fragment; 7 import android.support.v4.app.Fragment;
8 +import android.support.v7.widget.LinearLayoutManager;
9 +import android.support.v7.widget.RecyclerView;
7 import android.view.LayoutInflater; 10 import android.view.LayoutInflater;
8 import android.view.Menu; 11 import android.view.Menu;
9 import android.view.MenuInflater; 12 import android.view.MenuInflater;
10 import android.view.MenuItem; 13 import android.view.MenuItem;
11 import android.view.View; 14 import android.view.View;
12 import android.view.ViewGroup; 15 import android.view.ViewGroup;
16 +import android.widget.ImageView;
17 +import android.widget.TextView;
18 +
19 +import com.firebase.ui.database.FirebaseRecyclerAdapter;
20 +import com.google.firebase.database.DatabaseReference;
21 +import com.google.firebase.database.FirebaseDatabase;
22 +import com.squareup.picasso.Picasso;
13 23
14 /** 24 /**
15 * Created by HYB on 2017. 10. 11.. 25 * Created by HYB on 2017. 10. 11..
...@@ -17,18 +27,79 @@ import android.view.ViewGroup; ...@@ -17,18 +27,79 @@ import android.view.ViewGroup;
17 27
18 public class Community extends Fragment { 28 public class Community extends Fragment {
19 29
30 + private RecyclerView mBlogList;
31 + private RecyclerView.LayoutManager layoutManager;
32 + private DatabaseReference mDatabase;
20 33
21 @Nullable 34 @Nullable
22 @Override 35 @Override
23 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 36 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
24 setHasOptionsMenu(true); 37 setHasOptionsMenu(true);
25 - return inflater.inflate(R.layout.community, container, false); 38 +
39 + View view = inflater.inflate(R.layout.community, container, false);
40 +
41 + mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
42 +
43 + mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);
44 + mBlogList.setHasFixedSize(true);
45 + mBlogList.setLayoutManager(new LinearLayoutManager(getActivity()));
46 +
47 + return view;
48 + }
49 +
50 + @Override
51 + public void onStart() {
52 + super.onStart();
53 +
54 + FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
55 +
56 + Blog.class,
57 + R.layout.blog_row,
58 + BlogViewHolder.class,
59 + mDatabase
60 + ) {
61 + @Override
62 + protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
63 + viewHolder.setTitle(model.getTitle());
64 + viewHolder.setDesc(model.getDesc());
65 + viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
66 + }
67 + };
68 + mBlogList.setAdapter(firebaseRecyclerAdapter);
69 + }
70 +
71 + public static class BlogViewHolder extends RecyclerView.ViewHolder{
72 +
73 + View mView;
74 +
75 + public BlogViewHolder(View itemView) {
76 + super(itemView);
77 +
78 + mView = itemView;
79 + }
80 + public void setTitle(String title){
81 + TextView post_title = (TextView) mView.findViewById(R.id.post_title);
82 + post_title.setText(title);
83 + }
84 +
85 + public void setDesc(String desc){
86 + TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
87 + post_desc.setText(desc);
88 + }
89 +
90 + public void setImage(Context ctx, String image){
91 + ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
92 + Picasso.with(ctx).load(image).into(post_image);
93 +
94 + }
95 +
26 } 96 }
27 97
28 @Override 98 @Override
29 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 99 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
30 inflater.inflate(R.menu.community_menu, menu); 100 inflater.inflate(R.menu.community_menu, menu);
31 super.onCreateOptionsMenu(menu, inflater); 101 super.onCreateOptionsMenu(menu, inflater);
102 +
32 } 103 }
33 104
34 @Override 105 @Override
......
...@@ -12,8 +12,12 @@ import android.view.View; ...@@ -12,8 +12,12 @@ import android.view.View;
12 import android.widget.Button; 12 import android.widget.Button;
13 import android.widget.EditText; 13 import android.widget.EditText;
14 import android.widget.ImageButton; 14 import android.widget.ImageButton;
15 +import android.widget.Toast;
15 16
16 import com.google.android.gms.tasks.OnSuccessListener; 17 import com.google.android.gms.tasks.OnSuccessListener;
18 +import com.google.firebase.auth.FirebaseAuth;
19 +import com.google.firebase.database.DatabaseReference;
20 +import com.google.firebase.database.FirebaseDatabase;
17 import com.google.firebase.storage.FirebaseStorage; 21 import com.google.firebase.storage.FirebaseStorage;
18 import com.google.firebase.storage.StorageReference; 22 import com.google.firebase.storage.StorageReference;
19 import com.google.firebase.storage.UploadTask; 23 import com.google.firebase.storage.UploadTask;
...@@ -32,6 +36,8 @@ public class PostActivity extends AppCompatActivity { ...@@ -32,6 +36,8 @@ public class PostActivity extends AppCompatActivity {
32 private static final int PERMISSIONS_REQUEST_READ_STORAGE = 100; 36 private static final int PERMISSIONS_REQUEST_READ_STORAGE = 100;
33 37
34 private StorageReference mStorage; 38 private StorageReference mStorage;
39 + private DatabaseReference mDatabase;
40 + private FirebaseAuth firebaseAuth;
35 41
36 private ProgressDialog mProgress; 42 private ProgressDialog mProgress;
37 43
...@@ -51,6 +57,8 @@ public class PostActivity extends AppCompatActivity { ...@@ -51,6 +57,8 @@ public class PostActivity extends AppCompatActivity {
51 mSubmitBtn = (Button) findViewById(R.id.submitBtn); 57 mSubmitBtn = (Button) findViewById(R.id.submitBtn);
52 58
53 mStorage = FirebaseStorage.getInstance().getReference(); 59 mStorage = FirebaseStorage.getInstance().getReference();
60 + mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
61 + firebaseAuth = FirebaseAuth.getInstance();
54 62
55 mProgress = new ProgressDialog(this); 63 mProgress = new ProgressDialog(this);
56 64
...@@ -75,8 +83,8 @@ public class PostActivity extends AppCompatActivity { ...@@ -75,8 +83,8 @@ public class PostActivity extends AppCompatActivity {
75 83
76 mProgress.setMessage("Posting to Blog ... "); 84 mProgress.setMessage("Posting to Blog ... ");
77 85
78 - String title_val = mPostTitle.getText().toString().trim(); 86 + final String title_val = mPostTitle.getText().toString().trim();
79 - String desc_val = mPostDesc.getText().toString().trim(); 87 + final String desc_val = mPostDesc.getText().toString().trim();
80 88
81 if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) { 89 if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {
82 mProgress.show(); 90 mProgress.show();
...@@ -86,7 +94,17 @@ public class PostActivity extends AppCompatActivity { ...@@ -86,7 +94,17 @@ public class PostActivity extends AppCompatActivity {
86 @Override 94 @Override
87 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 95 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
88 Uri downloadUrl = taskSnapshot.getDownloadUrl(); 96 Uri downloadUrl = taskSnapshot.getDownloadUrl();
97 +
98 + DatabaseReference newPost = mDatabase.push();
99 + newPost.child("title").setValue(title_val);
100 + newPost.child("desc").setValue(desc_val);
101 + newPost.child("image").setValue(downloadUrl.toString());
102 + newPost.child("uid").setValue(firebaseAuth.getCurrentUser());
103 +
89 mProgress.dismiss(); 104 mProgress.dismiss();
105 +
106 + startActivity(new Intent(PostActivity.this, Main2Activity.class));
107 + Toast.makeText(PostActivity.this, "Success!", Toast.LENGTH_LONG).show();
90 } 108 }
91 }); 109 });
92 } 110 }
......
...@@ -35,7 +35,7 @@ public class ProfileActivity extends AppCompatActivity implements View.OnClickLi ...@@ -35,7 +35,7 @@ public class ProfileActivity extends AppCompatActivity implements View.OnClickLi
35 startActivity(new Intent(this,LoginActivity.class)); 35 startActivity(new Intent(this,LoginActivity.class));
36 } 36 }
37 37
38 - databaseReference = FirebaseDatabase.getInstance().getReference(); 38 + databaseReference = FirebaseDatabase.getInstance().getReference().child("UserInfo");
39 39
40 editTextAddress = (EditText) findViewById(R.id.editTextAddress); 40 editTextAddress = (EditText) findViewById(R.id.editTextAddress);
41 editTextName = (EditText)findViewById(R.id.editTextName); 41 editTextName = (EditText)findViewById(R.id.editTextName);
......
1 +<?xml version="1.0" encoding="utf-8"?>
2 +<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
3 + xmlns:app="http://schemas.android.com/apk/res-auto"
4 + android:layout_width="match_parent" android:layout_height="wrap_content"
5 + android:layout_margin="20dp">
6 +
7 + <LinearLayout
8 + android:layout_width="match_parent"
9 + android:layout_height="wrap_content"
10 + android:orientation="vertical"
11 + android:padding="0dp">
12 +
13 + <ImageView
14 + android:id="@+id/post_image"
15 + android:layout_width="match_parent"
16 + android:layout_height="wrap_content"
17 + android:adjustViewBounds="true"
18 + android:padding="15dp"
19 + android:scaleType="centerCrop"
20 + app:srcCompat="@mipmap/add_btn" />
21 +
22 + <TextView
23 + android:id="@+id/post_title"
24 + android:layout_width="match_parent"
25 + android:layout_height="wrap_content"
26 + android:padding="15dp"
27 + android:text="post title in here" />
28 +
29 + <TextView
30 + android:id="@+id/post_desc"
31 + android:layout_width="match_parent"
32 + android:layout_height="wrap_content"
33 + android:padding="10dp"
34 + android:paddingLeft="5dp"
35 + android:paddingRight="5dp"
36 + android:text="post description" />
37 + </LinearLayout>
38 +</android.support.v7.widget.CardView>
...\ No newline at end of file ...\ No newline at end of file
...@@ -4,4 +4,9 @@ ...@@ -4,4 +4,9 @@
4 android:layout_height="match_parent" 4 android:layout_height="match_parent"
5 android:orientation="horizontal"> 5 android:orientation="horizontal">
6 6
7 + <android.support.v7.widget.RecyclerView
8 + android:layout_width="match_parent"
9 + android:layout_height="match_parent"
10 + android:id="@+id/blog_list"></android.support.v7.widget.RecyclerView>
11 +
7 </LinearLayout> 12 </LinearLayout>
...\ No newline at end of file ...\ No newline at end of file
......