Home > Back-end >  Is there a way to loop dynamic links parameters to avoid repetition of values?
Is there a way to loop dynamic links parameters to avoid repetition of values?

Time:07-06

I'm using kotlin and I have implemented dynamic links to my app, and when ever the user clicks the link, it will retrieve specific data from Firebase depending on the link. I try to query the parameters of the link but I was unsuccessful so I had to get creative and use .endWith("") statement to assign each link a specific collection from Firebase. The problem is that the more links I use, the bigger the list will get and is not convinient for me. I know there has to be a more efficient way. Any suggestions? Thanks in advance.

    val drinksCoffeeShopsFB = FirebaseFirestore.getInstance().collection("Drinks").document("CoffeeShops")
    val foodFastFoodFB = FirebaseFirestore.getInstance().collection("Food").document("FastFood")
    var deepLink: Uri? = null
    val it = Firebase.dynamicLinks.getDynamicLink(intent).result
    if (it != null) { deepLink = it.link }

when{
    deepLink.toString().endsWith("DrinksCoffeeShops/1/")-> {drinksCoffeeShopsFB.collection("1").get()}
    deepLink.toString().endsWith("DrinksCoffeeShops/2/")-> {drinksCoffeeShopsFB.collection("2").get()}
    deepLink.toString().endsWith("DrinksCoffeeShops/3/")-> {drinksCoffeeShopsFB.collection("3").get()}
    deepLink.toString().endsWith("DrinksCoffeeShops/4/")-> {drinksCoffeeShopsFB.collection("4").get()}
    deepLink.toString().endsWith("DrinksCoffeeShops/5/")-> {drinksCoffeeShopsFB.collection("5").get()}
    deepLink.toString().endsWith("DrinksCoffeeShops/6/")-> {drinksCoffeeShopsFB.collection("6").get()} 
    ...

    deepLink.toString().endsWith("FoodFastFood/1/")-> {foodFastFoodFB.collection("1").get()} 
    deepLink.toString().endsWith("FoodFastFood/2/")-> {foodFastFoodFB.collection("2").get()} 
    deepLink.toString().endsWith("FoodFastFood/3/")-> {foodFastFoodFB.collection("3").get()} 
    deepLink.toString().endsWith("FoodFastFood/4/")-> {foodFastFoodFB.collection("4").get()}
    deepLink.toString().endsWith("FoodFastFood/5/")-> {foodFastFoodFB.collection("5").get()} 
    deepLink.toString().endsWith("FoodFastFood/6/")-> {foodFastFoodFB.collection("6").get()}
    ...

}

This is an example of the dynamic link being used to retrieve its parameters

   https://myapp.page.link/DrinksCoffeeShops/2/

CodePudding user response:

You can try something like this:

val id = link.removeSuffix("/").split('/').last() // This will give you the id present in the deepLink
if("DrinksCoffeeShops" in link)
    drinksCoffeeShopsFB.collection(id).get()
if("FoodFastFood" in link)
    foodFastFoodFB.collection(id).get()
  • Related