Skip to content

Commit

Permalink
Merge pull request #10 from armancodv/config
Browse files Browse the repository at this point in the history
config
  • Loading branch information
armancodv authored May 23, 2021
2 parents ed1c399 + c5b6154 commit 2b831eb
Show file tree
Hide file tree
Showing 40 changed files with 255 additions and 191 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ dependencies {
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-perf-ktx'
implementation 'com.google.firebase:firebase-config-ktx'

// json
implementation 'com.google.code.gson:gson:2.8.6'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand Down
1 change: 1 addition & 0 deletions app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.armanco.integral">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.armanco.integral">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".IntegralApp"
android:allowBackup="true"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/armanco/integral/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import android.content.Context
import androidx.room.Room
import com.armanco.integral.managers.db.Db
import com.armanco.integral.utils.facade.EventFacade
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -36,4 +40,16 @@ object AppModule {
@Singleton
@Provides
fun provideEventFacade() = EventFacade()

@Singleton
@Provides
fun provideRemoteConfig(): FirebaseRemoteConfig {
val remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
return remoteConfig
}

}
39 changes: 26 additions & 13 deletions app/src/main/java/com/armanco/integral/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
package com.armanco.integral

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.armanco.integral.utils.extensions.configAds
import com.armanco.integral.utils.extensions.isPersian
import com.armanco.integral.utils.extensions.isPro
import com.armanco.integral.utils.extensions.setLocale
import com.armanco.integral.utils.facade.EventFacade
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import javax.inject.Inject


@AndroidEntryPoint
class MainActivity: AppCompatActivity() {

@Inject
lateinit var eventFacade: EventFacade

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(!isPro) initAdMob()
else removeAdMob()
val model: MainViewModel by viewModels()
model.initEvents(this)
model.configAds.observe(this) {
if(!isPro) initAdMob(it?.bannerId)
else removeAdMob()
}
model.remoteConfig.fetchAndActivate().addOnCompleteListener(this) {
model.configAds.postValue(model.remoteConfig.configAds)
}

initToolbar()
eventFacade.init(this)
}

override fun onResume() {
Expand All @@ -39,14 +45,21 @@ class MainActivity: AppCompatActivity() {
}
}

private fun initAdMob() {
private fun initAdMob(bannerId: String?) {
MobileAds.initialize(this)
val adRequest = AdRequest.Builder().build()
adView?.loadAd(adRequest)
removeAdMob()
val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = bannerId
if (adView.adSize != null && adView.adUnitId != null) {
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
adContainer?.addView(adView)
}
}

private fun removeAdMob() {
adView?.visibility = View.GONE
adContainer?.removeAllViews()
}

private fun initToolbar() {
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/armanco/integral/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.armanco.integral

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.armanco.integral.utils.extensions.configAds
import com.armanco.integral.utils.facade.EventFacade
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
private val eventFacade: EventFacade,
val remoteConfig: FirebaseRemoteConfig,
): ViewModel() {
var configAds = MutableLiveData(remoteConfig.configAds)

fun initEvents(context: Context) {
eventFacade.init(context)
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/armanco/integral/models/ConfigAds.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.armanco.integral.models

data class ConfigAds (
val bannerId: String? = null,
val interstitialId: String? = null
) {
companion object {
const val KEY = "ads"
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/armanco/integral/models/ConfigLinks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.armanco.integral.models

data class ConfigLinks (
val proPackageName: String? = null,
val reportBug: String? = null,
val contribute: String? = null,
val privacy: String? = null,
val terms: String? = null,
) {
companion object {
const val KEY = "links"
const val PRO_PACKAGE_NAME_DEFAULT = "com.armanco.integral_pro"
const val REPORT_BUG_DEFAULT = "https://github.com/armancodv/integral/issues"
const val CONTRIBUTE_DEFAULT = "https://github.com/armancodv/integral"
const val PRIVACY_DEFAULT = "https://github.com/armancodv/integral/blob/master/privacy/PRIVACY-POLICY.md"
const val TERMS_DEFAULT = "https://github.com/armancodv/integral/blob/master/privacy/TERMS-AND-CONDITIONS.md"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.armanco.integral.R
import com.armanco.integral.utils.extensions.isPro
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.LoadAdError
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.fragment_image.*
Expand All @@ -22,24 +23,29 @@ class ImageFragment: Fragment(R.layout.fragment_image) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.load()
if(!isPro) showInterstitialAd()
model.configAds.observe(viewLifecycleOwner) {
if(!isPro) showInterstitialAd(model.configAds.value?.interstitialId)
}
arguments?.getInt(IMAGE_KEY)?.let { photoView?.setImageResource(it) }
}

private fun showInterstitialAd() {
mInterstitialAd = InterstitialAd(activity)
mInterstitialAd?.adUnitId = "ca-app-pub-4301546764905932/6653178697"
val adRequest = AdRequest.Builder().build()
mInterstitialAd?.loadAd(adRequest)
mInterstitialAd?.adListener = object : AdListener() {
override fun onAdLoaded() {
mInterstitialAd?.show()
}
override fun onAdFailedToLoad(adError: LoadAdError) {}
override fun onAdOpened() {}
override fun onAdClicked() {}
override fun onAdLeftApplication() {}
override fun onAdClosed() {}
private fun showInterstitialAd(interstitialId: String?) {
if (activity != null && interstitialId != null) {
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(
requireContext(),
interstitialId,
adRequest,
object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
mInterstitialAd = null
}

override fun onAdLoaded(interstitialAd: InterstitialAd) {
mInterstitialAd = interstitialAd
mInterstitialAd?.show(activity!!)
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.armanco.integral.navigation.image

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.armanco.integral.managers.repository.FormulaRepository
import com.armanco.integral.models.Formula
import com.armanco.integral.utils.extensions.configAds
import com.armanco.integral.utils.facade.EventFacade
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ImageViewModel @Inject constructor(
private val eventFacade: EventFacade
): ViewModel() {
private val eventFacade: EventFacade,
private val remoteConfig: FirebaseRemoteConfig,
) : ViewModel() {
var configAds = MutableLiveData(remoteConfig.configAds)
fun load() {
eventFacade.screenView(ImageFragment::class.java.simpleName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.armanco.integral.R
import com.armanco.integral.models.ConfigLinks
import com.armanco.integral.utils.extensions.goToGooglePlay
import com.armanco.integral.utils.extensions.goToUrl
import com.armanco.integral.utils.extensions.isPro
Expand All @@ -26,15 +27,23 @@ class SettingsFragment: Fragment(R.layout.fragment_settings) {
proVersion?.visibility = if(isPro) View.GONE else View.VISIBLE
proVersion?.onClick = {
model.selectProVersion()
activity?.goToGooglePlay("com.armanco.integral_pro")
activity?.goToGooglePlay(model.configLinks.value?.proPackageName ?: ConfigLinks.PRO_PACKAGE_NAME_DEFAULT)
}
reportBug?.onClick = {
model.selectReportBug()
activity?.goToUrl("https://github.com/armancodv/integral/issues")
activity?.goToUrl(model.configLinks.value?.reportBug ?: ConfigLinks.REPORT_BUG_DEFAULT)
}
contribute?.onClick = {
model.selectContribute()
activity?.goToUrl("https://github.com/armancodv/integral")
activity?.goToUrl(model.configLinks.value?.contribute ?: ConfigLinks.CONTRIBUTE_DEFAULT)
}
privacy?.onClick = {
model.selectPrivacy()
activity?.goToUrl(model.configLinks.value?.privacy ?: ConfigLinks.PRIVACY_DEFAULT)
}
terms?.onClick = {
model.selectTerms()
activity?.goToUrl(model.configLinks.value?.terms ?: ConfigLinks.TERMS_DEFAULT)
}
rate?.onClick = {
model.selectRate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package com.armanco.integral.navigation.settings

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.armanco.integral.managers.repository.FormulaRepository
import com.armanco.integral.models.Formula
import com.armanco.integral.utils.extensions.configLinks
import com.armanco.integral.utils.facade.EventFacade
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SettingsViewModel @Inject constructor(
private val eventFacade: EventFacade
private val eventFacade: EventFacade,
private val remoteConfig: FirebaseRemoteConfig,
): ViewModel() {

var configLinks = MutableLiveData(remoteConfig.configLinks)

fun load() {
eventFacade.screenView(SettingsFragment::class.java.simpleName)
}
Expand All @@ -31,6 +32,14 @@ class SettingsViewModel @Inject constructor(
eventFacade.selectContribute()
}

fun selectPrivacy() {
eventFacade.selectPrivacy()
}

fun selectTerms() {
eventFacade.selectTerms()
}

fun selectRate() {
eventFacade.selectRate()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.armanco.integral.utils.extensions

import com.armanco.integral.models.ConfigAds
import com.armanco.integral.models.ConfigLinks
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.gson.Gson

val FirebaseRemoteConfig.configLinks: ConfigLinks?
get() {
return Gson().fromJson(getString(ConfigLinks.KEY), ConfigLinks::class.java)
}

val FirebaseRemoteConfig.configAds: ConfigAds?
get() {
return Gson().fromJson(getString(ConfigAds.KEY), ConfigAds::class.java)
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/armanco/integral/utils/facade/EventFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ class EventFacade {
))
}

fun selectPrivacy() {
firebaseAnalytics?.logEvent(Event.SELECT_PRIVACY, bundleOf(
Param.LANGUAGE to context?.currentLanguage,
Param.COUNTRY to context?.currentCountry,
))
}

fun selectTerms() {
firebaseAnalytics?.logEvent(Event.SELECT_TERMS, bundleOf(
Param.LANGUAGE to context?.currentLanguage,
Param.COUNTRY to context?.currentCountry,
))
}

fun selectRate() {
firebaseAnalytics?.logEvent(Event.SELECT_RATE, bundleOf(
Param.LANGUAGE to context?.currentLanguage,
Expand All @@ -100,6 +114,8 @@ class EventFacade {
const val SELECT_PRO_VERSION = "select_pro_version"
const val SELECT_REPORT_BUG = "select_report_bug"
const val SELECT_CONTRIBUTE = "select_contribute"
const val SELECT_PRIVACY = "select_privacy"
const val SELECT_TERMS = "select_terms"
const val SELECT_RATE = "select_rate"
const val SUBMIT_RATE = "submit_rate"
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_privacy_tip_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12c5.16,-1.26 9,-6.45 9,-12V5L12,1L12,1zM11,7h2v2h-2V7zM11,11h2v6h-2V11z"/>
</vector>
Loading

0 comments on commit 2b831eb

Please sign in to comment.