|
1 | | -import axios from "axios"; |
2 | | -import { apiUrl } from "../utils/constants"; |
3 | | -import { Environment, Measurement } from "../types"; |
| 1 | +package com.nevmem.moneysaver.app.data |
4 | 2 |
|
5 | | -async function exportCsv(url: string, filename: string) { |
6 | | - try { |
7 | | - const response = await axios({ |
8 | | - url, |
9 | | - method: "GET", |
10 | | - responseType: "blob", |
11 | | - }); |
12 | | - const href = URL.createObjectURL(response.data); |
| 3 | +import android.util.Log |
| 4 | +import com.nevmem.moneysaver.app.room.AppDatabase |
| 5 | +import com.nevmem.moneysaver.app.room.entity.Feature |
| 6 | +import kotlinx.coroutines.CoroutineScope |
| 7 | +import kotlinx.coroutines.Dispatchers |
| 8 | +import kotlinx.coroutines.Job |
| 9 | +import kotlinx.coroutines.launch |
| 10 | +import java.lang.ref.WeakReference |
| 11 | +import javax.inject.Inject |
13 | 12 |
|
14 | | - const link = document.createElement("a"); |
15 | | - link.href = href; |
16 | | - link.setAttribute("download", filename); |
17 | | - document.body.appendChild(link); |
18 | | - link.click(); |
| 13 | +class SettingsManagerImpl @Inject constructor( |
| 14 | + private val appDatabase: AppDatabase |
| 15 | +) : SettingsManager { |
| 16 | + private val enabledFeatures = HashSet<Feature>() |
| 17 | + private val tag = "SETTINGS_MANAGER_IMPL" |
19 | 18 |
|
20 | | - document.body.removeChild(link); |
21 | | - URL.revokeObjectURL(href); |
22 | | - } catch (error) { |
23 | | - throw new Error(error); |
24 | | - } |
25 | | -} |
| 19 | + private val listeners = ArrayList<WeakReference<SettingsManagerListener>>() |
| 20 | + private var initialization: Job? = null |
26 | 21 |
|
27 | | -export async function dataFetcher<T>(url: string) { |
28 | | - return await axios |
29 | | - .get<T>(url) |
30 | | - .then((res) => res.data) |
31 | | - .catch((error) => { |
32 | | - throw new Error(error); |
33 | | - }); |
34 | | -} |
| 22 | + init { |
| 23 | + Log.d(tag, "Initialization") |
| 24 | + } |
35 | 25 |
|
36 | | -export async function postData<T>(url: string, payload: any) { |
37 | | - return await axios |
38 | | - .post<T>(url, payload) |
39 | | - .then((res) => res.data) |
40 | | - .catch((error) => { |
41 | | - throw new Error(error); |
42 | | - }); |
43 | | -} |
| 26 | + override fun initialize(): Job { |
| 27 | + if (initialization == null) { |
| 28 | + initialization = CoroutineScope(Dispatchers.IO).launch { |
| 29 | + appDatabase.featuresDao().loadAll().forEach { |
| 30 | + Log.d(tag, "Feature: ${it.featureName}") |
| 31 | + enabledFeatures.add(it) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return initialization as Job |
| 36 | + } |
44 | 37 |
|
45 | | -export async function deleteData(url: string) { |
46 | | - return await axios.delete(url).catch((error) => { |
47 | | - throw new Error(error); |
48 | | - }); |
49 | | -} |
| 38 | + override fun isFeatureEnabled(featureName: String): Boolean { |
| 39 | + return enabledFeatures.contains(Feature(featureName)) |
| 40 | + } |
50 | 41 |
|
51 | | -export async function exportMeasurementsCsv(benchmarkId: string) { |
52 | | - const url = `${apiUrl}/api/v2/export/measurements/csv?benchmarkId=${benchmarkId}`; |
53 | | - await exportCsv(url, "measurements.csv"); |
54 | | -} |
| 42 | + override fun enableFeature(featureName: String) { |
| 43 | + if (!isFeatureEnabled(featureName)) { |
| 44 | + val feature = Feature(featureName) |
| 45 | + enabledFeatures.add(feature) |
| 46 | + insertFeature(feature) |
| 47 | + notifyFeaturesChanged() |
| 48 | + } |
| 49 | + } |
55 | 50 |
|
56 | | -export async function exportEnvironmentsCsv(filters: Object) { |
57 | | - const withValues = Object.entries(filters) |
58 | | - .filter(([_key, value]) => Boolean(value)) |
59 | | - .map(([key, value]) => [key, value]); |
60 | | - const params = new URLSearchParams(withValues).toString(); |
61 | | - const url = `${apiUrl}/api/v2/export/environments/csv?${params}`; |
62 | | - await exportCsv(url, "environments.csv"); |
63 | | -} |
| 51 | + override fun disableFeature(featureName: String) { |
| 52 | + if (isFeatureEnabled(featureName)) { |
| 53 | + val featureToDelete = enabledFeatures.find { |
| 54 | + it.featureName == featureName |
| 55 | + } |
| 56 | + enabledFeatures.remove(Feature(featureName)) |
| 57 | + if (featureToDelete != null) { |
| 58 | + deleteFeature(featureToDelete) |
| 59 | + } |
| 60 | + notifyFeaturesChanged() |
| 61 | + } |
| 62 | + } |
64 | 63 |
|
65 | | -export async function exportBenchmarksCsv() { |
66 | | - const url = `${apiUrl}/api/v2/export/benchmarks/csv`; |
67 | | - await exportCsv(url, "benchmarks.csv"); |
68 | | -} |
| 64 | + override fun toggleFeature(featureName: String) { |
| 65 | + if (isFeatureEnabled(featureName)) { |
| 66 | + disableFeature(featureName) |
| 67 | + } else { |
| 68 | + enableFeature(featureName) |
| 69 | + } |
| 70 | + } |
69 | 71 |
|
70 | | -export async function getMeasurementsForBechmark(benchmarkId: string) { |
71 | | - const url = `${apiUrl}/api/v2/measurement/find?benchmarkId=${benchmarkId}`; |
72 | | - const data = await dataFetcher<Array<Measurement>>(url); |
73 | | - return data?.length; |
74 | | -} |
| 72 | + override fun subscribe(listener: WeakReference<SettingsManagerListener>) { |
| 73 | + listeners.add(listener) |
| 74 | + } |
75 | 75 |
|
76 | | -export async function saveEnvironment(payload: Object) { |
77 | | - return await postData<Array<Environment>>( |
78 | | - `${apiUrl}/api/v2/environment`, |
79 | | - payload |
80 | | - ); |
81 | | -} |
| 76 | + override fun unsubscribe(listener: WeakReference<SettingsManagerListener>) { |
| 77 | + listeners.remove(listener) |
| 78 | + } |
| 79 | + |
| 80 | + private fun notifyFeaturesChanged() { |
| 81 | + val iterator = listeners.iterator() |
| 82 | + while (iterator.hasNext()) { |
| 83 | + val ref = iterator.next().get() |
| 84 | + if (ref != null) { |
| 85 | + ref.onFeaturesUpdated() |
| 86 | + } else { |
| 87 | + iterator.remove() |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun insertFeature(feature: Feature) { |
| 93 | + CoroutineScope(Dispatchers.IO).launch { |
| 94 | + appDatabase.featuresDao().insert(feature) |
| 95 | + } |
| 96 | + } |
82 | 97 |
|
83 | | -export async function deleteEnvironment(id: string) { |
84 | | - return await deleteData(`${apiUrl}/api/v2/environment?id=${id}`); |
| 98 | + private fun deleteFeature(feature: Feature) { |
| 99 | + CoroutineScope(Dispatchers.IO).launch { |
| 100 | + appDatabase.featuresDao().delete(feature) |
| 101 | + } |
| 102 | + } |
85 | 103 | } |
0 commit comments