Skip to content

Commit

Permalink
Merge pull request #7 from armancodv/settings-events
Browse files Browse the repository at this point in the history
settings and events
  • Loading branch information
armancodv authored May 16, 2021
2 parents 4c867f9 + 58f6765 commit 50e2459
Show file tree
Hide file tree
Showing 48 changed files with 682 additions and 24 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ dependencies {
// over scroll
implementation 'me.everything:overscroll-decor-android:1.1.0'

// ripple
implementation 'com.balysv:material-ripple:1.0.2'

// google play
implementation 'com.google.android.play:core:1.10.0'

// firebase
implementation platform('com.google.firebase:firebase-bom:26.8.0')
implementation 'com.google.firebase:firebase-crashlytics-ktx'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/armanco/integral/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.armanco.integral
import android.content.Context
import androidx.room.Room
import com.armanco.integral.managers.db.Db
import com.armanco.integral.utils.facade.EventFacade
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -31,4 +32,8 @@ object AppModule {
@Singleton
@Provides
fun provideItemDao(db: Db) = db.formulaDao()

@Singleton
@Provides
fun provideEventFacade() = EventFacade()
}
13 changes: 10 additions & 3 deletions app/src/main/java/com/armanco/integral/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.armanco.integral.extensions.isPersian
import com.armanco.integral.extensions.isPro
import com.armanco.integral.extensions.setLocale
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.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()
initToolbar()
eventFacade.init(this)
}

override fun onResume() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ class CategoryFragment: Fragment(R.layout.fragment_category) {
solver?.setOnClickListener {
navigateToSolver()
}
settings?.setOnClickListener {
navigateToSettings()
}
model.load()
model.categories.observe(viewLifecycleOwner) { categories ->
categoryListView?.with(categories)
}
}

private fun navigateToItem(category: Category) {
model.selectCategory(category.id, getString(category.stringRes))
Log.d("category", category.toString())
findNavController().navigate(R.id.action_categoryFragment_to_formulaFragment,
Bundle().apply {
Expand All @@ -44,4 +48,8 @@ class CategoryFragment: Fragment(R.layout.fragment_category) {
findNavController().navigate(R.id.action_to_solverFragment)
}

private fun navigateToSettings() {
findNavController().navigate(R.id.action_to_settingsFragment)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ import androidx.lifecycle.viewModelScope
import com.armanco.integral.managers.repository.CategoryRepository
import com.armanco.integral.managers.repository.FormulaRepository
import com.armanco.integral.models.Category
import com.armanco.integral.utils.facade.EventFacade
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CategoryViewModel @Inject constructor(
private val repository: CategoryRepository,
private val formulaRepository: FormulaRepository
private val formulaRepository: FormulaRepository,
private val eventFacade: EventFacade
): ViewModel() {
val categories = MutableLiveData<List<Category>>()

fun load() {
eventFacade.screenView(CategoryFragment::class.java.simpleName)
viewModelScope.launch {
repository.populate()
formulaRepository.populate()
categories.value = repository.getAll()
}
}

fun selectCategory(id: Int, title: String) {
eventFacade.selectCategory(id, title)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class FormulaFragment: Fragment(R.layout.fragment_formula) {
}

private fun navigateToImage(formula: Formula) {
model.selectImage(formula.id, formula.categoryId, getString(formula.stringRes))
findNavController().navigate(R.id.action_formulaFragment_to_imageFragment,
Bundle().apply {
putInt(IMAGE_KEY, formula.drawableRes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ 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.facade.EventFacade
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class FormulaViewModel @Inject constructor(
private val repository: FormulaRepository
private val repository: FormulaRepository,
private val eventFacade: EventFacade
): ViewModel() {
val formulas = MutableLiveData<List<Formula>>()
fun load(id: Int?) {
eventFacade.screenView(FormulaFragment::class.java.simpleName)
viewModelScope.launch {
formulas.value = id?.let { repository.getByCategory(it) }
}
}

fun selectImage(id: Int, categoryId: Int, title: String) {
eventFacade.selectImage(id, categoryId, title)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package com.armanco.integral.navigation.image
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.armanco.integral.R
import com.armanco.integral.extensions.isPro
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.LoadAdError
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.fragment_image.*


@AndroidEntryPoint
class ImageFragment: Fragment(R.layout.fragment_image) {
private val model: ImageViewModel by viewModels()

private var mInterstitialAd: InterstitialAd? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.load()
if(!isPro) showInterstitialAd()
arguments?.getInt(IMAGE_KEY)?.let { photoView?.setImageResource(it) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.facade.EventFacade
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ImageViewModel @Inject constructor(
private val eventFacade: EventFacade
): ViewModel() {
fun load() {
eventFacade.screenView(ImageFragment::class.java.simpleName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.armanco.integral.navigation.settings

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.armanco.integral.R
import com.armanco.integral.utils.extensions.goToGooglePlay
import com.armanco.integral.utils.extensions.goToUrl
import com.armanco.integral.utils.extensions.isPro
import com.armanco.integral.utils.facade.ReviewFacade
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.fragment_settings.*

@AndroidEntryPoint
class SettingsFragment: Fragment(R.layout.fragment_settings) {

private val model: SettingsViewModel by viewModels()

private val reviewFacade = ReviewFacade()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.load()
reviewFacade.init(context)
proVersion?.visibility = if(isPro) View.GONE else View.VISIBLE
proVersion?.onClick = {
model.selectProVersion()
activity?.goToGooglePlay("com.armanco.integral_pro")
}
reportBug?.onClick = {
model.selectReportBug()
activity?.goToUrl("https://github.com/armancodv/integral/issues")
}
contribute?.onClick = {
model.selectContribute()
activity?.goToUrl("https://github.com/armancodv/integral")
}
rate?.onClick = {
model.selectRate()
reviewFacade.showReview(activity)
}
reviewFacade.onFinished = {
model.submitRate()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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.facade.EventFacade
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

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

fun load() {
eventFacade.screenView(SettingsFragment::class.java.simpleName)
}

fun selectProVersion() {
eventFacade.selectProVersion()
}

fun selectReportBug() {
eventFacade.selectReportBug()
}

fun selectContribute() {
eventFacade.selectContribute()
}

fun selectRate() {
eventFacade.selectRate()
}

fun submitRate() {
eventFacade.submitRate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SolverFragment: Fragment(R.layout.fragment_solver) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.load()
initValues()
calculate?.setOnClickListener {
model.calculate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package com.armanco.integral.navigation.solver
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.armanco.integral.managers.repository.SolverRepository
import com.armanco.integral.utils.facade.EventFacade
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class SolverViewModel @Inject constructor(
private val repository: SolverRepository
private val repository: SolverRepository,
private val eventFacade: EventFacade
): ViewModel() {
val result = MutableLiveData<Double?>()
val method = MutableLiveData(Method.Simpson)
Expand All @@ -18,6 +20,10 @@ class SolverViewModel @Inject constructor(
val lowerLimit = MutableLiveData(0.0)
val upperLimit = MutableLiveData(1.0)

fun load() {
eventFacade.screenView(SolverFragment::class.java.simpleName)
}

fun setMethod(method: Method) {
this.method.value = method
this.hasSteps.value = (method == Method.Romberg)
Expand All @@ -29,6 +35,7 @@ class SolverViewModel @Inject constructor(
Method.Simpson -> result.value = repository.simpson(function.value ?: "", lowerLimit.value ?: 0.0, upperLimit.value ?: 0.0)
Method.Romberg -> result.value = repository.romberg(function.value ?: "", lowerLimit.value ?: 0.0, upperLimit.value ?: 0.0, steps.value ?: 1)
}
eventFacade.calculate(method = method.value.toString(), function = function.value, lowerLimit = lowerLimit.value, upperLimit = upperLimit.value, steps = steps.value, result = result.value)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.armanco.integral.utils.extensions

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri


fun Activity.goToGooglePlay(appPackageName: String) {
val intent = try {
Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName"))
} catch (activityNotFoundException: ActivityNotFoundException) {
Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName"))
}
startActivity(intent)
}

fun Activity.goToGooglePlay() {
goToGooglePlay(packageName)
}

fun Activity.goToUrl(url: String?) {
val uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
Loading

0 comments on commit 50e2459

Please sign in to comment.