0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

Finished Get Shared Decks functionality: Implemented methods and workflow to update all cards in a deck and update all cards of the downloaded shared deck after loading it (because if that is not done, both the question and the answer of every card will be empty)

This commit is contained in:
Edu Zamora 2010-04-18 19:45:15 +02:00
parent aa962346a0
commit 94a565037c
7 changed files with 136 additions and 26 deletions

View File

@ -1,4 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com>
~
~ 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/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

View File

@ -576,7 +576,12 @@ public class AnkiDroid extends Activity
private void displayProgressDialogAndLoadDeck()
{
Log.i(TAG, "displayProgressDialogAndLoadDeck - Loading deck " + deckFilename);
displayProgressDialogAndLoadDeck(false);
}
private void displayProgressDialogAndLoadDeck(boolean updateAllCards)
{
Log.i(TAG, "displayProgressDialogAndLoadDeck - Loading deck " + deckFilename + ", update all cards = " + updateAllCards);
// Don't open database again in onResume() until we know for sure this attempt to load the deck is finished
deckSelected = true;
@ -586,10 +591,20 @@ public class AnkiDroid extends Activity
if (deckFilename != null && new File(deckFilename).exists())
{
showControls(false);
DeckTask.launchDeckTask(
DeckTask.TASK_TYPE_LOAD_DECK,
mLoadDeckHandler,
new DeckTask.TaskData(deckFilename));
if(updateAllCards)
{
DeckTask.launchDeckTask(
DeckTask.TASK_TYPE_LOAD_DECK_AND_UPDATE_CARDS,
mLoadDeckHandler,
new DeckTask.TaskData(deckFilename));
}
else
{
DeckTask.launchDeckTask(
DeckTask.TASK_TYPE_LOAD_DECK,
mLoadDeckHandler,
new DeckTask.TaskData(deckFilename));
}
}
else
{
@ -612,8 +627,7 @@ public class AnkiDroid extends Activity
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
@ -692,7 +706,8 @@ public class AnkiDroid extends Activity
savePreferences();
Log.i(TAG, "onActivityResult - deckSelected = " + deckSelected);
displayProgressDialogAndLoadDeck();
// Load deck and update all cards, because if that is not done both the answer and question will be empty
displayProgressDialogAndLoadDeck(true);
}
}

View File

@ -1,3 +1,19 @@
/***************************************************************************************
* Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com> *
* *
* 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 java.io.File;

View File

@ -1,6 +1,7 @@
/****************************************************************************************
* Copyright (c) 2009 Daniel Svärd <daniel.svard@gmail.com> *
* Copyright (c) 2009 Casey Link <unnamedrambler@gmail.com> *
* Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com> *
* *
* 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 *
@ -698,6 +699,37 @@ public class Deck
}
// TODO: The real methods to update cards on Anki should be implemented instead of this
public void updateAllCards()
{
Cursor cursor = AnkiDb.database.rawQuery(
"SELECT id, factId " +
"FROM cards",
null);
while (cursor.moveToNext())
{
// Get card
Card card = new Card();
card.fromDB(cursor.getLong(0));
Log.i(TAG, "Card id = " + card.id);
// Get the related fact
Fact fact = card.getFact();
//Log.i(TAG, "Fact id = " + fact.id);
// Generate the question and answer for this card and update it
HashMap<String,String> newQA = CardModel.formatQA(fact, card.getCardModel());
card.question = newQA.get("question");
Log.i(TAG, "Question = " + card.question);
card.answer = newQA.get("answer");
Log.i(TAG, "Answer = " + card.answer);
card.modified = System.currentTimeMillis() / 1000.0;
card.toDB();
}
}
/* Answering a card
***********************************************************/

View File

@ -1,5 +1,6 @@
/****************************************************************************************
* Copyright (c) 2009 Daniel Svärd <daniel.svard@gmail.com> *
* Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com> *
* *
* 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 *
@ -36,9 +37,10 @@ public class DeckTask extends AsyncTask<DeckTask.TaskData, DeckTask.TaskData, De
private static final String TAG = "AnkiDroid";
public static final int TASK_TYPE_LOAD_DECK = 0;
public static final int TASK_TYPE_ANSWER_CARD = 1;
public static final int TASK_TYPE_SUSPEND_CARD = 2;
public static final int TASK_TYPE_UPDATE_FACT = 3;
public static final int TASK_TYPE_LOAD_DECK_AND_UPDATE_CARDS = 1;
public static final int TASK_TYPE_ANSWER_CARD = 2;
public static final int TASK_TYPE_SUSPEND_CARD = 3;
public static final int TASK_TYPE_UPDATE_FACT = 4;
private static DeckTask instance;
private static DeckTask oldInstance;
@ -88,16 +90,29 @@ public class DeckTask extends AsyncTask<DeckTask.TaskData, DeckTask.TaskData, De
switch (type)
{
case TASK_TYPE_LOAD_DECK:
return doInBackgroundLoadDeck(params);
case TASK_TYPE_ANSWER_CARD:
return doInBackgroundAnswerCard(params);
case TASK_TYPE_SUSPEND_CARD:
return doInBackgroundSuspendCard(params);
case TASK_TYPE_UPDATE_FACT:
return doInBackgroundUpdateFact(params);
default:
return null;
case TASK_TYPE_LOAD_DECK:
return doInBackgroundLoadDeck(params);
case TASK_TYPE_LOAD_DECK_AND_UPDATE_CARDS:
TaskData taskData = doInBackgroundLoadDeck(params);
if(taskData.integer == AnkiDroid.DECK_LOADED)
{
taskData.deck.updateAllCards();
taskData.card = taskData.deck.getCurrentCard();
}
return taskData;
case TASK_TYPE_ANSWER_CARD:
return doInBackgroundAnswerCard(params);
case TASK_TYPE_SUSPEND_CARD:
return doInBackgroundSuspendCard(params);
case TASK_TYPE_UPDATE_FACT:
return doInBackgroundUpdateFact(params);
default:
return null;
}
}
@ -121,7 +136,7 @@ public class DeckTask extends AsyncTask<DeckTask.TaskData, DeckTask.TaskData, De
private TaskData doInBackgroundUpdateFact(TaskData[] params) {
// Save the fact
// Save the fact
Deck deck = params[0].getDeck();
Card editCard = params[0].getCard();
Fact editFact = editCard.fact;

View File

@ -64,7 +64,6 @@ public class Fact {
fromDb(id);
//TODO: load fields associated with this fact.
}
@ -83,7 +82,7 @@ public class Fact {
this.fields = fields;
}
private boolean fromDb(long id)
public boolean fromDb(long id)
{
this.id = id;
Cursor cursor = null;

View File

@ -1,3 +1,19 @@
/***************************************************************************************
* Copyright (c) 2009 Edu Zamora <edu.zasu@gmail.com> *
* *
* 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.async;
import android.content.Context;
@ -102,7 +118,7 @@ public class Connection extends AsyncTask<Connection.Payload, Object, Connection
} catch (Exception e) {
data.success = false;
data.exception = e;
Log.e(TAG, "Error getting shared decks");
Log.e(TAG, "Error getting shared decks = " + e.getMessage());
e.printStackTrace();
}
return data;
@ -115,7 +131,7 @@ public class Connection extends AsyncTask<Connection.Payload, Object, Connection
} catch (Exception e) {
data.success = false;
data.exception = e;
Log.e(TAG, "Error downloading shared deck");
Log.e(TAG, "Error downloading shared deck = " + e.getMessage());
e.printStackTrace();
}
return data;