Snippets

A5.ua Mobile Development Sugar CRM and more

Created by Olga Korokhina

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+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;
+    }
+    @Override
+    public WordModel getModel(int category, int difficulty, String locale) {
+        if(locale.contains("россия") || locale.contains("укра")){
+            locale = "ru_UA";
+        }else locale="en_EN";
+        List<WordModel> tmp;
+        do {
+            tmp = Select.from(WordModel.class)
+                    .where(Condition.prop("diff_id").eq(difficulty),
+                            Condition.prop("category_id").eq(category),
+                            Condition.prop("locale").eq(locale)).list();
+            difficulty++;
+        }while (tmp.isEmpty());
+
+        return tmp.get(new Random().nextInt(tmp.size()));
+    }
+
+    @Override
+    public boolean isDBExist() {
+        allWords = WordModel.listAll(WordModel.class);
+        if(allWords.isEmpty())
+        return false;
+        else return true;
+    }
+
+    @Override
+    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());
+                        Log.d("MyLogs", json);
+            SugarRecord.saveInTx(createdDB);
+            ins = context.getResources().openRawResource(
+                    context.getResources().getIdentifier("words_" + i + "_hm_en",
+                            "raw", context.getPackageName()));
+
+             json = readFromRaw(ins);
+             gson = new Gson();
+             listType = new TypeToken<List<WordModel>>() {}.getType();
+            createdDB = gson.fromJson(json,listType);
+            Log.d("MyLogs", "model count = "+ createdDB.size());
+            Log.d("MyLogs", json);
+            SugarRecord.saveInTx(createdDB);
+
+        }
+        return 0;
+    }
+
+    @Override
+    public void manipulateInTransaction() {
+
+    }
+    private String readFromRaw(InputStream inputStream){
+        BufferedReader is = null;
+            is = new BufferedReader(new InputStreamReader(inputStream));
+
+        StringBuilder sb = new StringBuilder();
+        try {
+            String line;
+            while ((line = is.readLine()) != null) {
+                sb.append(line);
+            }
+
+            inputStream.reset();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        return sb.toString();
+    }
+}
+
+//interface for reposytory above
+
+public interface Repository {
+     interface Splash{
+         boolean isDBExist();
+         int createDB();
+    }
+    WordModel getModel(int difficulty, int category, String locale);
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.