Skip to content

Commit 0863b92

Browse files
working on Unit Test
1 parent c018cda commit 0863b92

File tree

21 files changed

+995
-50
lines changed

21 files changed

+995
-50
lines changed

.idea/jarRepositories.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ dependencies {
5959
implementation 'androidx.appcompat:appcompat:1.1.0'
6060
implementation 'com.google.android.material:material:1.1.0'
6161
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
62+
63+
// Test
6264
testImplementation 'junit:junit:4.+'
6365
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
6466
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
67+
testImplementation 'org.mockito:mockito-core:1.10.19'
68+
testImplementation "com.google.truth:truth:1.0.1"
69+
70+
71+
6572

6673
// Recycler View
6774
implementation "androidx.recyclerview:recyclerview:$recycler_version"

app/src/main/java/com/daniyalxdubizzle/androidtakehomeproject/adapters/home/ItemHomeAdapter.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import com.bumptech.glide.request.transition.Transition
1515
import com.daniyalxdubizzle.androidtakehomeproject.R
1616
import com.daniyalxdubizzle.androidtakehomeproject.data.model.response.ItemListResponse
1717
import com.daniyalxdubizzle.androidtakehomeproject.data.model.response.ItemResponse
18+
import com.daniyalxdubizzle.androidtakehomeproject.utilities.GeneralHelper
1819
import com.daniyalxdubizzle.androidtakehomeproject.utilities.GeneralHelper.Companion.loadImage
20+
import com.daniyalxdubizzle.androidtakehomeproject.utilities.capitalizeWords
21+
import org.w3c.dom.Text
1922
import javax.inject.Inject
2023

2124

@@ -28,12 +31,15 @@ class ItemHomeAdapter @Inject constructor(
2831

2932
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
3033
val name = itemView.findViewById(R.id.productName) as TextView
34+
val productPrice = itemView.findViewById(R.id.productPrice) as TextView
3135
val productImage = itemView.findViewById(R.id.productImage) as ImageView
3236
val CV_ITEM = itemView.findViewById(R.id.CV_ITEM) as CardView
37+
val productDate = itemView.findViewById(R.id.productDate) as TextView
3338

3439
fun bindItem(data: ItemListResponse) {
35-
name.text = data.name
36-
40+
name.text = data.name.capitalizeWords
41+
productPrice.text = data.price
42+
productDate.text = GeneralHelper.dateParse(data.created_at)
3743
loadImage(
3844
itemView.context,
3945
data.image_urls_thumbnails.get(0),

app/src/main/java/com/daniyalxdubizzle/androidtakehomeproject/ui/ItemDetailFragment.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
4949

5050

5151
private void setUp(ItemListResponse singleViewItem) {
52-
GeneralHelper.Companion.loadImage(getActivity() , singleViewItem.getImage_urls().get(0) , binding.IVITEM);
53-
52+
GeneralHelper.Companion.loadImage(getActivity(), singleViewItem.getImage_urls().get(0), binding.IVITEM);
53+
binding.TVITEMNAME.setText(singleViewItem.getName());
54+
binding.TVITEMPRICE.setText(singleViewItem.getPrice());
5455

5556
}
5657

app/src/main/java/com/daniyalxdubizzle/androidtakehomeproject/ui/MainActivity.kt

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.daniyalxdubizzle.androidtakehomeproject.ui
22

33

44
import android.os.Bundle
5+
import android.view.View
56
import androidx.activity.viewModels
67
import androidx.appcompat.app.AppCompatActivity
78
import androidx.databinding.DataBindingUtil
@@ -14,7 +15,9 @@ import com.daniyalxdubizzle.androidtakehomeproject.adapters.home.ItemHomeAdapter
1415
import com.daniyalxdubizzle.androidtakehomeproject.data.model.remote.ResponseEvent
1516
import com.daniyalxdubizzle.androidtakehomeproject.data.model.response.ItemListResponse
1617
import com.daniyalxdubizzle.androidtakehomeproject.databinding.ActivityMainBinding
18+
import com.daniyalxdubizzle.androidtakehomeproject.utilities.GeneralHelper
1719
import com.daniyalxdubizzle.androidtakehomeproject.viewmodels.ItemViewModel
20+
import com.facebook.shimmer.ShimmerFrameLayout
1821
import dagger.hilt.android.AndroidEntryPoint
1922

2023

@@ -26,44 +29,48 @@ class MainActivity : AppCompatActivity() {
2629
private lateinit var binding: ActivityMainBinding
2730
private lateinit var itemHomeAdapter: ItemHomeAdapter
2831

32+
2933
override fun onCreate(savedInstanceState: Bundle?) {
3034
super.onCreate(savedInstanceState)
3135
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
32-
3336
binding.RVItem.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
3437
binding.RVItem.setHasFixedSize(true)
3538

3639
itemViewModel.itemState.observe(this, Observer {
3740
when (it) {
3841

3942
is ResponseEvent.Loading -> {
43+
binding.shimmerViewContainer.startShimmer()
4044
}
4145

4246
is ResponseEvent.Failure -> {
4347
}
4448

4549
is ResponseEvent.Success -> {
46-
// itemHomeAdapter = ItemHomeAdapter(it.data!!, it.data.results)
50+
// itemHomeAdapter = ItemHomeAdapter(it.data!!, it.data.results)
51+
binding.shimmerViewContainer.stopShimmer()
52+
binding.shimmerViewContainer.visibility = View.GONE
4753

4854
binding.RVItem.adapter =
4955
ItemHomeAdapter(it.data!!) { itemDto: ItemListResponse, position: Int ->
5056
openDetailFragment(ItemDetailFragment.newInstance(itemDto))
57+
// binding.toolbar.visibility = View.GONE
58+
System.out.println("#########" + GeneralHelper.dateParse(itemDto.created_at))
5159
}
5260

53-
// binding.RVItem.adapter = itemHomeAdapter
61+
// binding.RVItem.adapter = itemHomeAdapter
5462
itemHomeAdapter.notifyDataSetChanged()
5563

5664
}
5765

5866
}
5967
})
60-
61-
6268
}
6369

70+
6471
private fun openDetailFragment(fragment: Fragment) {
6572
supportFragmentManager.beginTransaction().add(R.id.container, fragment)
66-
.addToBackStack(if (supportFragmentManager.backStackEntryCount == 0) "First" else null)
73+
.addToBackStack(if (supportFragmentManager.backStackEntryCount == 0) "First" else null)
6774
.commit()
6875
}
6976
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.daniyalxdubizzle.androidtakehomeproject.utilities
2+
3+
val String.capitalizeWords get() = this.toLowerCase().split(" ").joinToString(" ") { it.capitalize() }
4+

app/src/main/java/com/daniyalxdubizzle/androidtakehomeproject/utilities/GeneralHelper.kt

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.daniyalxdubizzle.androidtakehomeproject.utilities
22

33
import android.content.Context
4-
import android.view.View
4+
import android.text.format.DateUtils
55
import android.widget.ImageView
6+
import androidx.appcompat.app.AppCompatActivity
67
import com.bumptech.glide.Glide
78
import com.daniyalxdubizzle.androidtakehomeproject.R
89
import org.json.JSONObject
10+
import java.text.ParseException
11+
import java.text.SimpleDateFormat
12+
import java.util.*
13+
914

1015
class GeneralHelper {
1116

@@ -25,5 +30,35 @@ class GeneralHelper {
2530
.placeholder(R.drawable.ic_launcher_foreground)
2631
.into(view)
2732
}
33+
34+
fun dateParse(date: String): String? {
35+
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS")
36+
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
37+
try {
38+
val time: Long = sdf.parse(date).getTime()
39+
val now = System.currentTimeMillis()
40+
return DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS)
41+
.toString()
42+
} catch (e: ParseException) {
43+
e.printStackTrace()
44+
}
45+
return null
46+
}
47+
48+
49+
fun isDateParse(date: String): Boolean {
50+
try {
51+
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS")
52+
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
53+
54+
val time: Long = sdf.parse(date).getTime()
55+
val now = System.currentTimeMillis()
56+
return true
57+
} catch (e: ParseException) {
58+
return false
59+
}
60+
return false
61+
}
62+
2863
}
2964
}

app/src/main/res/drawable/cart.png

8.42 KB
Loading

app/src/main/res/drawable/heart.png

17 KB
Loading

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,47 @@
1414
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
1515
android:layout_width="match_parent"
1616
android:layout_height="match_parent"
17-
tools:context=".ui.MainActivity"
1817
android:orientation="vertical"
19-
>
18+
tools:context=".ui.MainActivity">
2019

2120
<FrameLayout
21+
android:id="@+id/container"
2222
android:layout_width="match_parent"
23+
android:layout_height="wrap_content" />
24+
25+
<include
26+
layout="@layout/toolbar"
27+
android:layout_width="match_parent"
28+
android:layout_height="?attr/actionBarSize"
29+
android:id="@+id/toolbar"/>
30+
31+
<View
32+
android:layout_width="match_parent"
33+
android:layout_height="1dp"
34+
android:background="@color/gray5"/>
35+
36+
37+
<com.facebook.shimmer.ShimmerFrameLayout
38+
android:id="@+id/shimmer_view_container"
39+
android:layout_width="wrap_content"
2340
android:layout_height="wrap_content"
24-
android:id="@+id/container"/>
41+
android:layout_gravity="center"
42+
android:layout_marginTop="@dimen/_10sdp"
43+
android:orientation="vertical"
44+
app:shimmer_duration="1000">
45+
46+
<include
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
layout="@layout/item_loading_shimmer"/>
50+
51+
</com.facebook.shimmer.ShimmerFrameLayout>
2552

2653
<androidx.recyclerview.widget.RecyclerView
2754
android:id="@+id/RV_item"
2855
android:layout_width="match_parent"
29-
android:layout_height="wrap_content" />
56+
android:layout_height="wrap_content"
57+
android:layout_marginTop="@dimen/_10sdp" />
3058

3159
</LinearLayout>
3260

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
android:id="@+id/TV_ITEMNAME"
6666
android:textColor="@color/black"
6767
android:textSize="@dimen/_17sdp"
68-
android:text="NoteBook"
6968
android:textStyle="bold"/>
7069

7170

@@ -88,7 +87,6 @@
8887
android:textSize="@dimen/_15sdp"
8988
android:textColor="@color/greeeen"
9089
android:id="@+id/TV_ITEMPRICE"
91-
android:text="AED 600"
9290
/>
9391
</LinearLayout>
9492

@@ -101,7 +99,7 @@
10199
<TextView
102100
android:layout_width="match_parent"
103101
android:layout_height="wrap_content"
104-
android:text="4.5 / 5"
102+
android:text="@string/item_rating"
105103
android:gravity="center"
106104
android:drawableLeft="@android:drawable/star_on"/>
107105

@@ -143,7 +141,7 @@
143141
android:layout_width="0dp"
144142
android:layout_height="wrap_content"
145143
android:layout_weight="1.5"
146-
android:gravity="right">
144+
android:gravity="end">
147145

148146
<androidx.cardview.widget.CardView
149147
android:layout_width="@dimen/_50sdp"
@@ -154,10 +152,11 @@
154152
android:layout_marginBottom="@dimen/_10sdp">
155153

156154
<ImageView
157-
android:layout_width="wrap_content"
158-
android:layout_height="wrap_content"
155+
android:layout_width="@dimen/_25sdp"
156+
android:layout_height="@dimen/_25sdp"
159157
android:layout_gravity="center"
160-
android:src="@android:drawable/star_big_on"/>
158+
android:src="@drawable/heart"
159+
android:contentDescription="Favorite" />
161160

162161

163162
</androidx.cardview.widget.CardView>
@@ -170,7 +169,7 @@
170169
android:layout_width="0dp"
171170
android:layout_height="wrap_content"
172171
android:layout_weight="1.5"
173-
android:gravity="right">
172+
android:gravity="end">
174173

175174
<androidx.cardview.widget.CardView
176175
android:layout_width="@dimen/_50sdp"
@@ -181,10 +180,11 @@
181180
android:layout_marginBottom="@dimen/_10sdp">
182181

183182
<ImageView
184-
android:layout_width="wrap_content"
185-
android:layout_height="wrap_content"
183+
android:layout_width="@dimen/_25sdp"
184+
android:layout_height="@dimen/_25sdp"
186185
android:layout_gravity="center"
187-
android:src="@android:drawable/star_big_on"/>
186+
android:src="@drawable/cart"
187+
android:contentDescription="Cart" />
188188

189189

190190
</androidx.cardview.widget.CardView>
@@ -205,7 +205,7 @@
205205
android:layout_height="wrap_content"
206206
android:background="@drawable/rounded_button"
207207
app:backgroundTint="@null"
208-
android:text="Add to Cart"
208+
android:text="@string/single_purchase"
209209
android:layout_marginBottom="@dimen/_10sdp"
210210

211211

0 commit comments

Comments
 (0)