Home fregment done!

except onclick event
This commit is contained in:
Farbod 2021-12-11 17:57:16 +04:00
parent 0166e11745
commit f735a69dc2
13 changed files with 236 additions and 8 deletions

1
.idea/gradle.xml generated
View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

1
.idea/misc.xml generated
View File

@ -7,6 +7,7 @@
<entry key="app/src/main/res/layout/fragment_home.xml" value="0.1" />
<entry key="app/src/main/res/layout/fragment_library.xml" value="0.11956521739130435" />
<entry key="app/src/main/res/layout/fragment_subscriptions.xml" value="0.1" />
<entry key="app/src/main/res/layout/trending_row.xml" value="0.33" />
<entry key="app/src/main/res/menu/bottom_menu.xml" value="0.15520833333333334" />
</map>
</option>

View File

@ -42,4 +42,8 @@ dependencies {
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.squareup.picasso:picasso:2.8'
implementation 'de.hdodenhof:circleimageview:3.1.0'
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xyz.btcland.libretube">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -5,6 +5,16 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import okhttp3.*
import java.io.IOException
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@ -33,8 +43,21 @@ class Home : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<RecyclerView>(R.id.recview)
recyclerView.layoutManager = GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items))
val progressbar = view.findViewById<ProgressBar>(R.id.progressBar)
fetchJson(progressbar,recyclerView)
}
companion object {
@ -56,4 +79,42 @@ class Home : Fragment() {
}
}
}
}
private fun fetchJson(progressBar: ProgressBar, recyclerView: RecyclerView) {
val client = OkHttpClient()
fun run() {
val request = Request.Builder()
.url("https://pipedapi.kavin.rocks/trending?region=US")
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val body = response.body!!.string()
println(body)
val gson = GsonBuilder().create()
val itemType = object : TypeToken<List<Trending>>() {}.type
val trendingList = gson.fromJson<List<Trending>>(body, itemType)
runOnUiThread {
progressBar.visibility = View.GONE
recyclerView.adapter = TrendingAdapter(trendingList)
}
}
}
})
}
run()
}
fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -0,0 +1,14 @@
package xyz.btcland.libretube
data class Trending(
val url: String,
val title: String,
val thumbnail: String,
val uploaderName: String,
val uploaderUrl:String,
val uploaderAvatar:String,
val uploadedDate: String,
val duration: Int,
val views: Int,
val uploaderVerified: Boolean
)

View File

@ -0,0 +1,55 @@
package xyz.btcland.libretube
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import java.math.BigDecimal
import java.math.RoundingMode
class TrendingAdapter(private val trendingFeed: List<Trending>): RecyclerView.Adapter<CustomViewHolder>() {
override fun getItemCount(): Int {
return trendingFeed.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.trending_row,parent,false)
return CustomViewHolder(cell)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val trending = trendingFeed[position]
holder.v.findViewById<TextView>(R.id.textView_title).text = trending.title
holder.v.findViewById<TextView>(R.id.textView_channel).text = trending.uploaderName +""+ videoViews(trending.views)+""+trending.uploadedDate
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.thumbnail)
val channelImage = holder.v.findViewById<ImageView>(R.id.channel_image)
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
}
}
class CustomViewHolder(val v: View): RecyclerView.ViewHolder(v){
}
fun videoViews(views: Int): String{
when {
views<1000 -> {
return views.toString()
}
views in 1000..999999 -> {
val decimal = BigDecimal(views/1000).setScale(0, RoundingMode.HALF_EVEN)
return decimal.toString()+"K"
}
views in 1000000..10000000 -> {
val decimal = BigDecimal(views/1000000).setScale(0, RoundingMode.HALF_EVEN)
return decimal.toString()+"M"
}
else -> {
val decimal = BigDecimal(views/1000000).setScale(0, RoundingMode.HALF_EVEN)
return decimal.toString()+"M"
}
}
}

View File

@ -0,0 +1,6 @@
package xyz.btcland.libretube
data class Video(
val id: Int,
)

View File

@ -1,14 +1,31 @@
<?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=".Home">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="home" />
</FrameLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Title"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/thumbnail"
app:layout_constraintStart_toEndOf="@+id/channel_image"
app:layout_constraintTop_toBottomOf="@+id/thumbnail" />
<TextView
android:id="@+id/textView_channel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Channel Name"
app:layout_constraintEnd_toEndOf="@+id/textView_title"
app:layout_constraintStart_toStartOf="@+id/textView_title"
app:layout_constraintTop_toBottomOf="@+id/textView_title"
android:paddingBottom="16dp"
/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/channel_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thumbnail"
app:srcCompat="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="grid_items">2</integer>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="grid_items">4</integer>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="grid_items">1</integer>
</resources>