/** * handle a vertical or horizontal fling from within this Activity. If we have only shown the * first half of the current card so far, then the second half is shown to the user. Otherwise, a * new card is displayed. * * <p>Called by {@link AxialFlingListener} when it detects a vertical or horizontal fling. */ private void handleFling(boolean isCorrectGuess) { if (FlashcardApp.getInstance().isShowEntireCard()) { // if we are already waiting for a card, ignore extra fling. if (_waitingForNextCard) { return; } _waitingForNextCard = true; MsgDispatcher.sendMessageToController(MsgType.MSG_GET_NEXT_FC, 0, 0, null); Flashcard fc = FlashcardApp.getInstance().getCurrentFlashcard(); if (fc != null) { // save current fc as the previous fc. must clone it as the one // we send to update guess count may be released soon. if (_prevFlashcard != null) _prevFlashcard.release(); _prevFlashcard = fc.clone(); int arg1 = isCorrectGuess ? 1 : 0; MsgDispatcher.sendMessageToController(MsgType.MSG_FC_GUESS_RESULT, arg1, 0, fc); } } else { FlashcardApp.getInstance().setShowEntireCard(true); updateDisplay(); } }
/** Insert a set of flashcards (linked list) in a single transaction. */ private void insertFlashcardSet(Flashcard head) { openDB(); SQLiteStatement stmt = getCompiledStatement(SQL_INSERT_FC); boolean gotError = false; _curDB.beginTransaction(); try { for (Flashcard fc = head; fc != null; fc = Flashcard.Chain.getNext(fc)) { stmt.bindLong(1, _importState.getRandomId()); stmt.bindString(2, fc.getLang1Str()); stmt.bindString(3, fc.getLang2Str()); stmt.execute(); } _curDB.setTransactionSuccessful(); } catch (SQLException e) { gotError = true; MsgDispatcher.sendMessageToUI(MsgType.MSG_SHOW_ERROR_MSG, 0, 0, "unable to insert fc"); } finally { _curDB.endTransaction(); } Flashcard.Chain.releaseChain(head); // tell controller to send us more data if insert went fine. if (!gotError) { MsgDispatcher.sendMessageToController(MsgType.MSG_CONTINUE_IMPORT, 0, 0, null); } }
/** * the actual implementation of query. This just does a straight attempt to query for up to {@link * #requiredCount} flashcards from the table starting at the given id and level. This function * will not attempt to wrap to the beginning of the table if insufficient results were found. * * <p>The results of this query will be added to the passed in {@link QueryResult} object. */ private void queryNoWrap(QueryResult qr, int level, int minId, int requiredCount) { Cursor c = _curDB.rawQuery(SQL_QUERY2, new String[] {"" + minId}); if (c.moveToFirst()) { do { // extract fc from cursor and prepend to result chain. Flashcard fc = cursorToFlashcard(c); qr.head = Flashcard.Chain.append(qr.head, fc); if (fc.getID() > qr.maxId) qr.maxId = fc.getID(); ++qr.count; } while (c.moveToNext() && qr.count < requiredCount); } c.close(); }
/** Convert the current cursor row to a flashcard object. */ private Flashcard cursorToFlashcard(Cursor c) { Flashcard fc = Flashcard.acquire(); fc.setID(c.getInt(0)); fc.setLang1Str(c.getString(1)); fc.setLang2Str(c.getString(2)); fc.setLevel(c.getInt(3)); fc.setRightGuessCount(c.getInt(4)); return fc; }
/** update the level and count for the given flashcard in the database. */ private void updateFlashcard(Flashcard fc) { openDB(); _curDB.beginTransaction(); try { // Delete old entry if any. SQLiteStatement dStmt = getCompiledStatement(SQL_DELETE_FCS); dStmt.bindString(1, fc.getLang1Str()); dStmt.execute(); // insert new state entry. SQLiteStatement iStmt = getCompiledStatement(SQL_INSERT_FCS); iStmt.bindString(1, fc.getLang1Str()); iStmt.bindLong(2, fc.getLevel()); iStmt.bindLong(3, fc.getRightGuessCount()); iStmt.execute(); _curDB.setTransactionSuccessful(); } catch (SQLException e) { MsgDispatcher.sendMessageToUI( MsgType.MSG_SHOW_ERROR_MSG, 0, 0, "unable to update id=" + fc.getID()); } finally { _curDB.endTransaction(); } // this flashcard is no longer used by anyone so release it. fc.release(); }
public Flashcard getPreviousCard() { if (hasActiveCards()) { while (true) { cardIndex--; if (cardIndex < 0) { cardIndex = flashcards.size() - 1; } activeCard = flashcards.get(cardIndex); if (activeCard.getActive()) { return activeCard; } } } return activeCard; }
public Flashcard getNextCard() { if (hasActiveCards()) { while (true) { cardIndex++; if (cardIndex == flashcards.size()) { cardIndex = 0; } activeCard = flashcards.get(cardIndex); if (activeCard.getActive()) { return activeCard; } } } return activeCard; }
/** * update the display to show the current flashcard. Either the entire flashcard is shown or just * the first half depending on the value of the {@link #_showEntireCard} variable. */ private void updateDisplay() { Flashcard fc = FlashcardApp.getInstance().getCurrentFlashcard(); if (fc == null) { return; } String lang1Text = fc.getLang1Str(); String lang2Text = ""; if (FlashcardApp.getInstance().isShowEntireCard()) { lang2Text = fc.getLang2Str(); } _topText.setText(StringFormatter.formatEnglishString(lang1Text)); _bottomText.setText(StringFormatter.formatSpanishString(lang2Text)); _statusText.setText( "id: " + fc.getID() + " level: " + fc.getLevel() + " count: " + fc.getRightGuessCount()); }
public void flipActiveCard() { getActiveCard().setNativeFirst(!activeCard.getNativeFirst()); }
public String getActiveCardBack() { return getActiveCard().getNativeFirst() ? activeCard.getForeign() : activeCard.getText(); }
public Flashcard getActiveCard() { if (activeCard.equals(nullCard) && activeCards > 0) { activeCard = flashcards.get(cardIndex); } return activeCard; }