Skip to content

Commit

Permalink
Merge pull request #65 from elimu-ai/35-Use-different-StoryBook-font-…
Browse files Browse the repository at this point in the history
…size-and-letter-spacing-per-ReadingLevel

#35 text size by reading level
  • Loading branch information
nya-elimu authored Dec 11, 2020
2 parents e7ed5fc + 166d5b0 commit b8c0924
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public void onSingleClick(View v) {

Intent intent = new Intent(getApplicationContext(), StoryBookActivity.class);
intent.putExtra(StoryBookActivity.EXTRA_KEY_STORYBOOK_ID, finalStoryBook.getId());
intent.putExtra(StoryBookActivity.EXTRA_KEY_STORYBOOK_LEVEL, finalStoryBook.getReadingLevel());
startActivity(intent);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
Expand All @@ -33,6 +32,7 @@

import ai.elimu.analytics.utils.LearningEventUtil;
import ai.elimu.content_provider.utils.ContentProviderHelper;
import ai.elimu.model.enums.ReadingLevel;
import ai.elimu.model.enums.analytics.LearningEventType;
import ai.elimu.model.v2.gson.content.AudioGson;
import ai.elimu.model.v2.gson.content.ImageGson;
Expand All @@ -46,17 +46,19 @@
public class ChapterFragment extends Fragment {

private static final String ARG_CHAPTER_INDEX = "chapter_index";
private static final String ARG_READING_LEVEL = "reading_level";

private StoryBookChapterGson storyBookChapter;

private String chapterText;

private TextToSpeech tts;

public static ChapterFragment newInstance(int chapterIndex) {
public static ChapterFragment newInstance(int chapterIndex, ReadingLevel readingLevel) {
ChapterFragment fragment = new ChapterFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_CHAPTER_INDEX, chapterIndex);
bundle.putSerializable(ARG_READING_LEVEL, readingLevel);
fragment.setArguments(bundle);
return fragment;
}
Expand Down Expand Up @@ -110,8 +112,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
chapterText += storyBookParagraphGson.getOriginalText();
}

ReadingLevel readingLevel = (ReadingLevel) getArguments().get(ARG_READING_LEVEL);
int readingLevelPosition = (readingLevel == null) ? 0 : readingLevel.ordinal();

int[] fontSize = getResources().getIntArray(R.array.chapter_text_font_size);
String[] letterSpacing = getResources().getStringArray(R.array.chapter_text_letter_spacing);
String[] lineSpacing = getResources().getStringArray(R.array.chapter_text_line_spacing);

TextView textView = root.findViewById(R.id.chapter_text);
textView.setText(chapterText);
textView.setTextSize(fontSize[readingLevelPosition]);
textView.setLetterSpacing(Float.parseFloat(letterSpacing[readingLevelPosition]));
textView.setLineSpacing(0, Float.parseFloat(lineSpacing[readingLevelPosition]));
textView.setVisibility(View.VISIBLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.List;

import ai.elimu.model.enums.ReadingLevel;
import ai.elimu.model.v2.gson.content.StoryBookChapterGson;

public class ChapterPagerAdapter extends FragmentPagerAdapter {
Expand All @@ -17,15 +18,18 @@ public class ChapterPagerAdapter extends FragmentPagerAdapter {

public static List<StoryBookChapterGson> storyBookChapters;

public ChapterPagerAdapter(FragmentManager fm, Context context, List<StoryBookChapterGson> storyBookChapters) {
private ReadingLevel readingLevel;

public ChapterPagerAdapter(FragmentManager fm, Context context, List<StoryBookChapterGson> storyBookChapters, ReadingLevel readingLevel) {
super(fm);
this.context = context;
this.storyBookChapters = storyBookChapters;
this.readingLevel = readingLevel;
}

@Override
public Fragment getItem(int position) {
return ChapterFragment.newInstance(position);
return ChapterFragment.newInstance(position, readingLevel);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import ai.elimu.content_provider.utils.ContentProviderHelper;
import ai.elimu.model.enums.ReadingLevel;
import ai.elimu.model.v2.gson.content.StoryBookChapterGson;
import ai.elimu.vitabu.BuildConfig;
import ai.elimu.vitabu.R;
Expand All @@ -17,6 +18,7 @@
public class StoryBookActivity extends AppCompatActivity {

public static final String EXTRA_KEY_STORYBOOK_ID = "EXTRA_KEY_STORYBOOK_ID";
public static final String EXTRA_KEY_STORYBOOK_LEVEL = "EXTRA_KEY_STORYBOOK_LEVEL";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -27,13 +29,14 @@ protected void onCreate(Bundle savedInstanceState) {

Long storyBookId = getIntent().getLongExtra(EXTRA_KEY_STORYBOOK_ID, 0);
Log.i(getClass().getName(), "storyBookId: " + storyBookId);
ReadingLevel readingLevel = (ReadingLevel) getIntent().getSerializableExtra(EXTRA_KEY_STORYBOOK_LEVEL);

// Fetch StoryBookChapters from the elimu.ai Content Provider (see https://github.com/elimu-ai/content-provider)
List<StoryBookChapterGson> storyBookChapters = ContentProviderHelper.getStoryBookChapterGsons(storyBookId, getApplicationContext(), BuildConfig.CONTENT_PROVIDER_APPLICATION_ID);

ViewPager viewPager = findViewById(R.id.view_pager);

ChapterPagerAdapter chapterPagerAdapter = new ChapterPagerAdapter(getSupportFragmentManager(), this, storyBookChapters);
ChapterPagerAdapter chapterPagerAdapter = new ChapterPagerAdapter(getSupportFragmentManager(), this, storyBookChapters, readingLevel);
viewPager.setAdapter(chapterPagerAdapter);

ZoomOutPageTransformer zoomOutPageTransformer = new ZoomOutPageTransformer();
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,25 @@
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="bottom_sheet_corner_radius">16dp</dimen>

<array name="chapter_text_font_size">
<item>32</item>
<item>30</item>
<item>28</item>
<item>26</item>
</array>

<string-array name="chapter_text_letter_spacing">
<item>0.2</item>
<item>0.15</item>
<item>0.1</item>
<item>0.05</item>
</string-array>

<string-array name="chapter_text_line_spacing">
<item>1.5</item>
<item>1.4</item>
<item>1.3</item>
<item>1.2</item>
</string-array>
</resources>

0 comments on commit b8c0924

Please sign in to comment.