Skip to content

Commit 1f18253

Browse files
macusermacuser
authored andcommitted
Revisioned view API
1 parent d33fc79 commit 1f18253

File tree

9 files changed

+182
-137
lines changed

9 files changed

+182
-137
lines changed

codeview/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 24
6-
buildToolsVersion "24.0.2"
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.1"
77

88
defaultConfig {
99
minSdkVersion 15
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -25,8 +25,8 @@ android {
2525
dependencies {
2626
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2727

28-
compile 'com.android.support:appcompat-v7:24.2.0'
29-
compile 'com.android.support:recyclerview-v7:24.2.0'
28+
compile 'com.android.support:appcompat-v7:25.0.1'
29+
compile 'com.android.support:recyclerview-v7:25.0.1'
3030
}
3131
repositories {
3232
mavenCentral()

codeview/src/main/assets/training-set/javascript/new 3.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,32 @@ var minExpectedLifetime = '20s';
6767
* @api public
6868
*/
6969

70-
function UpServer (server, file, opts) {
71-
if (this == global) return new UpServer(server, file, opts);
70+
function UpServer (server, file, options) {
71+
if (this == global) return new UpServer(server, file, options);
7272

7373
Distributor.call(this, server);
7474

7575
var self = this;
76-
opts = opts || {};
76+
options = options || {};
7777

7878
this.file = file;
79-
this.numWorkers = eq(opts.numWorkers || numWorkers, { cpus: cpus });
80-
this.workerTimeout = ms(null != opts.workerTimeout
81-
? opts.workerTimeout : workerTimeout);
82-
this.requires = opts.requires || [];
83-
this.assumeReady = opts.assumeReady === undefined ? true : !!opts.assumeReady;
84-
this.keepAlive = opts.keepAlive || false;
85-
this.minExpectedLifetime = ms(opts.minExpectedLifetime != null ? opts.minExpectedLifetime : minExpectedLifetime);
86-
if (false !== opts.workerPingInterval) {
87-
this.workerPingInterval = ms(opts.workerPingInterval || '1m');
79+
this.numWorkers = eq(options.numWorkers || numWorkers, { cpus: cpus });
80+
this.workerTimeout = ms(null != options.workerTimeout
81+
? options.workerTimeout : workerTimeout);
82+
this.requires = options.requires || [];
83+
this.assumeReady = options.assumeReady === undefined ? true : !!options.assumeReady;
84+
this.keepAlive = options.keepAlive || false;
85+
this.minExpectedLifetime = ms(options.minExpectedLifetime != null ? options.minExpectedLifetime : minExpectedLifetime);
86+
if (false !== options.workerPingInterval) {
87+
this.workerPingInterval = ms(options.workerPingInterval || '1m');
8888
}
8989

9090
this.workers = [];
9191
this.spawning = [];
9292
this.lastIndex = -1;
9393

94-
if (opts.title) {
95-
this.title = opts.title;
94+
if (options.title) {
95+
this.title = options.title;
9696
process.title = this.title + ' master';
9797
}
9898

@@ -290,15 +290,15 @@ function Worker (server) {
290290
this.server = server;
291291
this.readyState = 'spawning';
292292

293-
var opts = JSON.stringify({
293+
var options = JSON.stringify({
294294
file: server.file
295295
, requires: server.requires
296296
, assumeReady: server.assumeReady
297297
, pingInterval: server.workerPingInterval
298298
, title: server.title
299299
});
300300

301-
this.proc = fork(__dirname + '/worker.js', [opts], { env: process.env });
301+
this.proc = fork(__dirname + '/worker.js', [options], { env: process.env });
302302
this.proc.on('message', this.onMessage.bind(this));
303303
this.proc.on('exit', this.onExit.bind(this));
304304
this.pid = this.proc.pid;

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
5959
}
6060

6161
/**
62-
* Code adapter accessor.
63-
*/
64-
private fun getAdapter() = vCodeList.adapter as? AbstractCodeAdapter<*>
65-
66-
/**
67-
* Highlight code by defined programming language.
68-
* It holds the placeholder on the view until code is not highlighted.
62+
* Highlight code with defined programming language.
63+
* It holds the placeholder on view until code is not highlighted.
6964
*/
7065
private fun highlight() {
7166
getAdapter()?.highlight {
@@ -97,51 +92,95 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
9792
/**
9893
* Prepare view with default adapter & options.
9994
*/
100-
private fun prepare() {
101-
setAdapter(CodeWithNotesAdapter(context))
102-
}
95+
private fun prepare() = setAdapter(CodeWithNotesAdapter(context))
96+
97+
/**
98+
* View options accessor.
99+
*/
100+
fun getOptions(): Options? = getAdapter()?.options
103101

104102
/**
105103
* Initialize with options.
106104
*
107-
* @param opts Options
105+
* @param options Options
106+
*/
107+
fun setOptions(options: Options) = setOptions(options, false)
108+
109+
/**
110+
* Set options & initialize if needed.
111+
*
112+
* @param options Options
113+
* @param isSaveAdapter Save adapter?
108114
*/
109-
fun setOptions(opts: Options) {
110-
setAdapter(CodeWithNotesAdapter(context, opts))
115+
fun setOptions(options: Options, isSaveAdapter: Boolean = true) {
116+
setAdapter(if (isSaveAdapter)
117+
getAdapter() ?: CodeWithNotesAdapter(context, options)
118+
else
119+
CodeWithNotesAdapter(context, options))
111120
}
112121

122+
/**
123+
* Code adapter accessor.
124+
*/
125+
fun getAdapter() = vCodeList.adapter as? AbstractCodeAdapter<*>
126+
113127
/**
114128
* Initialize with adapter.
115129
*
116130
* @param adapter Adapter
117131
*/
118-
fun setAdapter(adapter: AbstractCodeAdapter<*>) {
132+
fun setAdapter(adapter: AbstractCodeAdapter<*>) = setAdapter(adapter, false)
133+
134+
/**
135+
* Set adapter & initialize if needed.
136+
*
137+
* @param adapter Adapter
138+
* @param isSaveOptions Save options?
139+
*/
140+
fun setAdapter(adapter: AbstractCodeAdapter<*>, isSaveOptions: Boolean = true) {
141+
if (isSaveOptions)
142+
adapter.options = getOptions() ?: Options(context)
143+
119144
vCodeList.adapter = adapter
120-
setupShadows(adapter.opts.shadows)
145+
setupShadows(adapter.options.shadows)
121146
highlight()
122147
}
123148

124149
/**
125150
* Set code content.
126-
* At this point view should be prepared, otherwise it
127-
* will be configured automatically with default params.
151+
*
152+
* There are two ways before code will be highlighted:
153+
* 1) view is not initialized (adapter or options are not set),
154+
* prepare with default params & try to classify language
155+
* 2) view initialized with some params, language:
156+
* a) is set: used defined programming language
157+
* b) not set: try to classify
128158
*
129159
* @param code Code content
130160
*/
131161
fun setCode(code: String) {
132162
getAdapter() ?: prepare()
133-
getAdapter()?.updateCode(code)
163+
getAdapter()!!.updateCode(code)
134164
}
135165

136166
/**
137167
* Set code content.
138168
*
169+
* There are two ways before code will be highlighted:
170+
* 1) view is not initialized, prepare with default params
171+
* 2) view initialized with some params, set new language
172+
*
139173
* @param code Code content
140174
* @param language Programming language
141175
*/
142176
fun setCode(code: String, language: String) {
143-
getAdapter() ?: setOptions(Options(context, language = language))
144-
getAdapter()?.updateCode(code)
177+
val options = if (getAdapter() == null)
178+
Options(context)
179+
else
180+
getAdapter()!!.options
181+
182+
setOptions(options.withLanguage(language))
183+
getAdapter()!!.updateCode(code)
145184
}
146185
}
147186

0 commit comments

Comments
 (0)