Skip to content

Commit 15d24a7

Browse files
committed
Agregamos fragmento de promoción, ara cargar los datos del remote config
1 parent c0e0d08 commit 15d24a7

File tree

4 files changed

+159
-1
lines changed

4 files changed

+159
-1
lines changed

NiloPartner/app/src/main/java/com/barryzea/niloclient/MainActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.barryzea.niloclient.interfaces.OnProductListener
2828
import com.barryzea.niloclient.order.OrderActivity
2929
import com.barryzea.niloclient.pojo.Product
3030
import com.barryzea.niloclient.profile.ProfileFragment
31+
import com.barryzea.niloclient.promo.PromoFragment
3132
import com.barryzea.niloclient.settings.SettingsActivity
3233
import com.barryzea.niloclient.settings.SettingsFragment
3334
import com.firebase.ui.auth.AuthMethodPickerLayout
@@ -310,7 +311,12 @@ class MainActivity : AppCompatActivity(), OnProductListener, MainAux {
310311
startActivity(Intent(this, SettingsActivity::class.java))
311312
}
312313
R.id.itemPromo->{
313-
314+
val fragment=PromoFragment()
315+
supportFragmentManager
316+
.beginTransaction()
317+
.add(R.id.containerMain, fragment)
318+
.addToBackStack(null)
319+
.commit()
314320
}
315321
}
316322
return super.onOptionsItemSelected(item)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.barryzea.niloclient.promo
2+
3+
import android.annotation.SuppressLint
4+
import android.os.Bundle
5+
import android.view.*
6+
import androidx.appcompat.app.AppCompatActivity
7+
import androidx.fragment.app.Fragment
8+
import com.barryzea.niloclient.R
9+
import com.barryzea.niloclient.databinding.FragmentPromoBinding
10+
import com.barryzea.niloclient.interfaces.MainAux
11+
import com.bumptech.glide.Glide
12+
import com.bumptech.glide.load.engine.DiskCacheStrategy
13+
import com.google.android.material.badge.BadgeDrawable
14+
import com.google.android.material.badge.BadgeUtils
15+
import com.google.firebase.ktx.Firebase
16+
import com.google.firebase.remoteconfig.ktx.remoteConfig
17+
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
18+
import kotlin.math.roundToInt
19+
20+
/****
21+
* Project Nilo Client
22+
* Created by Barry Zea H. on 28/02/2022.
23+
* Copyright (c) All rights reserved.
24+
***/
25+
26+
class PromoFragment: Fragment() {
27+
private var bind:FragmentPromoBinding?=null
28+
private var title:String=""
29+
30+
override fun onCreateView(
31+
inflater: LayoutInflater,
32+
container: ViewGroup?,
33+
savedInstanceState: Bundle?
34+
): View? {
35+
bind= FragmentPromoBinding.inflate(inflater, container, false)
36+
bind?.let{
37+
return it.root
38+
}
39+
40+
return super.onCreateView(inflater, container, savedInstanceState)
41+
42+
}
43+
44+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
45+
super.onViewCreated(view, savedInstanceState)
46+
configToolbar()
47+
configRemoteConfig()
48+
}
49+
private fun configToolbar(){
50+
(activity as? MainAux)?.showButton(false)
51+
(activity as? AppCompatActivity)?.apply {
52+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
53+
title=supportActionBar?.title
54+
supportActionBar?.title=getString(R.string.promo_fragment_title)
55+
setHasOptionsMenu(true)
56+
57+
}
58+
}
59+
@SuppressLint("UnsafeOptInUsageError")
60+
private fun configRemoteConfig() {
61+
val remoteConfig= Firebase.remoteConfig
62+
remoteConfig.setDefaultsAsync(R.xml.remote_config_default)
63+
//solicitar y extraer datos de remoteconfig
64+
remoteConfig.fetchAndActivate()
65+
66+
//si no hay ningún error se extraerán los datos ya sea desde el servidor o localmente
67+
.addOnCompleteListener {
68+
if(it.isSuccessful){
69+
70+
val percentage=remoteConfig.getDouble("percentage")
71+
val photoUrl=remoteConfig.getString("photoUrl")
72+
val message=remoteConfig.getString("message")
73+
bind?.let{
74+
it.tvMessage.text=message
75+
it.tvPercentage.text=String.format("%s0%%", (percentage).roundToInt().toString())
76+
77+
Glide.with(this)
78+
.load(photoUrl)
79+
.diskCacheStrategy(DiskCacheStrategy.NONE)
80+
.placeholder(R.drawable.ic_access_time)
81+
.error(R.drawable.ic_offer)
82+
.centerCrop()
83+
.into(it.imgPromo)
84+
}
85+
86+
}
87+
}
88+
}
89+
90+
override fun onPrepareOptionsMenu(menu: Menu) {
91+
menu.clear()
92+
super.onPrepareOptionsMenu(menu)
93+
}
94+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
95+
if(item.itemId == android.R.id.home){
96+
activity?.onBackPressed()
97+
}
98+
return super.onOptionsItemSelected(item)
99+
}
100+
override fun onDestroyView() {
101+
super.onDestroyView()
102+
(activity as? MainAux)?.showButton(true)
103+
(activity as? AppCompatActivity)?.apply {
104+
supportActionBar?.setDisplayHomeAsUpEnabled(false)
105+
supportActionBar?.title=title
106+
setHasOptionsMenu(false)
107+
}
108+
bind=null
109+
}
110+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
xmlns:tools="http://schemas.android.com/tools"
7+
android:clickable="true"
8+
android:focusable="true"
9+
android:background="@color/white">
10+
<ImageView
11+
android:layout_width="0dp"
12+
android:layout_height="0dp"
13+
android:id="@+id/imgPromo"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintTop_toTopOf="parent"
17+
app:layout_constraintDimensionRatio="16:9"/>
18+
<com.google.android.material.textview.MaterialTextView
19+
android:layout_width="0dp"
20+
android:layout_height="wrap_content"
21+
android:id="@+id/tvPercentage"
22+
android:padding="@dimen/common_padding_default"
23+
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
24+
tools:text="Descuento del 10%"
25+
app:layout_constraintStart_toStartOf="parent"
26+
app:layout_constraintEnd_toEndOf="parent"
27+
app:layout_constraintBottom_toBottomOf="parent"/>
28+
<com.google.android.material.textview.MaterialTextView
29+
android:layout_width="0dp"
30+
android:layout_height="0dp"
31+
android:id="@+id/tvMessage"
32+
android:padding="@dimen/common_padding_default"
33+
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
34+
tools:text="@string/welcome_text"
35+
app:layout_constraintStart_toStartOf="parent"
36+
app:layout_constraintEnd_toEndOf="parent"
37+
app:layout_constraintTop_toBottomOf="@id/imgPromo"
38+
app:layout_constraintBottom_toTopOf="@id/tvPercentage"/>
39+
40+
41+
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<string name="item_title_logout">Cerrar sesión</string>
5555
<string name="item_title_profile">Editar perfil</string>
5656
<string name="item_title_order">Historial de compras</string>
57+
<string name="promo_fragment_title">Promociones</string>
5758

5859

5960
</resources>

0 commit comments

Comments
 (0)