Skip to content

Commit

Permalink
финальные шаги (надеюсь)
Browse files Browse the repository at this point in the history
  • Loading branch information
FazziCLAY committed Feb 4, 2022
1 parent 03a9ae2 commit ff3f00e
Show file tree
Hide file tree
Showing 28 changed files with 1,199 additions and 203 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SchoolGuide">

<service
android:name=".app.global.AutoGlobalUpdateService"
android:enabled="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
Expand Down Expand Up @@ -51,6 +52,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(SharedConstrains.getAppNullView(this));
return;
}

startActivity(PresetListActivity.getLaunchIntent(this));
finish();
}
Expand Down
123 changes: 65 additions & 58 deletions app/src/main/java/ru/fazziclay/schoolguide/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,96 +1,103 @@
package ru.fazziclay.schoolguide;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import ru.fazziclay.schoolguide.app.SchoolGuideApp;
import ru.fazziclay.schoolguide.app.Settings;
import ru.fazziclay.schoolguide.databinding.SettingsActivityBinding;
import ru.fazziclay.schoolguide.app.global.AutoGlobalUpdateService;
import ru.fazziclay.schoolguide.app.listener.PresetListUpdateListener;
import ru.fazziclay.schoolguide.callback.CallbackStorage;
import ru.fazziclay.schoolguide.callback.Status;

public class SettingsActivity extends AppCompatActivity {
private static final String KEY_IS_DEVELOPER_FEATURES = "isDeveloperFeatures";
private static final String KEY_IS_BUILTIN_PRESET_LIST = "isBuiltinPresetList";
private static final String KEY_IS_SHOW_EMPTY_NOTIFICATION = "isShowEmptyNotification";

public static Intent getLaunchIntent(Context context) {
return new Intent(context, SettingsActivity.class);
}

private SchoolGuideApp app;
private Settings settings;
private SettingsActivityBinding binding;

private SharedPreferences preferences;

private final SharedPreferences.OnSharedPreferenceChangeListener listener = (sharedPreferences, key) -> {
boolean contains = sharedPreferences.contains(key);
if (!contains) return;

switch (key) {
case KEY_IS_DEVELOPER_FEATURES:
settings.isDeveloperFeatures = sharedPreferences.getBoolean(key, false);
break;

case KEY_IS_BUILTIN_PRESET_LIST:
settings.isBuiltInPresetList = sharedPreferences.getBoolean(key, false);
AutoGlobalUpdateService.update(app);
break;

case KEY_IS_SHOW_EMPTY_NOTIFICATION:
settings.isStopForegroundIsNone = !sharedPreferences.getBoolean(key, false);
break;
}
app.saveSettings();
app.getPresetListUpdateCallbacks().run((callbackStorage, callback) -> callback.onPresetListUpdate());
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

app = SchoolGuideApp.get(this);
if (app == null) {
setContentView(SharedConstrains.getAppNullView(this));
return;
}
settings = app.getSettings();

binding = SettingsActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setContentView(R.layout.activity_settings);
setTitle(R.string.settings_activityTitle);

if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
finish();
return;
}

setActualState(settings);
setCallbacks();
}
preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit()
.putBoolean(KEY_IS_SHOW_EMPTY_NOTIFICATION, !settings.isStopForegroundIsNone)
.putBoolean(KEY_IS_DEVELOPER_FEATURES, settings.isDeveloperFeatures)
.putBoolean(KEY_IS_BUILTIN_PRESET_LIST, settings.isBuiltInPresetList)
.apply();

private void setActualState(Settings settings) {
binding.developerFeatures.setChecked(settings.developerFeatures);
binding.stopForegroundIsNone.setChecked(settings.stopForegroundIsNone);
binding.isFirstMonday.setChecked(settings.isFirstMonday);
binding.syncGlobalPresetList.setChecked(settings.globalPresetListSync);
preferences.registerOnSharedPreferenceChangeListener(listener);
}

private void setCallbacks() {
binding.developerFeatures.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.developerFeatures = isChecked;
save();
});

binding.stopForegroundIsNone.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.stopForegroundIsNone = isChecked;
save();
});

binding.isFirstMonday.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.isFirstMonday = isChecked;
save();
});

binding.syncGlobalPresetList.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.globalPresetListSync = isChecked;
save();
});

binding.changeScheduleNotifyBeforeTime.setOnClickListener(v -> {
EditText editText = new EditText(this);
editText.setText(String.valueOf(settings.scheduleNotifyBeforeTime));

new AlertDialog.Builder(this)
.setView(editText)
.setPositiveButton("APPLY", (ig, ign) -> {
try {
settings.scheduleNotifyBeforeTime = Integer.parseInt(editText.getText().toString());
} catch (Exception e) {
app.getAppTrace().point("changeScheduleNotifyBeforeTimeDialog", e);
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
save();
})
.show();
});
@Override
protected void onDestroy() {
super.onDestroy();
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}

private void save() {
app.saveSettings();
Snackbar snackbar = Snackbar.make(binding.getRoot(), "Saved!", Snackbar.LENGTH_SHORT);
snackbar.show();
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings_preferences, rootKey);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import java.util.List;

import ru.fazziclay.schoolguide.SharedConstrains;
import ru.fazziclay.schoolguide.app.listener.GlobalUpdateListener;
import ru.fazziclay.schoolguide.app.global.AutoGlobalUpdateService;
import ru.fazziclay.schoolguide.app.global.GlobalBuiltinPresetList;
import ru.fazziclay.schoolguide.app.global.GlobalVersionManifest;
import ru.fazziclay.schoolguide.app.listener.PresetListUpdateListener;
import ru.fazziclay.schoolguide.app.scheduleinformator.ScheduleInformatorApp;
import ru.fazziclay.schoolguide.callback.CallbackImportance;
import ru.fazziclay.schoolguide.callback.CallbackStorage;
Expand Down Expand Up @@ -156,6 +158,8 @@ public static SchoolGuideApp get() {
* **/
private final CallbackStorage<GlobalUpdateListener> globalUpdateCallbacks = new CallbackStorage<>();

private final CallbackStorage<PresetListUpdateListener> presetListUpdateCallbacks = new CallbackStorage<>();


public SchoolGuideApp(Context context) {
if (context == null) {
Expand Down Expand Up @@ -320,4 +324,8 @@ public void setGlobalBuiltinPresetList(GlobalBuiltinPresetList globalBuiltinPres
public CallbackStorage<GlobalUpdateListener> getGlobalUpdateCallbacks() {
return globalUpdateCallbacks;
}

public CallbackStorage<PresetListUpdateListener> getPresetListUpdateCallbacks() {
return presetListUpdateCallbacks;
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/ru/fazziclay/schoolguide/app/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import ru.fazziclay.schoolguide.app.scheduleinformator.android.PresetEditActivity;

public class Settings {
public boolean developerFeatures = false;
public boolean isDeveloperFeatures = false;
public int scheduleNotifyBeforeTime = 2*60*60;
public boolean stopForegroundIsNone = true;
public boolean isStopForegroundIsNone = true;
public boolean isFirstMonday = true;
public PresetEditActivity.ColorScheme presetEditColorScheme = PresetEditActivity.ColorScheme.DEFAULT;

public boolean globalPresetListSync = false;
public boolean isBuiltInPresetList = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import ru.fazziclay.schoolguide.SharedConstrains;
import ru.fazziclay.schoolguide.app.SchoolGuideApp;
import ru.fazziclay.schoolguide.callback.CallbackStorage;
import ru.fazziclay.schoolguide.app.GlobalUpdateListener;
import ru.fazziclay.schoolguide.app.listener.GlobalUpdateListener;

public class AutoGlobalUpdateService extends Service {
private SchoolGuideApp app;
private CallbackStorage<GlobalUpdateListener> callbacks;
private Handler handler;
private Runnable runnable;

Expand All @@ -29,21 +28,10 @@ public void onCreate() {
stopSelf();
return;
}
callbacks = app.getGlobalUpdateCallbacks();

handler = new Handler(getMainLooper());
runnable = () -> {
GlobalManager.get(app, new GlobalManager.GlobalManagerInterface() {
@Override
public void failed(Exception exception) {}

@Override
public void success(GlobalKeys keys, GlobalVersionManifest versionManifest, GlobalBuiltinPresetList builtinSchedule) {
app.setGlobalVersionManifest(versionManifest);
app.setGlobalBuiltinPresetList(builtinSchedule);
callbacks.run((callbackStorage, callback) -> callback.onGlobalUpdate(keys, versionManifest, builtinSchedule));
}
});
update(app);
handler.postDelayed(runnable, 60 * 60 * 1000);
};
}
Expand All @@ -58,4 +46,18 @@ public int onStartCommand(Intent intent, int flags, int startId) {
public IBinder onBind(Intent intent) {
return null;
}

public static void update(SchoolGuideApp app) {
GlobalManager.get(app, new GlobalManager.GlobalManagerInterface() {
@Override
public void failed(Exception exception) {}

@Override
public void success(GlobalKeys keys, GlobalVersionManifest versionManifest, GlobalBuiltinPresetList builtinSchedule) {
app.setGlobalVersionManifest(versionManifest);
app.setGlobalBuiltinPresetList(builtinSchedule);
app.getGlobalUpdateCallbacks().run((callbackStorage, callback) -> callback.onGlobalUpdate(keys, versionManifest, builtinSchedule));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.fazziclay.schoolguide.app;
package ru.fazziclay.schoolguide.app.listener;

import ru.fazziclay.schoolguide.app.global.GlobalBuiltinPresetList;
import ru.fazziclay.schoolguide.app.global.GlobalKeys;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.fazziclay.schoolguide.app.listener;

import ru.fazziclay.schoolguide.callback.Callback;
import ru.fazziclay.schoolguide.callback.Status;

public interface PresetListUpdateListener extends Callback {
Status onPresetListUpdate();
}
Loading

0 comments on commit ff3f00e

Please sign in to comment.