chore: store sentences list as instance

This commit is contained in:
əlemi 2023-05-04 15:38:16 +02:00
parent 235de2ebd7
commit 4ab63fd07d
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 29 additions and 26 deletions

View file

@ -3,8 +3,8 @@ package dev.alemi.sentences;
import dev.alemi.R;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AppCompatActivity;
@ -14,21 +14,23 @@ public class MainActivity extends AppCompatActivity {
TextView currSentence;
FloatingActionButton buttonNext;
String[] sentences;
SentenceList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.list = new SentenceList();
this.currSentence = findViewById(R.id.currSentence);
this.buttonNext = findViewById(R.id.button);
this.buttonNext.setVisibility(View.VISIBLE);
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());
}
}

View file

@ -2,18 +2,34 @@ package dev.alemi.sentences;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
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 final class SentenceList {
private static Queue<Integer> order = new LinkedList<>();
public String current() {
return this.cur;
}
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.",
"Glue the sheet to the dark blue background.",
"It's easy to tell the depth of a well.",
@ -735,19 +751,4 @@ public final class SentenceList {
"She called his name many times.",
"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];
}
}

View file

@ -14,7 +14,7 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="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:padding="24dp"
tools:context=".MainActivity"