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

Commit cf79713

Browse files
committed
[UPDATE] [#102] Refined list item with author info.
1 parent a227476 commit cf79713

File tree

6 files changed

+91
-21
lines changed

6 files changed

+91
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.data
18+
19+
import java.text.SimpleDateFormat
20+
import java.util.Date
21+
import java.util.Locale
22+
23+
24+
object FirestoreDateFormatter {
25+
fun date(timestamp: Long): String {
26+
val sfd = SimpleDateFormat("EEEE, MMMM d", Locale.CANADA)
27+
return sfd.format(Date(timestamp * 1000)) // NOTE: x1000 is Firebase specific conversion
28+
}
29+
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
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.data
18+
19+
20+
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,
27+
/**
28+
* Possible values
29+
* "blog", "techtalk"
30+
*/
31+
val type: String = "techtalk"
32+
)

app/src/main/java/com/hossainkhan/android/demo/data/ItemModel.kt renamed to app/src/main/java/com/hossainkhan/android/demo/data/Timestamp.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616

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

19-
data class ItemModel(val id: Int)
19+
/**
20+
* Internal dataclass to capture timestamp from Firebase Firestore
21+
* Will be deleted when actual data-model is received.
22+
*/
23+
data class Timestamp(var _seconds: Long, var _nanoseconds: Int)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ 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.hossainkhan.android.demo.data.ItemModel
22+
import com.hossainkhan.android.demo.data.ResourceInfo
2323
import javax.inject.Inject
2424

2525
class LearningResourceViewModel @Inject constructor() : ViewModel() {
26-
private val _data = MutableLiveData<List<ItemModel>>()
27-
val data: LiveData<List<ItemModel>> = _data
26+
private val _data = MutableLiveData<List<ResourceInfo>>()
27+
val data: LiveData<List<ResourceInfo>> = _data
2828

29-
private val sampleData = mutableListOf<ItemModel>()
29+
private val sampleData = mutableListOf<ResourceInfo>()
3030

3131
init {
3232
generateSampleDataSet()
@@ -35,13 +35,13 @@ class LearningResourceViewModel @Inject constructor() : ViewModel() {
3535

3636
private fun generateSampleDataSet() {
3737
for (i in 1..20) {
38-
sampleData.add(ItemModel(i))
38+
//sampleData.add(ResourceInfo(i))
3939
}
4040
}
4141

4242

4343

44-
fun onItemClicked(item: ItemModel) {
44+
fun onItemClicked(item: ResourceInfo) {
4545

4646
}
4747
}

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ import android.view.ViewGroup
2121
import androidx.databinding.DataBindingUtil
2222
import androidx.recyclerview.widget.DiffUtil
2323
import com.hossainkhan.android.demo.R
24-
import com.hossainkhan.android.demo.data.ItemModel
24+
import com.hossainkhan.android.demo.data.ResourceInfo
2525
import com.hossainkhan.android.demo.databinding.ListItemResourceTechTalkBinding
2626
import com.hossainkhan.android.demo.ui.common.DataBoundListAdapter
2727

2828
class ResourceListAdapter(
29-
private val itemClickCallback: ((ItemModel) -> Unit)?
30-
) : DataBoundListAdapter<ItemModel, ListItemResourceTechTalkBinding>(
31-
diffCallback = object : DiffUtil.ItemCallback<ItemModel>() {
32-
override fun areItemsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean {
33-
return oldItem.id == newItem.id
34-
}
29+
private val itemClickCallback: ((ResourceInfo) -> Unit)?
30+
) : DataBoundListAdapter<ResourceInfo, ListItemResourceTechTalkBinding>(
31+
diffCallback = object : DiffUtil.ItemCallback<ResourceInfo>() {
32+
override fun areItemsTheSame(oldItem: ResourceInfo, newItem: ResourceInfo): Boolean {
33+
return oldItem.url == newItem.url
34+
}
3535

36-
override fun areContentsTheSame(oldItem: ItemModel, newItem: ItemModel): Boolean {
37-
return oldItem == newItem
36+
override fun areContentsTheSame(oldItem: ResourceInfo, newItem: ResourceInfo): Boolean {
37+
return oldItem == newItem
38+
}
3839
}
39-
}
4040
) {
4141

4242
override fun createBinding(parent: ViewGroup): ListItemResourceTechTalkBinding {
4343
val binding = DataBindingUtil.inflate<ListItemResourceTechTalkBinding>(
44-
LayoutInflater.from(parent.context), R.layout.list_item_resource_tech_talk,
45-
parent, false
44+
LayoutInflater.from(parent.context), R.layout.list_item_resource_tech_talk,
45+
parent, false
4646
)
4747

4848
binding.actionPlay.setOnClickListener {
@@ -53,7 +53,7 @@ class ResourceListAdapter(
5353
return binding
5454
}
5555

56-
override fun bind(binding: ListItemResourceTechTalkBinding, item: ItemModel) {
56+
override fun bind(binding: ListItemResourceTechTalkBinding, item: ResourceInfo) {
5757
binding.data = item
5858
}
5959
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@
105105

106106
<variable
107107
name="data"
108-
type="com.hossainkhan.android.demo.data.ItemModel" />
108+
type="com.hossainkhan.android.demo.data.ResourceInfo" />
109109
</data>
110110
</layout>

0 commit comments

Comments
 (0)