+public class WordsRepositoryImpl implements Repository, Repository.Splash, SugarTransactionHelper.Callback{
+ private Context context;
+ private List<WordModel> allWords;
+ private List<WordModel> createdDB;
+ public WordsRepositoryImpl(Context context){
+ this.context = context;
+ public WordModel getModel(int category, int difficulty, String locale) {
+ if(locale.contains("россия") || locale.contains("укра")){
+ tmp = Select.from(WordModel.class)
+ .where(Condition.prop("diff_id").eq(difficulty),
+ Condition.prop("category_id").eq(category),
+ Condition.prop("locale").eq(locale)).list();
+ }while (tmp.isEmpty());
+ return tmp.get(new Random().nextInt(tmp.size()));
+ public boolean isDBExist() {
+ allWords = WordModel.listAll(WordModel.class);
+ public int createDB() {
+ for(int i=1; i<=10; i++){
+ InputStream ins = context.getResources().openRawResource(
+ context.getResources().getIdentifier("words_"+i+"_hm",
+ "raw", context.getPackageName()));
+ String json = readFromRaw(ins);
+ Gson gson = new Gson();
+ Type listType = new TypeToken<List<WordModel>>() {}.getType();
+ createdDB = gson.fromJson(json,listType);
+ Log.d("MyLogs", "model count = "+ createdDB.size());
+ SugarRecord.saveInTx(createdDB);
+ ins = context.getResources().openRawResource(
+ context.getResources().getIdentifier("words_" + i + "_hm_en",
+ "raw", context.getPackageName()));
+ json = readFromRaw(ins);
+ listType = new TypeToken<List<WordModel>>() {}.getType();
+ createdDB = gson.fromJson(json,listType);
+ Log.d("MyLogs", "model count = "+ createdDB.size());
+ SugarRecord.saveInTx(createdDB);
+ public void manipulateInTransaction() {
+ private String readFromRaw(InputStream inputStream){
+ BufferedReader is = null;
+ is = new BufferedReader(new InputStreamReader(inputStream));
+ StringBuilder sb = new StringBuilder();
+ while ((line = is.readLine()) != null) {
+ } catch (IOException e) {
+//interface for reposytory above
+public interface Repository {
+ WordModel getModel(int difficulty, int category, String locale);