2019-05-29 05:48:41 +02:00
import { SettingsCookie } from "data" ;
2019-05-31 07:53:11 +02:00
import DataStore from "./datastore" ;
2019-05-29 05:48:41 +02:00
import ContentManager from "./contentmanager" ;
import BdApi from "./pluginapi" ;
2019-06-03 22:25:08 +02:00
// import EmoteModule from "./emotes";
2019-05-30 17:44:05 +02:00
import Events from "./emitter" ;
2019-05-31 07:53:11 +02:00
import WebpackModules from "./webpackmodules" ;
2019-05-29 05:48:41 +02:00
import { SettingsPanel as SettingsRenderer } from "ui" ;
2019-05-31 07:53:11 +02:00
import Utilities from "./utilities" ;
2019-06-04 06:18:15 +02:00
import { Toasts } from "ui" ;
2019-05-31 07:53:11 +02:00
//WebpackModules.getModule(m => m.getSection && m.getProps && !m.getGuildId && !m.getChannel)
//WebpackModules.getByProps("getGuildId", "getSection")
2019-05-29 05:48:41 +02:00
2019-06-04 21:17:23 +02:00
export default new class SettingsManager {
2019-05-29 05:48:41 +02:00
constructor ( ) {
this . renderer = new SettingsRenderer ( { onChange : this . updateSettings . bind ( this ) } ) ;
}
2019-05-31 07:53:11 +02:00
initialize ( ) {
DataStore . initialize ( ) ;
if ( ! DataStore . getSettingGroup ( "settings" ) ) return this . saveSettings ( ) ;
const savedSettings = this . loadSettings ( ) ;
$ ( "<style id=\"customcss\">" ) . text ( atob ( DataStore . getBDData ( "bdcustomcss" ) ) ) . appendTo ( document . head ) ;
for ( const setting in savedSettings ) {
if ( savedSettings [ setting ] !== undefined ) SettingsCookie [ setting ] = savedSettings [ setting ] ;
}
this . saveSettings ( ) ;
this . patchSections ( ) ;
}
async patchSections ( ) {
const UserSettings = await this . getUserSettings ( ) ; // data.returnValue.type;
Utilities . monkeyPatch ( UserSettings . prototype , "generateSections" , { after : ( data ) => {
2019-06-04 21:17:23 +02:00
console . log ( data ) ; /* eslint-disable-line no-console */
2019-05-31 07:53:11 +02:00
data . returnValue . splice ( 23 , 0 , { section : "DIVIDER" } ) ;
data . returnValue . splice ( 24 , 0 , { section : "HEADER" , label : "BandagedBD" } ) ;
2019-06-04 06:18:15 +02:00
data . returnValue . splice ( 25 , 0 , { section : "BBD Settings" , label : "Settings" , element : ( ) => this . renderer . core2 } ) ;
data . returnValue . splice ( 26 , 0 , { section : "BBD Test" , label : "Test Tab" , onClick : function ( ) { Toasts . success ( "This can just be a click listener!" , { forceShow : true } ) ; } } ) ;
2019-06-04 16:21:07 +02:00
data . returnValue . splice ( 27 , 0 , { section : "CUSTOM" , element : ( ) => this . renderer . attribution } ) ;
2019-05-31 07:53:11 +02:00
} } ) ;
const viewClass = WebpackModules . getByProps ( "standardSidebarView" ) . standardSidebarView . split ( " " ) [ 0 ] ;
const node = document . querySelector ( ` . ${ viewClass } ` ) ;
Utilities . getInternalInstance ( node ) . return . return . return . return . return . return . stateNode . forceUpdate ( ) ;
}
getUserSettings ( ) {
return new Promise ( resolve => {
const cancel = Utilities . monkeyPatch ( WebpackModules . getByProps ( "getUserSettingsSections" ) . default . prototype , "render" , { after : ( data ) => {
resolve ( data . returnValue . type ) ;
data . thisObject . forceUpdate ( ) ;
cancel ( ) ;
} } ) ;
} ) ;
}
saveSettings ( ) {
DataStore . setSettingGroup ( "settings" , SettingsCookie ) ;
}
loadSettings ( ) {
return DataStore . getSettingGroup ( "settings" ) ;
}
2019-05-29 05:48:41 +02:00
updateSettings ( id , enabled ) {
2019-05-30 17:44:05 +02:00
Events . dispatch ( "setting-updated" , "Modules" , id , enabled ) ;
2019-05-29 05:48:41 +02:00
SettingsCookie [ id ] = enabled ;
2019-06-03 22:25:08 +02:00
// if (id == "bda-es-4") {
// if (enabled) EmoteModule.autoCapitalize();
// else EmoteModule.disableAutoCapitalize();
// }
2019-05-29 05:48:41 +02:00
if ( id == "fork-ps-5" ) {
if ( enabled ) {
ContentManager . watchContent ( "plugin" ) ;
ContentManager . watchContent ( "theme" ) ;
}
else {
ContentManager . unwatchContent ( "plugin" ) ;
ContentManager . unwatchContent ( "theme" ) ;
}
}
if ( id == "fork-wp-1" ) {
BdApi . setWindowPreference ( "transparent" , enabled ) ;
if ( enabled ) BdApi . setWindowPreference ( "backgroundColor" , null ) ;
else BdApi . setWindowPreference ( "backgroundColor" , "#2f3136" ) ;
}
2019-05-31 07:53:11 +02:00
this . saveSettings ( ) ;
2019-05-29 05:48:41 +02:00
}
initializeSettings ( ) {
2019-06-03 22:25:08 +02:00
// if (SettingsCookie["bda-es-4"]) EmoteModule.autoCapitalize();
2019-05-29 05:48:41 +02:00
if ( SettingsCookie [ "fork-ps-5" ] ) {
ContentManager . watchContent ( "plugin" ) ;
ContentManager . watchContent ( "theme" ) ;
}
2019-05-31 07:53:11 +02:00
this . saveSettings ( ) ;
2019-05-29 05:48:41 +02:00
}
} ;