<commit_before>package me.zchang.onchart.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;

import me.zchang.onchart.BuildConfig;
import me.zchang.onchart.R;
import me.zchang.onchart.student.Course;


/*
 *    Copyright 2015 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

public class ConfigManager {
    private static String SETTING_FILE;

    final static String CHART_FILE_NAME = "chart.js";

    Context context;
    SharedPreferences sp;
    CourseSQLiteHelper courseSQLiteHelper;

	OnConfigChangeListner configChangeListner;

    private boolean firstLaunch = false;

    public final static int labelImgIndices[] = {
            R.mipmap.little_label1,
            R.mipmap.autumn,
            R.mipmap.winter,
            R.mipmap.spring,
            R.mipmap.night
    };

	public void registerListener(OnConfigChangeListner listener) {
		configChangeListner = listener;
		sp.registerOnSharedPreferenceChangeListener(listener);
    }

	public void unRegisterListener(OnConfigChangeListner listener) {
		configChangeListner = null;
		sp.unregisterOnSharedPreferenceChangeListener(listener);
    }

	public ConfigManager(Context context) {
		this.context = context;
        SETTING_FILE = context.getString(R.string.pref_file_name);

        sp = context.getSharedPreferences(SETTING_FILE, Context.MODE_PRIVATE);
        courseSQLiteHelper = new CourseSQLiteHelper(context, context.getString(R.string.course_database_name), null, 1);

        if (getLastVersionCode() != BuildConfig.VERSION_CODE) {
            firstLaunch = true;
            saveLastVersionCode(BuildConfig.VERSION_CODE);
            // create the database at the first launch.
            SQLiteDatabase courseDatabase = courseSQLiteHelper.getWritableDatabase();
            courseDatabase.close();
        }
    }

	public ConfigManager saveSchedule(List<Course> courses) {
		courseSQLiteHelper.addCourses(courses);
        return this;
    }

    public List<Course> getSchedule() {
        return courseSQLiteHelper.getCourses();
    }

	public ConfigManager deleteSchedule() {
		courseSQLiteHelper.clearCourses();
        return this;
    }

    public boolean insertCourse(Course course) {
	    boolean result = courseSQLiteHelper.insertCourse(course);
	    if (configChangeListner != null)
		    configChangeListner.onInsertCourse(course);
	    return result;
    }

	public ConfigManager saveImgPathIndex(long key, int resIndex) {
		courseSQLiteHelper.setImgPathIndex(key, resIndex);
        return this;
    }

    public int getImgPathIndex(int key) {<commit_msg>Fix bug of inserting course.<commit_after>package me.zchang.onchart.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;

import me.zchang.onchart.BuildConfig;
import me.zchang.onchart.R;
import me.zchang.onchart.student.Course;


/*
 *    Copyright 2015 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

public class ConfigManager {
    private static String SETTING_FILE;

    final static String CHART_FILE_NAME = "chart.js";

    Context context;
    SharedPreferences sp;
    CourseSQLiteHelper courseSQLiteHelper;

	OnConfigChangeListner configChangeListner;

    private boolean firstLaunch = false;

    public final static int labelImgIndices[] = {
            R.mipmap.little_label1,
            R.mipmap.autumn,
            R.mipmap.winter,
            R.mipmap.spring,
            R.mipmap.night
    };

	public void registerListener(OnConfigChangeListner listener) {
		configChangeListner = listener;
		sp.registerOnSharedPreferenceChangeListener(listener);
    }

	public void unRegisterListener(OnConfigChangeListner listener) {
		configChangeListner = null;
		sp.unregisterOnSharedPreferenceChangeListener(listener);
    }

	public ConfigManager(Context context) {
		this.context = context;
        SETTING_FILE = context.getString(R.string.pref_file_name);

        sp = context.getSharedPreferences(SETTING_FILE, Context.MODE_PRIVATE);
        courseSQLiteHelper = new CourseSQLiteHelper(context, context.getString(R.string.course_database_name), null, 1);

        if (getLastVersionCode() != BuildConfig.VERSION_CODE) {
            firstLaunch = true;
            saveLastVersionCode(BuildConfig.VERSION_CODE);
            // create the database at the first launch.
            SQLiteDatabase courseDatabase = courseSQLiteHelper.getWritableDatabase();
            courseDatabase.close();
        }
    }

	public ConfigManager saveSchedule(List<Course> courses) {
		courseSQLiteHelper.addCourses(courses);
        return this;
    }

    public List<Course> getSchedule() {
        return courseSQLiteHelper.getCourses();
    }

	public ConfigManager deleteSchedule() {
		courseSQLiteHelper.clearCourses();
        return this;
    }

    public boolean insertCourse(Course course) {
	    boolean result = courseSQLiteHelper.insertCourse(course);
        if (configChangeListner != null && result)
            configChangeListner.onInsertCourse(course);
	    return result;
    }

	public ConfigManager saveImgPathIndex(long key, int resIndex) {
		courseSQLiteHelper.setImgPathIndex(key, resIndex);
        return this;
    }

    public int getImgPathIndex(int key) {
{'fraction_non_alphanumeric': 0.07841614906832298, 'fraction_numerical': 0.003105590062111801, 'mean_word_length': 3.3786539768864716, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7057955', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1744, 'n_words': 451}