Skip to content

Commit

Permalink
Implement store screen further
Browse files Browse the repository at this point in the history
  • Loading branch information
andraantariksa committed Apr 19, 2022
1 parent 840805a commit 12c17e5
Show file tree
Hide file tree
Showing 28 changed files with 395 additions and 131 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ some tuning.
- Splashscreen
- Dagger Hilt
- Flow coroutine
- Compose Navigation
- Retrofit
- Project structure
- Networking using Retrofit
Expand Down
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ dependencies {
implementation project(":core")

// Lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"

Expand Down Expand Up @@ -95,6 +94,8 @@ dependencies {

// Maps
implementation "com.google.android.gms:play-services-maps:18.0.2"
// Image
implementation 'com.squareup.picasso:picasso:2.71828'

implementation "com.google.firebase:firebase-appcheck-safetynet:16.0.0-beta06"
implementation "androidx.core:core-splashscreen:1.0.0-beta02"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<activity
android:name=".ui.coupons.CouponsActivity"
android:exported="false" />
<activity
android:name=".ui.product.ProductActivity"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.DividerItemDecoration
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.CancellationTokenSource
import dagger.hilt.android.AndroidEntryPoint
import id.shaderboi.koffie.R
import id.shaderboi.koffie.core.domain.model.common.Coordinate
import id.shaderboi.koffie.core.util.Resource
import id.shaderboi.koffie.databinding.FragmentStoreBinding
import id.shaderboi.koffie.ui.coupons.CouponsActivity
Expand All @@ -37,10 +43,16 @@ class StoreFragment : Fragment() {
val binding get() = _binding!!

private val homeViewModel by viewModels<StoreViewModel>()
private val args by navArgs<StoreFragmentArgs>()

@Inject
lateinit var numberFormatter: DecimalFormat

private val fusedLocationClient: FusedLocationProviderClient by lazy {
LocationServices.getFusedLocationProviderClient(requireContext())
}
private var cancellationTokenSource = CancellationTokenSource()

private val takePromoCoupon = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
Expand All @@ -52,7 +64,7 @@ class StoreFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

homeViewModel.onEvent(StoreEvent.Load(1))
homeViewModel.onEvent(StoreEvent.Load(args.storeId))
}

override fun onCreateView(
Expand All @@ -62,6 +74,15 @@ class StoreFragment : Fragment() {
): View {
_binding = FragmentStoreBinding.inflate(inflater, container, false)

args.storeWithDistance?.let { storeWithDistance ->
binding.apply {
shimmerFrameLayoutStore.hideShimmer()

textViewTitle.text = storeWithDistance.store.name
textViewDistance.text = "${numberFormatter.format(storeWithDistance.distance)} km"
}
}

askPermission()
collectUIEvent()
setupView()
Expand All @@ -78,7 +99,7 @@ class StoreFragment : Fragment() {
is Resource.Error -> {
}
is Resource.Loaded -> {
binding.shimmerFrameLayoutMain.stopShimmer()
binding.shimmerFrameLayoutProducts.hideShimmer()
binding.recyclerViewProducts.adapter =
CategorizedProductAdapter(res.data, numberFormatter)
}
Expand All @@ -94,8 +115,44 @@ class StoreFragment : Fragment() {
homeViewModel.store.collectLatest { res ->
when (res) {
is Resource.Error -> {}
is Resource.Loaded -> {}
is Resource.Loading -> {}
is Resource.Loaded -> {
binding.apply {
shimmerFrameLayoutStore.hideShimmer()

textViewTitle.text = res.data.name
fusedLocationClient.getCurrentLocation(
LocationRequest.PRIORITY_HIGH_ACCURACY,
cancellationTokenSource.token
).addOnCompleteListener { task ->
if (task.isSuccessful) {
val location = task.result
val locationCoordinate =
Coordinate(location.latitude, location.longitude)
val distance =
res.data.coordinate.distanceTo(locationCoordinate)
textViewDistance.text =
"${numberFormatter.format(distance)} km"
}
}

includeViewPromo.textViewPromoCaption.background = null
textViewDistance.background = null
textViewTitle.background = null
}
}
is Resource.Loading -> {
val context = requireContext()
if (args.storeWithDistance == null) {
binding.apply {
includeViewPromo.textViewPromoCaption.background =
ContextCompat.getDrawable(context, R.color.placeholder)
textViewDistance.background =
ContextCompat.getDrawable(context, R.color.placeholder)
textViewTitle.background =
ContextCompat.getDrawable(context, R.color.placeholder)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package id.shaderboi.koffie.ui.main.store.adapter

import IntentExtra
import android.content.Intent
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import id.shaderboi.koffie.core.domain.model.store.products.Product
import id.shaderboi.koffie.databinding.ItemProductBinding
import id.shaderboi.koffie.ui.product.ProductActivity
import java.text.DecimalFormat

class ProductsAdapter(
Expand Down Expand Up @@ -32,13 +37,25 @@ class ProductsAdapter(
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val product = products[position]
holder.binding.apply {
// textViewTitle.text = product.name
// textViewDescription.text = product.description
// textViewPrice.text = numberFormatter.format(product.price)
// product.discount?.let { discount ->
// textViewDiscount.text = numberFormatter.format(product.discount)
// }
// imageViewProduct
textViewTitle.text = product.name
textViewDescription.text = product.description
textViewPrice.text = numberFormatter.format(product.price)
product.discount?.let { discount ->
textViewDiscounted.text = numberFormatter.format(product.discount)
textViewPrice.paintFlags = textViewPrice.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
}

Picasso.get()
.load(product.imageUrl)
.into(imageViewProduct)

root.setOnClickListener {
val context = root.context
val intent = Intent(context, ProductActivity::class.java).apply {
putExtra(IntentExtra.PRODUCT_ID, product.id)
}
context.startActivity(intent)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class StoreViewModel @Inject constructor(
private fun loadStoreAndProduct(storeId: Int) = viewModelScope.launch {
_products.emit(Resource.Loading())
_store.emit(Resource.Loading())
// launch {
// try {
// _store.emit(Resource.Loaded(storeRepository.getStore(storeId)))
// } catch (ex: Exception) {
// _store.emit(Resource.Error(ex))
// }
// }
// launch {
// try {
// _products.emit(Resource.Loaded(storeRepository.getProducts(storeId)))
// } catch (ex: Exception) {
// _products.emit(Resource.Error(ex))
// }
// }
launch {
try {
_store.emit(Resource.Loaded(storeRepository.getStore(storeId)))
} catch (ex: Exception) {
_store.emit(Resource.Error(ex))
}
}
launch {
try {
_products.emit(Resource.Loaded(storeRepository.getProducts(storeId)))
} catch (ex: Exception) {
_products.emit(Resource.Error(ex))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package id.shaderboi.koffie.ui.main.stores

import Bundle as BundleConst
import Permission
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -12,12 +11,14 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.tasks.CancellationTokenSource
import com.google.android.material.appbar.AppBarLayout
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -32,6 +33,8 @@ import kotlinx.coroutines.launch
import pub.devrel.easypermissions.EasyPermissions
import java.text.DecimalFormat
import javax.inject.Inject
import Bundle as BundleConst


@AndroidEntryPoint
class StoresFragment : Fragment() {
Expand Down Expand Up @@ -162,11 +165,25 @@ class StoresFragment : Fragment() {
is Resource.Error -> {
}
is Resource.Loaded -> {
binding.shimmerFrameLayoutMain.stopShimmer()
binding.shimmerFrameLayoutMain.hideShimmer()

binding.recyclerViewStores.adapter =
StoresAdapter(res.data, numberFormatter) { store ->

val navController = findNavController()
val action =
StoresFragmentDirections.actionNavigationHomeStoresToNavigationHomeMain(
store.store.id,
store
)
navController.navigate(action)
}
res.data.forEach { storeWithDistance ->
map.addMarker(
MarkerOptions()
.position(storeWithDistance.store.coordinate.toLatLng())
.title(storeWithDistance.store.name)
)
}
}
is Resource.Loading -> {
binding.recyclerViewStores.adapter = StoresShimmerAdapter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package id.shaderboi.koffie.ui.main.stores.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import id.shaderboi.koffie.core.domain.model.store.Store
import id.shaderboi.koffie.core.domain.model.store.StoreWithDistance
import id.shaderboi.koffie.databinding.ItemStoreBinding
import java.text.DecimalFormat

class StoresAdapter(
val stores: List<Store>,
val stores: List<StoreWithDistance>,
val numberFormatter: DecimalFormat,
val onClick: (store: Store) -> Unit
val onClick: (store: StoreWithDistance) -> Unit
) :
RecyclerView.Adapter<StoresAdapter.ViewHolder>() {
inner class ViewHolder(val binding: ItemStoreBinding) : RecyclerView.ViewHolder(binding.root)
Expand All @@ -23,9 +23,11 @@ class StoresAdapter(
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val store = stores[position]
holder.binding.apply {
textViewTitle.text = store.name
textViewTitle.text = numberFormatter.format(store.distance)
textViewAddress.text = store.address
val distance = numberFormatter.format(store.distance / 1000F)

textViewTitle.text = store.store.name
textViewDistance.text = "${distance}km"
textViewAddress.text = store.store.address
root.setOnClickListener {
onClick(store)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import id.shaderboi.koffie.core.domain.model.common.Coordinate
import id.shaderboi.koffie.core.domain.model.common.toCoordinate
import id.shaderboi.koffie.core.domain.model.store.Store
import id.shaderboi.koffie.core.domain.model.store.StoreWithDistance
import id.shaderboi.koffie.core.domain.repository.StoreRepository
import id.shaderboi.koffie.core.util.Resource
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -17,7 +18,7 @@ import javax.inject.Inject
class StoresViewModel @Inject constructor(
val storeRepository: StoreRepository
) : ViewModel() {
private val _stores = MutableSharedFlow<Resource<List<Store>>>()
private val _stores = MutableSharedFlow<Resource<List<StoreWithDistance>>>()
val stores = _stores.asSharedFlow()

fun onEvent(event: StoresEvent) {
Expand All @@ -28,6 +29,6 @@ class StoresViewModel @Inject constructor(

private fun load(coordinate: Coordinate) = viewModelScope.launch {
_stores.emit(Resource.Loading())
// _stores.emit(Resource.Loaded(storeRepository.getStores(coordinate)))
_stores.emit(Resource.Loaded(storeRepository.getStores(coordinate)))
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
android:text="Total Payment" />

<TextView
android:id="@+id/text_view_discount"
android:id="@+id/text_view_discounted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
Expand Down
Loading

0 comments on commit 12c17e5

Please sign in to comment.