Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ dependencies {
implementation(libs.bundles.google.play.review)
implementation(libs.kotlinx.serialization.json)
implementation (libs.airbnb.lottie.compose)
implementation(platform(libs.koin.bom))
implementation(libs.bundles.koin)

implementation(libs.squareup.okhttp)
implementation(libs.jakewarthon.timber)
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/com/brainwallet/BrainwalletApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import android.app.Application
import android.content.Context
import android.content.res.Resources
import com.appsflyer.AppsFlyerLib
import com.brainwallet.di.Module
import com.brainwallet.di.dataModule
import com.brainwallet.di.viewModelModule
import com.brainwallet.notification.setupNotificationChannels
import com.brainwallet.presenter.activities.util.BRActivity
import com.brainwallet.presenter.entities.ServiceItems
Expand All @@ -17,6 +18,10 @@ import com.brainwallet.tools.util.Utils
import com.brainwallet.util.cryptography.KeyStoreKeyGenerator
import com.brainwallet.util.cryptography.KeyStoreManager
import com.google.firebase.crashlytics.FirebaseCrashlytics
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.logger.Level
import timber.log.Timber
import timber.log.Timber.DebugTree
import java.util.Timer
Expand All @@ -28,12 +33,12 @@ class BrainwalletApp : Application() {
override fun onCreate() {
super.onCreate()

initializeModule()

/** DEV: Top placement requirement. */
val enableCrashlytics = !Utils.isEmulatorOrDebug(this)
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enableCrashlytics)

initializeModule()

setupNotificationChannels(this)

AnalyticsManager.init(this)
Expand Down Expand Up @@ -68,9 +73,13 @@ class BrainwalletApp : Application() {
}

protected fun initializeModule() {
module = Module(this)
module!!.remoteConfigSource.initialize()
keyStoreManager = KeyStoreManager(this, KeyStoreKeyGenerator.Impl())

startKoin {
androidLogger(if (BuildConfig.DEBUG) Level.DEBUG else Level.ERROR)
androidContext(this@BrainwalletApp)
modules(dataModule, viewModelModule)
}
}

// override fun attachBaseContext(base: Context) {
Expand Down Expand Up @@ -100,11 +109,6 @@ class BrainwalletApp : Application() {
@SuppressLint("StaticFieldLeak")
private var currentActivity: Activity? = null

//TODO: revisit this, please migrate using koin for DI Framework
@SuppressLint("StaticFieldLeak")
@JvmField
var module: Module? = null

@SuppressLint("StaticFieldLeak")
@JvmField
var keyStoreManager: KeyStoreManager? = null
Expand Down
50 changes: 28 additions & 22 deletions app/src/main/java/com/brainwallet/di/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,40 @@ import com.brainwallet.data.repository.SettingRepository
import com.brainwallet.data.source.RemoteConfigSource
import com.brainwallet.tools.manager.BRApiManager
import com.brainwallet.tools.sqlite.CurrencyDataSource
import com.brainwallet.ui.screens.inputwords.InputWordsViewModel
import com.brainwallet.ui.screens.setpasscode.SetPasscodeViewModel
import com.brainwallet.ui.screens.unlock.UnLockViewModel
import com.brainwallet.ui.screens.welcome.WelcomeViewModel
import com.brainwallet.ui.screens.yourseedproveit.YourSeedProveItViewModel
import com.brainwallet.ui.screens.yourseedwords.YourSeedWordsViewModel
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import org.koin.android.ext.koin.androidApplication
import org.koin.core.module.dsl.viewModel
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.module

//TODO: revisit later
class Module(
val context: Context,
val remoteConfigSource: RemoteConfigSource = provideRemoteConfigSource(),
val apiManager: BRApiManager = provideBRApiManager(remoteConfigSource),
val sharedPreferences: SharedPreferences = provideSharedPreferences(context),
val settingRepository: SettingRepository = provideSettingRepository(
sharedPreferences = sharedPreferences,
currencyDataSource = CurrencyDataSource.getInstance(context)
)
)
//todo module using koin as di framework here

private fun provideBRApiManager(remoteConfigSource: RemoteConfigSource): BRApiManager {
return BRApiManager(remoteConfigSource)
val dataModule = module {
single<RemoteConfigSource> {
RemoteConfigSource.FirebaseImpl(Firebase.remoteConfig).also {
it.initialize()
}
}
single { BRApiManager(get()) }
single { CurrencyDataSource.getInstance(get()) }
single<SharedPreferences> { provideSharedPreferences(context = androidApplication()) }
single<SettingRepository> { SettingRepository.Impl(get(), get()) }
}

private fun provideRemoteConfigSource(): RemoteConfigSource {
return RemoteConfigSource.FirebaseImpl(Firebase.remoteConfig)
}

private fun provideSettingRepository(
sharedPreferences: SharedPreferences,
currencyDataSource: CurrencyDataSource
): SettingRepository {
return SettingRepository.Impl(sharedPreferences, currencyDataSource)
val viewModelModule = module {
viewModelOf(::WelcomeViewModel)
viewModel { InputWordsViewModel() }
viewModel { SetPasscodeViewModel() }
viewModel { UnLockViewModel() }
viewModel { YourSeedProveItViewModel() }
viewModel { YourSeedWordsViewModel() }
}

private fun provideSharedPreferences(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.brainwallet.tools.animation.BRAnimator;
import com.brainwallet.tools.manager.BRSharedPrefs;

import org.koin.java.KoinJavaComponent;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -44,6 +46,8 @@ public static SettingsActivity getApp() {
return app;
}

private SettingRepository settingRepository = (SettingRepository) KoinJavaComponent.inject(SettingRepository.class).getValue();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -135,7 +139,6 @@ private void populateItems() {
items.add(new BRSettingsItem(getString(R.string.Settings_manage), "", null, true));

//toggle dark mode
SettingRepository settingRepository = BrainwalletApp.module.getSettingRepository();
boolean isDarkMode = settingRepository.isDarkMode();
items.add(new BRSettingsItem(getString(R.string.toggle_dark_mode), getString(isDarkMode ? androidx.appcompat.R.string.abc_capital_on : androidx.appcompat.R.string.abc_capital_off), v -> {
settingRepository.toggleDarkMode(!isDarkMode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.brainwallet.presenter.activities.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -13,22 +12,22 @@

import com.brainwallet.BrainwalletApp;
import com.brainwallet.data.repository.SettingRepository;
import com.brainwallet.di.Module;
import com.brainwallet.presenter.activities.DisabledActivity;
import com.brainwallet.presenter.activities.intro.RecoverActivity;
import com.brainwallet.presenter.activities.intro.WriteDownActivity;
import com.brainwallet.tools.animation.BRAnimator;
import com.brainwallet.tools.manager.BRApiManager;
import com.brainwallet.tools.manager.InternetManager;
import com.brainwallet.tools.security.AuthManager;
import com.brainwallet.tools.security.BRKeyStore;
import com.brainwallet.tools.security.BitcoinUrlHandler;
import com.brainwallet.tools.security.PostAuth;
import com.brainwallet.tools.threads.BRExecutor;
import com.brainwallet.tools.util.BRConstants;
import com.brainwallet.tools.util.ExtensionKt;
import com.brainwallet.ui.BrainwalletActivity;
import com.brainwallet.wallet.BRWalletManager;

import org.koin.java.KoinJavaComponent;

import java.util.Locale;

import timber.log.Timber;
Expand All @@ -39,12 +38,11 @@ public class BRActivity extends AppCompatActivity {
System.loadLibrary(BRConstants.NATIVE_LIB_NAME);
}

SettingRepository settingRepository;
private SettingRepository settingRepository = (SettingRepository) KoinJavaComponent.inject(SettingRepository.class).getValue();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

settingRepository = BrainwalletApp.module.getSettingRepository(); //just inject here
String languageCode = settingRepository.getCurrentLanguage().getCode();
Locale.setDefault(settingRepository.getCurrentLanguage().toLocale());
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(languageCode));
Expand Down Expand Up @@ -150,7 +148,8 @@ public static void init(Activity app) {


if (!(app instanceof RecoverActivity || app instanceof WriteDownActivity)) {
BrainwalletApp.module.getApiManager().startTimer(app);
BRApiManager apiManager = KoinJavaComponent.get(BRApiManager.class);
apiManager.startTimer(app);
}

//show wallet locked if it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
import com.brainwallet.BuildConfig;
import com.brainwallet.R;
import com.brainwallet.tools.animation.BRAnimator;
import com.brainwallet.tools.manager.BRApiManager;
import com.brainwallet.tools.manager.BRSharedPrefs;
import com.brainwallet.tools.util.BRConstants;
import com.brainwallet.tools.util.Utils;

import org.koin.core.component.KoinComponentKt;
import org.koin.java.KoinJavaComponent;

import java.util.Date;

import timber.log.Timber;
Expand Down Expand Up @@ -139,7 +143,7 @@ public static String url(Context context, Partner partner, String currency) {
Long timestamp = new Date().getTime();
String uuid = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
String prefix = partner == Partner.MOONPAY ? "/moonpay/buy" : "";
String baseUrl = BrainwalletApp.module.getApiManager().getBaseUrlProd();
String baseUrl = ((BRApiManager) KoinJavaComponent.get(BRApiManager.class)).getBaseUrlProd();
return String.format(baseUrl + prefix + "?address=%s&code=%s&idate=%s&uid=%s", walletAddress, currency, timestamp, uuid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.brainwallet.data.source.RemoteConfigSource;

import org.json.JSONObject;
import org.koin.java.KoinJavaComponent;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -95,7 +96,7 @@ public View onCreateView(LayoutInflater inflater,
* remote config example here
*/
try {
RemoteConfigSource remoteConfigSource = BrainwalletApp.module.getRemoteConfigSource();
RemoteConfigSource remoteConfigSource = KoinJavaComponent.get(RemoteConfigSource.class);
String string = remoteConfigSource.getString(KEY_FEATURE_MENU_HIDDEN_EXAMPLE);
Timber.d("timber: [RemoteConfig] -> " + string);
JSONObject configValue = new JSONObject(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.brainwallet.BrainwalletApp
import com.brainwallet.R
import com.brainwallet.data.model.Language
import com.brainwallet.data.repository.SettingRepository
Expand All @@ -17,17 +16,16 @@ import com.brainwallet.tools.util.getString
import com.brainwallet.ui.RoundedBottomSheetDialogFragment
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import org.koin.android.ext.android.inject
import java.util.Locale

class ChangeLanguageBottomSheet : RoundedBottomSheetDialogFragment() {
lateinit var binding: ChangeLanguageBottomSheetBinding

lateinit var settingRepository: SettingRepository
private val settingRepository by inject<SettingRepository>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

settingRepository = BrainwalletApp.module!!.settingRepository
}

override fun onCreateView(
Expand Down
16 changes: 5 additions & 11 deletions app/src/main/java/com/brainwallet/tools/manager/BRSharedPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.brainwallet.data.repository.SettingRepository;
import com.brainwallet.tools.util.BRConstants;

import org.koin.java.KoinJavaComponent;
import org.koin.mp.KoinPlatformTools_jvmKt;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
Expand Down Expand Up @@ -35,11 +38,7 @@ public static void removeListener(OnIsoChangedListener listener) {
}

public static String getIsoSymbol(Context context) {
if (BrainwalletApp.module == null) {
return "USD";
}

SharedPreferences settingsToGet = BrainwalletApp.module.getSharedPreferences();
SharedPreferences settingsToGet = KoinJavaComponent.get(SharedPreferences.class);
String defIso;
String defaultLanguage = Locale.getDefault().getLanguage();

Expand All @@ -61,12 +60,7 @@ else if (defaultLanguage == "en") {
}

public static void putIso(Context context, String code) {
//migrate using new shared preferences used by setting repository
if (BrainwalletApp.module == null) {
return;
}

SharedPreferences settings = BrainwalletApp.module.getSharedPreferences();
SharedPreferences settings = KoinJavaComponent.get(SharedPreferences.class);
SharedPreferences.Editor editor = settings.edit();
editor.putString(SettingRepository.KEY_FIAT_CURRENCY_CODE, code); //using new shared prefs used by setting repository
editor.apply();
Expand Down
Loading