~schunka/financisto/cz-locales

« back to all changes in this revision

Viewing changes to src/ru/orangesoftware/financisto/activity/TransactionActivity.java

  • Committer: Denis Solonenko
  • Date: 2012-05-01 16:02:18 UTC
  • Revision ID: denis.solonenko@gmail.com-20120501160218-y8m4kqd6v0iujov7
Save splits state in TransactionActivity

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import android.content.DialogInterface;
15
15
import android.content.Intent;
16
16
import android.database.Cursor;
 
17
import android.os.Bundle;
17
18
import android.util.Log;
18
19
import android.view.Menu;
19
20
import android.view.MenuItem;
31
32
import ru.orangesoftware.financisto.widget.AmountInput;
32
33
import ru.orangesoftware.financisto.widget.AmountInput.OnAmountChangedListener;
33
34
 
 
35
import java.io.*;
34
36
import java.util.*;
35
 
import java.util.concurrent.atomic.AtomicLong;
36
37
 
37
38
import static ru.orangesoftware.financisto.model.Category.isSplit;
38
39
import static ru.orangesoftware.financisto.utils.AndroidUtils.isSupportedApiLevel;
42
43
public class TransactionActivity extends AbstractTransactionActivity {
43
44
 
44
45
        public static final String CURRENT_BALANCE_EXTRA = "accountCurrentBalance";
 
46
    public static final String ACTIVITY_STATE = "ACTIVITY_STATE";
45
47
 
46
48
        private static final int MENU_TURN_GPS_ON = Menu.FIRST;
47
49
    private static final int SPLIT_REQUEST = 5001;
48
50
 
49
 
    private final AtomicLong idSequence = new AtomicLong();
 
51
    private long idSequence = 0;
50
52
    private final IdentityHashMap<View, Transaction> viewToSplitMap = new IdentityHashMap<View, Transaction>();
51
53
 
52
54
    private AutoCompleteTextView payeeText;
438
440
 
439
441
    private void createSplit(boolean asTransfer) {
440
442
        Transaction split = new Transaction();
441
 
        split.id = idSequence.decrementAndGet();
 
443
        split.id = --idSequence;
442
444
        split.fromAccountId = selectedAccountId;
443
445
        split.fromAmount = split.unsplitAmount = calculateUnsplitAmount();
444
446
        editSplit(split, asTransfer ? SplitTransferActivity.class : SplitTransactionActivity.class);
461
463
        }
462
464
    }
463
465
 
 
466
    @Override
 
467
    protected void onSaveInstanceState(Bundle outState) {
 
468
        super.onSaveInstanceState(outState);
 
469
        Log.d("Financisto", "onSaveInstanceState");
 
470
        try {
 
471
            if (selectedCategoryId == Category.SPLIT_CATEGORY_ID) {
 
472
                Log.d("Financisto", "Saving splits...");
 
473
                ActivityState state = new ActivityState();
 
474
                state.categoryId = selectedCategoryId;
 
475
                state.idSequence = idSequence;
 
476
                state.splits = new ArrayList<Transaction>(viewToSplitMap.values());
 
477
                ByteArrayOutputStream s = new ByteArrayOutputStream();
 
478
                try {
 
479
                    ObjectOutputStream out = new ObjectOutputStream(s);
 
480
                    out.writeObject(state);
 
481
                    outState.putByteArray(ACTIVITY_STATE, s.toByteArray());
 
482
                } finally {
 
483
                    s.close();
 
484
                }
 
485
            }
 
486
        } catch (IOException e) {
 
487
            Log.e("Financisto", "Unable to save state", e);
 
488
        }
 
489
    }
 
490
 
 
491
    @Override
 
492
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
 
493
        super.onRestoreInstanceState(savedInstanceState);
 
494
        Log.d("Financisto", "onRestoreInstanceState");
 
495
        byte[] bytes = savedInstanceState.getByteArray(ACTIVITY_STATE);
 
496
        if (bytes != null) {
 
497
            try {
 
498
                ByteArrayInputStream s = new ByteArrayInputStream(bytes);
 
499
                try {
 
500
                    ObjectInputStream in = new ObjectInputStream(s);
 
501
                    ActivityState state = (ActivityState) in.readObject();
 
502
                    if (state.categoryId == Category.SPLIT_CATEGORY_ID) {
 
503
                        Log.d("Financisto", "Restoring splits...");
 
504
                        viewToSplitMap.clear();
 
505
                        splitsLayout.removeAllViews();
 
506
                        idSequence = state.idSequence;
 
507
                        selectCategory(state.categoryId);
 
508
                        for (Transaction split : state.splits) {
 
509
                            addOrEditSplit(split);
 
510
                        }
 
511
                    }
 
512
                } finally {
 
513
                    s.close();
 
514
                }
 
515
            } catch (Exception e) {
 
516
                Log.e("Financisto", "Unable to restore state", e);
 
517
            }
 
518
        }
 
519
    }
 
520
 
464
521
    private void addOrEditSplit(Transaction split) {
465
522
        View v = findView(split);
466
523
        if (v  == null) {
543
600
        super.onDestroy();
544
601
    }
545
602
 
 
603
    private static class ActivityState implements Serializable {
 
604
        public long categoryId;
 
605
        public long idSequence;
 
606
        public List<Transaction> splits;
 
607
    }
 
608
    
546
609
 
547
610
}