Skip to content

Commit

Permalink
Merge pull request #120 from itszechs/firebase
Browse files Browse the repository at this point in the history
Added Firebase
  • Loading branch information
itszechs authored Jun 25, 2023
2 parents 1ee013c + 67fcedf commit 289fa51
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build
/build
/release
google-services.json
24 changes: 21 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ plugins {
id "androidx.navigation.safeargs.kotlin"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
id "com.google.gms.google-services"
id "com.google.firebase.crashlytics"
}

def localPropertiesFile = rootProject.file("local.properties")
def localProperties = new Properties()
localProperties.load(new FileInputStream(localPropertiesFile))

android {

namespace "zechs.drive.stream"
Expand All @@ -25,6 +31,9 @@ android {
archivesBaseName = "${applicationId}-${versionCode}"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

resValue "string", "ad_app_id", localProperties['ad.appid']
buildConfigField "String", "ad_home_banner", localProperties['ad.home.banner']
}

buildTypes {
Expand Down Expand Up @@ -65,10 +74,19 @@ dependencies {
implementation project(path: ":mpv")

def exoplayer_version = "2.18.7"
def moshi_version = '1.15.0'
def moshi_version = "1.15.0"
def retrofit_version = "2.9.0"
def room_version = "2.5.1"

// Firebase
implementation platform("com.google.firebase:firebase-bom:32.1.0")
// Analytics
implementation "com.google.firebase:firebase-analytics-ktx"
// Crashlytics
implementation "com.google.firebase:firebase-crashlytics-ktx"
// Admob
implementation "com.google.android.gms:play-services-ads:22.1.0"

// Room
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
Expand Down Expand Up @@ -111,8 +129,8 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1"

// Dagger hilt
implementation 'com.google.dagger:hilt-android:2.46.1'
kapt 'com.google.dagger:hilt-compiler:2.46.1'
implementation "com.google.dagger:hilt-android:2.46.1"
kapt "com.google.dagger:hilt-compiler:2.46.1"

// Splash
implementation "androidx.core:core-splashscreen:1.0.1"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
android:supportsPictureInPicture="true"
android:theme="@style/Theme.Fullscreen"
tools:targetApi="n" />

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/ad_app_id" />
</application>

</manifest>
13 changes: 12 additions & 1 deletion app/src/main/java/zechs/drive/stream/ThisApp.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package zechs.drive.stream

import android.app.Application
import com.google.android.gms.ads.MobileAds
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class ThisApp : Application()
class ThisApp : Application() {

override fun onCreate() {
super.onCreate()
Firebase.crashlytics.setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)
MobileAds.initialize(this)
}

}
33 changes: 33 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
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.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import zechs.drive.stream.BuildConfig
import zechs.drive.stream.R
import zechs.drive.stream.data.model.LatestRelease
import zechs.drive.stream.databinding.ActivityMainBinding
import zechs.drive.stream.utils.AdUnits
import zechs.drive.stream.utils.AppTheme
import zechs.drive.stream.utils.ext.navigateSafe
import zechs.drive.stream.utils.state.Resource
Expand Down Expand Up @@ -79,6 +84,7 @@ class MainActivity : AppCompatActivity() {
themeObserver()
updateObserver()
redirectOnLogin()
setupAds()
}

private fun themeObserver() {
Expand Down Expand Up @@ -140,6 +146,7 @@ class MainActivity : AppCompatActivity() {
sendUpdateNotification(release)
}
}

is Resource.Error -> Log.d(TAG, it.message!!)
else -> {}
}
Expand Down Expand Up @@ -244,4 +251,30 @@ class MainActivity : AppCompatActivity() {
}
AppCompatDelegate.setDefaultNightMode(mode)
}

private fun setupAds() {
val linearLayout = binding.linearLayout
var adView: AdView? = null
val adId = if (BuildConfig.DEBUG) AdUnits.DEBUG_BANNER else AdUnits.RELEASE_BANNER

viewModel.showAds.observe(this) { enabled ->
if (enabled) {
Log.d(TAG, "setupAds(), Loading AdView...")
adView = AdView(this@MainActivity)
adView!!.apply {
setAdSize(AdSize.BANNER)
adUnitId = adId
loadAd(AdRequest.Builder().build())
}
linearLayout.addView(adView)
} else {
if (adView == null) return@observe
Log.d(TAG, "setupAds(), Removing AdView...")
linearLayout.removeView(adView)
adView!!.destroy()
adView = null
}
}
}

}
18 changes: 18 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ class MainViewModel @Inject constructor(
var isChecking = false
private set

private val _showAds = MutableLiveData<Boolean>()
val showAds: LiveData<Boolean> = _showAds

var adsEnabled = false
private set

init {
viewModelScope.launch {
getTheme()
getEnableAds()
val status = getLoginStatus()
if (status) {
getPlayer()
Expand Down Expand Up @@ -107,4 +114,15 @@ class MainViewModel @Inject constructor(
getPlayer()
}

private suspend fun getEnableAds() {
val fetchEnableAds = appSettings.fetchEnableAds()
adsEnabled = fetchEnableAds
_showAds.postValue(fetchEnableAds)
}

fun setEnableAds(enable: Boolean) = viewModelScope.launch {
appSettings.saveEnableAds(enable)
getEnableAds()
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zechs.drive.stream.ui.settings

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -56,6 +57,7 @@ class SettingsFragment : BaseFragment() {
setupThemeMenu()
setupDefaultPlayerMenu()
setupCheckForUpdates()
setupAdsSetting()
}

private fun setupThemeMenu() {
Expand Down Expand Up @@ -157,6 +159,31 @@ class SettingsFragment : BaseFragment() {

}

private fun setupAdsSetting() {
binding.switchAdsSupport.isChecked = mainViewModel.adsEnabled
binding.settingAdsSupport.setOnClickListener {
if (binding.switchAdsSupport.isChecked) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.disable_ads))
.setMessage(getString(R.string.disable_ads_message))
.setNegativeButton(getString(R.string.no)) { dialog, _ ->
dialog.dismiss()
}
.setPositiveButton(getString(R.string.yes)) { dialog, _ ->
dialog.dismiss()
Log.d(TAG, "Disabling ads")
binding.switchAdsSupport.isChecked = false
mainViewModel.setEnableAds(false)
}
.show()
} else {
binding.switchAdsSupport.isChecked = true
mainViewModel.setEnableAds(true)
showSnackBar(getString(R.string.thank_you_for_supporting_the_app))
}
}
}

private fun showSnackBar(message: String) {
Snackbar.make(
binding.root,
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/zechs/drive/stream/utils/AdUnits.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package zechs.drive.stream.utils

import zechs.drive.stream.BuildConfig

class AdUnits {
companion object {
const val DEBUG_BANNER = "ca-app-pub-3940256099942544/6300978111"
const val RELEASE_BANNER = BuildConfig.ad_home_banner
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/zechs/drive/stream/utils/AppSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppSettings @Inject constructor(
const val APP_THEME = "APP_THEME"
const val VIDEO_PLAYER = "VIDEO_PLAYER"
const val LAST_UPDATED = "LAST_UPDATED"
const val ENABLE_ADS = "ENABLE_ADS"
}

private val sessionStore = appContext.dataStore
Expand Down Expand Up @@ -84,6 +85,22 @@ class AppSettings @Inject constructor(
Log.d(TAG, "fetchLastUpdated: $parsed")
return parsed
}

suspend fun saveEnableAds(enable: Boolean) {
val dataStoreKey = stringPreferencesKey(ENABLE_ADS)
sessionStore.edit { settings ->
settings[dataStoreKey] = enable.toString()
}
Log.d(TAG, "saveEnableAds: $enable")
}

suspend fun fetchEnableAds(): Boolean {
val dataStoreKey = stringPreferencesKey(ENABLE_ADS)
val preferences = sessionStore.data.first()
val enable = preferences[dataStoreKey] ?: return true
Log.d(TAG, "fetchEnableAds: $enable")
return enable.toBoolean()
}
}

enum class AppTheme(
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.main.MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/mainNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />

</FrameLayout>
</LinearLayout>
46 changes: 46 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,52 @@
</LinearLayout>
</LinearLayout>


<LinearLayout
android:id="@+id/settingAdsSupport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">

<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@null"
android:src="@null" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:orientation="vertical">

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable_ads"
android:textAppearance="?attr/textAppearanceTitleMedium" />

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/support_the_developer_by_enabling_ads"
android:textAppearance="?attr/textAppearanceBodySmall" />

</LinearLayout>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switchAdsSupport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="false" />
</LinearLayout>

<LinearLayout
android:id="@+id/settingCheckForUpdate"
android:layout_width="match_parent"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@
<string name="check_for_updates">Check for updates</string>
<string name="exoplayer">ExoPlayer</string>
<string name="mpv">MPV</string>
<string name="enable_ads">Enable Ads</string>
<string name="support_the_developer_by_enabling_ads">Support the developer by enabling ads</string>
<string name="disable_ads">Disable Ads</string>
<string name="disable_ads_message">Are you sure you want to disable ads?\n\nThis will remove all ads from the app. Ads help support the development of this app.</string>
<string name="thank_you_for_supporting_the_app">Thank you for supporting the app!</string>
</resources>
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ buildscript {
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.45"
classpath "com.google.gms:google-services:4.3.15"
classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.6"
}
}

Expand Down

0 comments on commit 289fa51

Please sign in to comment.