-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit_Deck.java
More file actions
169 lines (151 loc) · 6.95 KB
/
Copy pathEdit_Deck.java
File metadata and controls
169 lines (151 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.example.thoma.SmartStudy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Edit_Deck extends AppCompatActivity {
private int position;
private int cardNum = 0;
private int side = 0;
private boolean update = true;
EditText editText;
EditText editTextTitle;
TextView sideView;
TextView numberView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit__deck);
Intent intent = getIntent();
position = intent.getIntExtra("position", 0);
Log.d("Edit_Deck", "Decks.get(position).getSize() = " + MainActivity.Decks.get(position).getSize());
editText = findViewById(R.id.Edit_Text);
editTextTitle = findViewById(R.id.Edit_Text_Title);
sideView = findViewById(R.id.Edit_Deck_Side_View);
numberView = findViewById(R.id.Edit_Deck_Card_Number);
cardNum = 0;
editTextTitle.setText(MainActivity.Decks.get(position).getName());
//Deal with no cards case
if(MainActivity.Decks.get(position).getSize() == 0) NoCards();
else {
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
sideView.setText("Front");
numberView.setText((cardNum + 1) + "/" + MainActivity.Decks.get(position).getSize());
}
}
@Override
protected void onPause(){
//If the deck has been deleted or there are no cards left we don't
//want to save what's currently on the screen
if(update) UpdateDeck();
super.onPause();
}
//two Next() functions so we can call it from the button and from
//within code
public void Next(View v){Next();}
public void Next(){
//Deal with no Cards case
if(MainActivity.Decks.get(position).getSize() == 0) NoCards();
else {
UpdateDeck();
//use Mod to go from end to beginning
cardNum = Mod((cardNum + 1), MainActivity.Decks.get(position).getSize());
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
sideView.setText("Front");
numberView.setText((cardNum + 1) + "/" + MainActivity.Decks.get(position).getSize());
side = 0;//we always want to start on the front
}
}
public void Last(View v){
//Deal with no cards case
if(MainActivity.Decks.get(position).getSize() == 0) NoCards();
else {
UpdateDeck();
//Use Mod to go from beginning to end
cardNum = Mod((cardNum - 1), MainActivity.Decks.get(position).getSize());
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
sideView.setText("Front");
numberView.setText((cardNum + 1) + "/" + MainActivity.Decks.get(position).getSize());
side = 0;//always want to start on the front side
}
}
public void Flip(View v){
//Deal with no cards case
if(MainActivity.Decks.get(position).getSize() == 0) NoCards();
else {
UpdateDeck();
if (side == 0)
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getBack());
else editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
side = (side + 1) % 2;
if(side == 0) sideView.setText("Front");
else sideView.setText("Back");
}
}
public void UpdateDeck(){
//Put whatever is currently in the edit text fields into appropriate card
if(side == 0) MainActivity.Decks.get(position).getCard(cardNum).ModifyFront(editText.getText().toString());
else MainActivity.Decks.get(position).getCard(cardNum).ModifyBack(editText.getText().toString());
MainActivity.Decks.get(position).editName(editTextTitle.getText().toString());
}
public void New(View v){
//Make a new card with default front and back
//Then move to that card so the user can modify it
MainActivity.Decks.get(position).addCard(new Card("New card's front", "New card's back"));
cardNum = MainActivity.Decks.get(position).getSize() - 1;
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
sideView.setText("Front");
numberView.setText((cardNum + 1) + "/" + MainActivity.Decks.get(position).getSize());
//If we had already said that we shouldn't be updating because there are no cards
//We need to change that because there are some cards now
update = true;
}
public void Delete(View v){
//Definitely don't want to delete anything if there isn't anything to delete
if(MainActivity.Decks.get(position).getSize() == 0) NoCards();
else {
MainActivity.Decks.get(position).removeCard(cardNum);
//if there are still some cards left just move to one of them
if (MainActivity.Decks.get(position).getSize() != 0){
cardNum = Mod((cardNum - 1), MainActivity.Decks.get(position).getSize());
editText.setText(MainActivity.Decks.get(position).getCard(cardNum).getFront());
numberView.setText((cardNum + 1) + "/" + MainActivity.Decks.get(position).getSize());
sideView.setText("Front");
}
//if there aren't any cards left now then we don't want to
//be updating anymore and we can just call NoCards()
else{
update = false;
NoCards();
}
Log.d("", String.valueOf(cardNum));
side = 0;//Always want to start on the front
}
}
public int Mod(int x, int y){
//Java doesn't have an actual Modulo function
//% is just a remainder opperation so it sometimes gives
//negative numbers. We don't want that so we made our
//own Modulo function
int z = x%y;
if(z < 0) z += y;
return z;
}
public void deleteDeck(View v){
Toast.makeText(this, String.valueOf(MainActivity.Decks.get(0).getName()) + "has been deleted", Toast.LENGTH_LONG).show();
MainActivity.Decks.remove(position);
update = false;//set update to false wo we don't try to save something into the deck we just deleted
//Go back to Edit_Decks Activity
Intent intent = new Intent(this, Edit_Decks.class);
startActivity(intent);
}
public void NoCards(){
editText.setText("There are no cards in this deck yet");
sideView.setText("ERROR");
numberView.setText("ERROR");
}
}