mirror of
https://git.alemi.dev/harvard-android.git
synced 2024-11-23 07:54:51 +01:00
chore: store sentences list as instance
This commit is contained in:
parent
235de2ebd7
commit
4ab63fd07d
3 changed files with 29 additions and 26 deletions
|
@ -3,8 +3,8 @@ package dev.alemi.sentences;
|
||||||
import dev.alemi.R;
|
import dev.alemi.R;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
@ -14,21 +14,23 @@ public class MainActivity extends AppCompatActivity {
|
||||||
TextView currSentence;
|
TextView currSentence;
|
||||||
FloatingActionButton buttonNext;
|
FloatingActionButton buttonNext;
|
||||||
String[] sentences;
|
String[] sentences;
|
||||||
|
SentenceList list;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
this.list = new SentenceList();
|
||||||
this.currSentence = findViewById(R.id.currSentence);
|
this.currSentence = findViewById(R.id.currSentence);
|
||||||
this.buttonNext = findViewById(R.id.button);
|
this.buttonNext = findViewById(R.id.button);
|
||||||
this.buttonNext.setVisibility(View.VISIBLE);
|
|
||||||
|
|
||||||
this.buttonNext.setOnClickListener(view -> {
|
this.buttonNext.setOnClickListener(view -> {
|
||||||
this.currSentence.setText(SentenceList.next());
|
int index = this.list.step();
|
||||||
|
Toast.makeText(getApplicationContext(), String.format("#%d", index), Toast.LENGTH_SHORT).show();
|
||||||
|
this.currSentence.setText(this.list.current());
|
||||||
});
|
});
|
||||||
|
|
||||||
this.currSentence.setText(SentenceList.next());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,34 @@ package dev.alemi.sentences;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import android.widget.Toast;
|
public class SentenceList {
|
||||||
|
private Queue<Integer> order = new LinkedList<>();
|
||||||
|
private String cur = SentenceList.sentences[0];
|
||||||
|
|
||||||
// TODO do a resource rather than this horrible solution...
|
public String current() {
|
||||||
public final class SentenceList {
|
return this.cur;
|
||||||
private static Queue<Integer> order = new LinkedList<>();
|
}
|
||||||
|
|
||||||
public static String[] sentences = {
|
public int step() {
|
||||||
|
if (this.order.size() <= 0) {
|
||||||
|
LinkedList<Integer> range = IntStream
|
||||||
|
.range(0, SentenceList.sentences.length)
|
||||||
|
.boxed()
|
||||||
|
.collect(Collectors.toCollection(LinkedList::new));
|
||||||
|
Collections.shuffle(range);
|
||||||
|
this.order = range;
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = this.order.poll();
|
||||||
|
this.cur = SentenceList.sentences[index];
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] sentences = { // TODO maybe store them as a resource? idk
|
||||||
"The birch canoe slid on the smooth planks.",
|
"The birch canoe slid on the smooth planks.",
|
||||||
"Glue the sheet to the dark blue background.",
|
"Glue the sheet to the dark blue background.",
|
||||||
"It's easy to tell the depth of a well.",
|
"It's easy to tell the depth of a well.",
|
||||||
|
@ -735,19 +751,4 @@ public final class SentenceList {
|
||||||
"She called his name many times.",
|
"She called his name many times.",
|
||||||
"When you hear the bell, come quickly.",
|
"When you hear the bell, come quickly.",
|
||||||
};
|
};
|
||||||
|
|
||||||
public static String next() {
|
|
||||||
if (SentenceList.order.size() <= 0) {
|
|
||||||
LinkedList<Integer> range = IntStream
|
|
||||||
.range(0, SentenceList.sentences.length)
|
|
||||||
.boxed()
|
|
||||||
.collect(Collectors.toCollection(LinkedList::new));
|
|
||||||
Collections.shuffle(range);
|
|
||||||
SentenceList.order = range;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = SentenceList.order.poll();
|
|
||||||
|
|
||||||
return SentenceList.sentences[index];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:layout_centerVertical="true"
|
android:layout_centerVertical="true"
|
||||||
android:text="Harvard Sentences"
|
android:text="Press the floating button to cycle at random the 720 harvard sentences. No duplicates will be shown until all sentences have been cycled (or the app is restarted)."
|
||||||
android:textSize="24dp"
|
android:textSize="24dp"
|
||||||
android:padding="24dp"
|
android:padding="24dp"
|
||||||
tools:context=".MainActivity"
|
tools:context=".MainActivity"
|
||||||
|
|
Loading…
Reference in a new issue