Skip to content

Commit

Permalink
HomePage Activities, Adapters, RecycleView
Browse files Browse the repository at this point in the history
  • Loading branch information
NMsby committed Nov 8, 2024
1 parent 17d1325 commit fd28a1b
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 15 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/org/myd/instagramclone/adapters/PostAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.myd.instagramclone.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.github.marlonlom.utilities.timeago.TimeAgo
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.firestore.toObject
import com.google.firebase.ktx.Firebase
import org.myd.instagramclone.R
import org.myd.instagramclone.databinding.PostRvBinding
import org.myd.instagramclone.models.Post
import org.myd.instagramclone.models.User
import org.myd.instagramclone.utils.USER_NODE


class PostAdapter(var context : Context, private var postList : ArrayList<Post>) : RecyclerView.Adapter<PostAdapter.MyHolder>() {
inner class MyHolder(var binding : PostRvBinding) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val binding = PostRvBinding.inflate(LayoutInflater.from(context), parent, false)
return MyHolder(binding)
}

override fun getItemCount(): Int {
return postList.size
}

override fun onBindViewHolder(holder: MyHolder, position: Int) {
Firebase.firestore.collection(USER_NODE).document(postList[position].uid).get().addOnSuccessListener {
val user = it.toObject<User>()
Glide.with(context).load(user!!.image).placeholder(R.drawable.user).into(holder.binding.profileImage)
holder.binding.name.text = user.name
}
Glide.with(context).load(postList[position].postUrl).placeholder(R.drawable.loading).into(holder.binding.postImage)

val text = TimeAgo.using(postList.get(position).time.toLong())

holder.binding.time.text = text
holder.binding.caption.text = postList[position].caption
holder.binding.like.setOnClickListener {
holder.binding.like.setImageResource(R.drawable.red_like)
}

}
}
34 changes: 34 additions & 0 deletions app/src/main/java/org/myd/instagramclone/adapters/ReelAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.myd.instagramclone.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import org.myd.instagramclone.R
import org.myd.instagramclone.databinding.ReelDgBinding
import org.myd.instagramclone.models.Reel

class ReelAdapter(var context: Context, var reelList:ArrayList<Reel>) : RecyclerView.Adapter<ReelAdapter.ViewHolder>() {
inner class ViewHolder(var binding: ReelDgBinding) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var binding = ReelDgBinding.inflate(LayoutInflater.from(context), parent, false)
return ViewHolder(binding)
}

override fun getItemCount(): Int {
return reelList.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Picasso.get().load(reelList[position].profileLink).placeholder(R.drawable.user).into(holder.binding.profileImage)
holder.binding.caption.text = reelList[position].caption
holder.binding.videoView.setVideoPath(reelList[position].reelUrl)
holder.binding.videoView.setOnPreparedListener {
holder.binding.progressBar.visibility = View.GONE
holder.binding.videoView.start()
}
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/org/myd/instagramclone/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.firestore.toObject
import com.google.firebase.ktx.Firebase
import org.myd.instagramclone.R
import org.myd.instagramclone.adapters.PostAdapter
import org.myd.instagramclone.databinding.FragmentHomeBinding
import org.myd.instagramclone.models.Post
import org.myd.instagramclone.utils.POST

class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private var postList = ArrayList<Post>()
private lateinit var adapter : PostAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -25,8 +34,24 @@ class HomeFragment : Fragment() {
): View? {
// Inflate the layout for this fragment
binding = FragmentHomeBinding.inflate(inflater, container, false)
adapter = PostAdapter(requireContext(), postList)
binding.postRv.layoutManager = LinearLayoutManager(requireContext())
binding.postRv.adapter = adapter

setHasOptionsMenu(true)
(requireActivity() as AppCompatActivity).setSupportActionBar(binding.materialToolbar2)

Firebase.firestore.collection(POST).get().addOnSuccessListener {
var tempList = ArrayList<Post>()
postList.clear()
for (i in it.documents) {
var post : Post = i.toObject<Post>()!!
tempList.add(post)
}
postList.addAll(tempList)
adapter.notifyDataSetChanged()
}

return binding.root
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/myd/instagramclone/models/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package org.myd.instagramclone.models
class Post {
var postUrl: String = ""
var caption: String = ""
var uid: String = ""
var time :String = ""
constructor()
constructor(postUrl: String, caption: String) {
this.postUrl = postUrl
this.caption = caption
}

constructor(postUrl: String, caption: String, uid: String, time: String) {
this.postUrl = postUrl
this.caption = caption
this.uid = uid
this.time = time
}

}
30 changes: 22 additions & 8 deletions app/src/main/java/org/myd/instagramclone/post/PostActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.firebase.Firebase
import com.google.firebase.auth.auth
import com.google.firebase.firestore.firestore
import com.google.firebase.auth.ktx.auth

import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.firestore.ktx.toObject
import com.google.firebase.ktx.Firebase
import org.myd.instagramclone.HomeActivity
import org.myd.instagramclone.R
import org.myd.instagramclone.databinding.ActivityPostBinding
import org.myd.instagramclone.models.Post
import org.myd.instagramclone.models.User
import org.myd.instagramclone.utils.POST
import org.myd.instagramclone.utils.POST_FOLDER
import org.myd.instagramclone.utils.USER_NODE
import org.myd.instagramclone.utils.uploadImage

class PostActivity : AppCompatActivity() {
Expand Down Expand Up @@ -63,12 +67,22 @@ class PostActivity : AppCompatActivity() {
}

binding.postButton.setOnClickListener {
val post = Post(imageUrl!!, binding.caption.editText?.text.toString())
Firebase.firestore.collection(USER_NODE).document().get()
.addOnSuccessListener {
var user = it.toObject<User>()!!
val post: Post = Post(
postUrl = imageUrl!!,
caption = binding.caption.editText?.text.toString(),
uid = Firebase.auth.currentUser!!.uid,
time = System.currentTimeMillis().toString()
)

Firebase.firestore.collection(POST).document().set(post).addOnSuccessListener {
Firebase.firestore.collection(Firebase.auth.currentUser!!.uid).document().set(post).addOnSuccessListener {
startActivity(Intent(this@PostActivity, HomeActivity::class.java))
finish()
Firebase.firestore.collection(POST).document().set(post).addOnSuccessListener {
Firebase.firestore.collection(Firebase.auth.currentUser!!.uid).document()
.set(post).addOnSuccessListener {
startActivity(Intent(this@PostActivity, HomeActivity::class.java))
finish()
}
}
}
}
Expand Down
Binary file added app/src/main/res/drawable/chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/like.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/red_like.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/save.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 87 additions & 7 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,94 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout 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=".fragments.HomeFragment">
tools:context=".fragments.HomeFragment" >

<!-- TODO: Update blank fragment layout -->
<TextView
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/materialToolbar2"
style="@style/Base.Theme.InstagramClone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the HomeFragment" />
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Instagram"
app:titleMarginStart="30dp" />

</FrameLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialToolbar2">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/user"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="20dp"
android:layout_height="20dp"
android:contentDescription="@string/todo"
android:src="@android:drawable/ic_menu_add"
app:backgroundTint="#4788F6"
app:layout_constraintBottom_toBottomOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="@+id/imageView3"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:layout_constraintVertical_bias="0.14" />

<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nestedScrollView"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/post_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"
tools:listitem="@layout/post_rv" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit fd28a1b

Please sign in to comment.