Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit f9177f5

Browse files
committed
[ADDED] [#102] Data binding for the resources list items.
1 parent 625fefe commit f9177f5

File tree

8 files changed

+85
-38
lines changed

8 files changed

+85
-38
lines changed

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
android:name=".ui.functionaldemo.TedTalkPlaybackActivity"
4545
android:parentActivityName=".ui.browse.LayoutBrowseActivity" />
4646
<activity
47+
android:label="@string/additional_resource_title"
4748
android:name=".ui.resource.LearningResourceActivity"
4849
android:parentActivityName=".ui.browse.LayoutBrowseActivity" />
4950
</application>

app/src/main/java/com/hossainkhan/android/demo/data/FirestoreDateFormatter.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616

1717
package com.hossainkhan.android.demo.data
1818

19+
import com.google.firebase.Timestamp
1920
import java.text.SimpleDateFormat
2021
import java.util.Date
2122
import java.util.Locale
2223

2324

2425
object FirestoreDateFormatter {
25-
fun date(timestamp: Long): String {
26+
fun date(timestamp: Timestamp): String {
2627
val sfd = SimpleDateFormat("EEEE, MMMM d", Locale.CANADA)
27-
return sfd.format(Date(timestamp * 1000)) // NOTE: x1000 is Firebase specific conversion
28+
return sfd.format(timestamp.toDate())
2829
}
2930

30-
fun time(timestamp: Long): String {
31-
val sfd = SimpleDateFormat("hh:mm aa", Locale.CANADA)
32-
return sfd.format(Date(timestamp * 1000)) // NOTE: x1000 is Firebase specific conversion
31+
fun date(date: Date): String {
32+
val sfd = SimpleDateFormat("MMMM d, yyyy", Locale.CANADA)
33+
return sfd.format(date)
3334
}
3435
}

app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt

+17-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,26 @@
1616

1717
package com.hossainkhan.android.demo.data
1818

19+
import com.google.firebase.Timestamp
20+
import java.util.*
21+
1922

2023
data class ResourceInfo(
21-
val author: String,
22-
val summary: String,
23-
val title: String,
24-
val event: String,
25-
val url: String,
26-
val publish_date: Timestamp,
24+
var author: String = "",
25+
var summary: String = "",
26+
var title: String = "",
27+
var event: String = "",
28+
var url: String = "",
29+
var publish_date: Timestamp? = null,
2730
/**
2831
* Possible values
2932
* "blog", "techtalk"
3033
*/
31-
val type: String = "techtalk"
32-
)
34+
var type: String = "techtalk"
35+
) {
36+
val formattedDate: String
37+
get() {
38+
val date = publish_date?.toDate() ?: Date()
39+
return FirestoreDateFormatter.date(date)
40+
}
41+
}

app/src/main/java/com/hossainkhan/android/demo/di/DataStoreModule.kt

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.hossainkhan.android.demo.di
1919
import android.content.Context
2020
import android.content.SharedPreferences
2121
import android.content.res.Resources
22+
import com.google.firebase.firestore.FirebaseFirestore
2223
import dagger.Module
2324
import dagger.Provides
2425

@@ -33,4 +34,9 @@ class DataStoreModule {
3334
internal fun provideAndroidResoures(context: Context): Resources {
3435
return context.resources
3536
}
37+
38+
@Provides
39+
internal fun provideFirestore(context: Context): FirebaseFirestore {
40+
return FirebaseFirestore.getInstance()
41+
}
3642
}

app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.hossainkhan.android.demo.ui.resource
1818

19+
import android.content.ActivityNotFoundException
20+
import android.content.Intent
21+
import android.net.Uri
1922
import androidx.appcompat.app.AppCompatActivity
2023
import android.os.Bundle
2124
import androidx.databinding.DataBindingUtil
@@ -24,6 +27,7 @@ import androidx.lifecycle.ViewModelProvider
2427
import androidx.recyclerview.widget.LinearLayoutManager
2528
import androidx.recyclerview.widget.RecyclerView
2629
import com.hossainkhan.android.demo.R
30+
import com.hossainkhan.android.demo.data.ResourceInfo
2731
import com.hossainkhan.android.demo.databinding.ActivityLearningResourceBinding
2832
import com.hossainkhan.android.demo.viewmodel.ViewModelProviderFactory
2933
import dagger.android.AndroidInjection
@@ -52,7 +56,7 @@ class LearningResourceActivity : AppCompatActivity() {
5256

5357
val ideaListAdapter = ResourceListAdapter { data ->
5458
Timber.d("Item Clicked: $data")
55-
viewModel.onItemClicked(data)
59+
openContent(data)
5660
}
5761

5862
ideaList.apply {
@@ -65,4 +69,17 @@ class LearningResourceActivity : AppCompatActivity() {
6569
ideaListAdapter.submitList(result)
6670
})
6771
}
72+
73+
private fun openContent(resourceInfo: ResourceInfo) {
74+
// TODO: Update logic later when the URL is not only youtube.
75+
val intentApp = Intent(Intent.ACTION_VIEW,
76+
Uri.parse("vnd.youtube:" + resourceInfo.url.split("=")[1]))
77+
78+
val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse(resourceInfo.url))
79+
try {
80+
startActivity(intentApp)
81+
} catch (ex: ActivityNotFoundException) {
82+
startActivity(intentBrowser)
83+
}
84+
}
6885
}

app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceViewModel.kt

+22-16
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,35 @@ package com.hossainkhan.android.demo.ui.resource
1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.ViewModel
22+
import com.google.firebase.firestore.FirebaseFirestore
23+
import com.google.firebase.firestore.Query
2224
import com.hossainkhan.android.demo.data.ResourceInfo
25+
import timber.log.Timber
2326
import javax.inject.Inject
2427

25-
class LearningResourceViewModel @Inject constructor() : ViewModel() {
28+
class LearningResourceViewModel @Inject constructor(
29+
firestore: FirebaseFirestore
30+
) : ViewModel() {
2631
private val _data = MutableLiveData<List<ResourceInfo>>()
2732
val data: LiveData<List<ResourceInfo>> = _data
2833

29-
private val sampleData = mutableListOf<ResourceInfo>()
34+
private val resourceData = mutableListOf<ResourceInfo>()
3035

3136
init {
32-
generateSampleDataSet()
33-
_data.value = sampleData.toList()
34-
}
35-
36-
private fun generateSampleDataSet() {
37-
for (i in 1..20) {
38-
//sampleData.add(ResourceInfo(i))
39-
}
40-
}
41-
42-
43-
44-
fun onItemClicked(item: ResourceInfo) {
45-
37+
Timber.i("Loading data from firestore: %s", firestore)
38+
firestore.collection("external-resources")
39+
.orderBy("publish_date", Query.Direction.DESCENDING)
40+
.get()
41+
.addOnSuccessListener { result ->
42+
for (document in result) {
43+
val x = document.toObject(ResourceInfo::class.java)
44+
Timber.i("Resource: $x")
45+
resourceData.add(x)
46+
}
47+
_data.value = resourceData
48+
}
49+
.addOnFailureListener { exception ->
50+
Timber.w(exception, "Error getting documents.")
51+
}
4652
}
4753
}

app/src/main/res/layout/list_item_resource_tech_talk.xml

+12-8
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,35 @@
5050
android:layout_width="0dp"
5151
android:layout_height="wrap_content"
5252
android:layout_marginTop="@dimen/material_content_distance_small"
53-
android:text="In this session you will learn best practices of using ConstraintLayout on Android, particularly covering tips and tricks in the Layout Editor and new features introduced in the 2.0 version. We will go over how to take advantage of those to create UI more efficiently."
53+
android:text="@{data.summary}"
5454
app:layout_constraintEnd_toStartOf="@+id/action_play"
5555
app:layout_constraintStart_toStartOf="@+id/guideline_vertical_start"
56-
app:layout_constraintTop_toBottomOf="@+id/resource_author" />
56+
app:layout_constraintTop_toBottomOf="@+id/resource_author"
57+
tools:text="In this session you will learn best practices of using ConstraintLayout on Android, particularly covering tips and tricks in the Layout Editor and new features introduced in the 2.0 version. We will go over how to take advantage of those to create UI more efficiently." />
5758

5859
<TextView
5960
android:id="@+id/resource_date"
6061
android:layout_width="wrap_content"
6162
android:layout_height="wrap_content"
6263
android:layout_marginTop="@dimen/material_content_distance_small"
6364
android:layout_marginBottom="@dimen/material_vertical_margin"
64-
android:text="December 2, 2018"
65+
android:text="@{data.formattedDate}"
6566
app:layout_constraintBottom_toBottomOf="parent"
6667
app:layout_constraintStart_toStartOf="@+id/guideline_vertical_start"
67-
app:layout_constraintTop_toBottomOf="@+id/resource_summary" />
68+
app:layout_constraintTop_toBottomOf="@+id/resource_summary"
69+
tools:text="December 2, 2018" />
6870

6971
<TextView
7072
android:id="@+id/resource_title"
7173
style="@style/TextAppearance.AppCompat.Title"
7274
android:layout_width="0dp"
7375
android:layout_height="wrap_content"
7476
android:layout_marginTop="@dimen/material_vertical_margin"
75-
android:text="This is the title of the talk."
77+
android:text="@{data.title}"
7678
app:layout_constraintEnd_toStartOf="@+id/action_play"
7779
app:layout_constraintStart_toStartOf="@+id/guideline_vertical_start"
78-
app:layout_constraintTop_toTopOf="parent" />
80+
app:layout_constraintTop_toTopOf="parent"
81+
tools:text="This is the title of the talk." />
7982

8083

8184
<TextView
@@ -85,10 +88,11 @@
8588
android:layout_height="wrap_content"
8689
android:layout_marginTop="@dimen/material_content_distance_small"
8790
android:gravity="end"
88-
android:text="by Author of the talk."
91+
android:text="@{@string/tech_talk_author_and_venue(data.author, data.event)}"
8992
app:layout_constraintEnd_toStartOf="@+id/action_play"
9093
app:layout_constraintStart_toStartOf="@+id/guideline_vertical_start"
91-
app:layout_constraintTop_toBottomOf="@+id/resource_title" />
94+
app:layout_constraintTop_toBottomOf="@+id/resource_title"
95+
tools:text="by Author of the talk." />
9296

9397
<androidx.constraintlayout.widget.Guideline
9498
android:id="@+id/guideline_vertical_start"

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<string name="barrier_label_button_small">Action Label</string>
2323
<string name="barrier_label_button_long">Action Longer Translated Label</string>
2424

25+
<string name="additional_resource_title">External Resources</string>
26+
<string name="tech_talk_author_and_venue">by %1$s at %2$s</string>
27+
2528
</resources>

0 commit comments

Comments
 (0)