Skip to content

Commit

Permalink
Agregamos fragmento de promoción, ara cargar los datos del remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
hall9zeha committed Feb 28, 2022
1 parent c0e0d08 commit 15d24a7
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.barryzea.niloclient.interfaces.OnProductListener
import com.barryzea.niloclient.order.OrderActivity
import com.barryzea.niloclient.pojo.Product
import com.barryzea.niloclient.profile.ProfileFragment
import com.barryzea.niloclient.promo.PromoFragment
import com.barryzea.niloclient.settings.SettingsActivity
import com.barryzea.niloclient.settings.SettingsFragment
import com.firebase.ui.auth.AuthMethodPickerLayout
Expand Down Expand Up @@ -310,7 +311,12 @@ class MainActivity : AppCompatActivity(), OnProductListener, MainAux {
startActivity(Intent(this, SettingsActivity::class.java))
}
R.id.itemPromo->{

val fragment=PromoFragment()
supportFragmentManager
.beginTransaction()
.add(R.id.containerMain, fragment)
.addToBackStack(null)
.commit()
}
}
return super.onOptionsItemSelected(item)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.barryzea.niloclient.promo

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.barryzea.niloclient.R
import com.barryzea.niloclient.databinding.FragmentPromoBinding
import com.barryzea.niloclient.interfaces.MainAux
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeUtils
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import kotlin.math.roundToInt

/****
* Project Nilo Client
* Created by Barry Zea H. on 28/02/2022.
* Copyright (c) All rights reserved.
***/

class PromoFragment: Fragment() {
private var bind:FragmentPromoBinding?=null
private var title:String=""

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
bind= FragmentPromoBinding.inflate(inflater, container, false)
bind?.let{
return it.root
}

return super.onCreateView(inflater, container, savedInstanceState)

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
configToolbar()
configRemoteConfig()
}
private fun configToolbar(){
(activity as? MainAux)?.showButton(false)
(activity as? AppCompatActivity)?.apply {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
title=supportActionBar?.title
supportActionBar?.title=getString(R.string.promo_fragment_title)
setHasOptionsMenu(true)

}
}
@SuppressLint("UnsafeOptInUsageError")
private fun configRemoteConfig() {
val remoteConfig= Firebase.remoteConfig
remoteConfig.setDefaultsAsync(R.xml.remote_config_default)
//solicitar y extraer datos de remoteconfig
remoteConfig.fetchAndActivate()

//si no hay ningún error se extraerán los datos ya sea desde el servidor o localmente
.addOnCompleteListener {
if(it.isSuccessful){

val percentage=remoteConfig.getDouble("percentage")
val photoUrl=remoteConfig.getString("photoUrl")
val message=remoteConfig.getString("message")
bind?.let{
it.tvMessage.text=message
it.tvPercentage.text=String.format("%s0%%", (percentage).roundToInt().toString())

Glide.with(this)
.load(photoUrl)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.placeholder(R.drawable.ic_access_time)
.error(R.drawable.ic_offer)
.centerCrop()
.into(it.imgPromo)
}

}
}
}

override fun onPrepareOptionsMenu(menu: Menu) {
menu.clear()
super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if(item.itemId == android.R.id.home){
activity?.onBackPressed()
}
return super.onOptionsItemSelected(item)
}
override fun onDestroyView() {
super.onDestroyView()
(activity as? MainAux)?.showButton(true)
(activity as? AppCompatActivity)?.apply {
supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.title=title
setHasOptionsMenu(false)
}
bind=null
}
}
41 changes: 41 additions & 0 deletions NiloPartner/app/src/main/res/layout/fragment_promo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:focusable="true"
android:background="@color/white">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/imgPromo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="16:9"/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/tvPercentage"
android:padding="@dimen/common_padding_default"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
tools:text="Descuento del 10%"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/tvMessage"
android:padding="@dimen/common_padding_default"
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
tools:text="@string/welcome_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/imgPromo"
app:layout_constraintBottom_toTopOf="@id/tvPercentage"/>


</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions NiloPartner/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<string name="item_title_logout">Cerrar sesión</string>
<string name="item_title_profile">Editar perfil</string>
<string name="item_title_order">Historial de compras</string>
<string name="promo_fragment_title">Promociones</string>


</resources>

0 comments on commit 15d24a7

Please sign in to comment.