-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c550746
commit c32f6e9
Showing
7 changed files
with
141 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
/* | ||
* Copyright (c) 2022 Prateek Singh <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.google.gson.Gson | ||
import com.ichi2.anki.services.DeckMetaDataService | ||
import com.ichi2.anki.worker.DeckMetaDataWorker | ||
import timber.log.Timber | ||
|
||
class DeckMetaDataPreference(context: Context) { | ||
|
@@ -21,22 +37,22 @@ class DeckMetaDataPreference(context: Context) { | |
return sharedPreferences.getString(key, default)!! | ||
} | ||
|
||
fun setMetaData(key: String, value: DeckMetaDataService.Meta) { | ||
fun setMetaData(key: String, value: DeckMetaDataWorker.Meta) { | ||
val json: String = Gson().toJson(value) | ||
sharedPreferences.edit() | ||
.putString(key, json) | ||
.apply() | ||
Timber.tag("META").e(json) | ||
} | ||
|
||
fun getMetaData(key: String): DeckMetaDataService.Meta? { | ||
fun getMetaData(key: String): DeckMetaDataWorker.Meta? { | ||
val jsonData = sharedPreferences | ||
.getString(key, null) | ||
|
||
return if (jsonData == null) { | ||
null | ||
} else { | ||
Gson().fromJson(jsonData, DeckMetaDataService.Meta::class.java) | ||
Gson().fromJson(jsonData, DeckMetaDataWorker.Meta::class.java) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 0 additions & 135 deletions
135
AnkiDroid/src/main/java/com/ichi2/anki/services/DeckMetaDataService.kt
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
AnkiDroid/src/main/java/com/ichi2/anki/worker/DeckMetaDataWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2022 Prateek Singh <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.worker | ||
|
||
import android.content.Context | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.ichi2.anki.CollectionHelper | ||
import com.ichi2.anki.DeckMetaDataPreference | ||
import com.ichi2.libanki.sched.Counts | ||
import timber.log.Timber | ||
|
||
/** | ||
* This Worker is responsible to Collect all the metadata and store it in share preference. | ||
* Technically it refreshes the Deck Meta Data. Whenever this service is called | ||
* then I will collect the new data of decks. | ||
* */ | ||
class DeckMetaDataWorker(val context: Context, workerParameters: WorkerParameters) : Worker(context, workerParameters) { | ||
|
||
private lateinit var colHelper: CollectionHelper | ||
|
||
override fun doWork(): Result { | ||
|
||
colHelper = CollectionHelper.getInstance() | ||
|
||
Timber.tag("META").e("Deck Meta Data Worker started at: ${colHelper.getTimeSafe(context).currentDate}") | ||
|
||
// Update the data of Deck Metadata from HERE | ||
val col = colHelper.getCol(context) | ||
val deckList = col.sched.deckDueList() | ||
|
||
deckList.forEach { | ||
val new = it?.newCount ?: -1 | ||
val lrn = it?.lrnCount ?: -1 | ||
val rev = it?.revCount ?: -1 | ||
val data = Meta( | ||
it?.did ?: -1, | ||
it?.fullDeckName ?: "Deck Name", | ||
new, | ||
lrn, | ||
rev, | ||
col.sched.eta(Counts(new, lrn, rev), false) | ||
) | ||
storeInPreference(data) | ||
} | ||
|
||
updateLastFetch(colHelper.getTimeSafe(context).toString()) | ||
|
||
return Result.success() // Done work successfully... | ||
} | ||
|
||
private fun updateLastFetch(time: String) { | ||
val metaPreference = DeckMetaDataPreference(context) | ||
metaPreference.putString(LAST_FETCH_TIME, time) | ||
} | ||
|
||
/** | ||
* It is used to store the data in the DeckMetaData Shared Preference. | ||
* */ | ||
private fun storeInPreference(list: Meta) { | ||
val metaPreference = DeckMetaDataPreference(context) | ||
metaPreference.setMetaData(list.did.toString(), list) | ||
} | ||
|
||
data class Meta( | ||
val did: Long, | ||
val deckName: String, | ||
val new: Int, | ||
val lrn: Int, | ||
val rev: Int, | ||
val eta: Int | ||
) | ||
|
||
companion object { | ||
const val DECK_META_WORKER = "DeckMetaData" | ||
const val LAST_FETCH_TIME = "LAST_FETCH" | ||
} | ||
} |