Skip to content

Commit e8d032e

Browse files
author
Yevhen Babiichuk (DustDFG)
committed
Convert newpipe/util/PeertubeHelper to kotlin
1 parent 174f33c commit e8d032e

2 files changed

Lines changed: 61 additions & 69 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/PeertubeHelper.java

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2019-2026 NewPipe contributors <https://newpipe.net>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package org.schabi.newpipe.util
7+
8+
import android.content.Context
9+
import androidx.core.content.edit
10+
import androidx.preference.PreferenceManager
11+
import com.grack.nanojson.JsonObject
12+
import com.grack.nanojson.JsonParser
13+
import com.grack.nanojson.JsonParserException
14+
import com.grack.nanojson.JsonWriter
15+
import org.schabi.newpipe.R
16+
import org.schabi.newpipe.extractor.ServiceList
17+
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance
18+
19+
object PeertubeHelper {
20+
@JvmStatic
21+
fun getInstanceList(context: Context): List<PeertubeInstance> {
22+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
23+
val savedInstanceListKey = context.getString(R.string.peertube_instance_list_key)
24+
val savedJson = sharedPreferences.getString(savedInstanceListKey, null)
25+
if (savedJson == null) {
26+
return listOf(currentInstance)
27+
}
28+
29+
try {
30+
val array = JsonParser.`object`().from(savedJson).getArray("instances")
31+
val result: MutableList<PeertubeInstance> = ArrayList()
32+
for (instance in array) {
33+
if (instance is JsonObject) {
34+
val name = instance.getString("name")
35+
val url = instance.getString("url")
36+
result.add(PeertubeInstance(url, name))
37+
}
38+
}
39+
return result
40+
} catch (e: JsonParserException) {
41+
return listOf(currentInstance)
42+
}
43+
}
44+
45+
@JvmStatic
46+
fun selectInstance(instance: PeertubeInstance, context: Context): PeertubeInstance {
47+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
48+
val selectedInstanceKey = context.getString(R.string.peertube_selected_instance_key)
49+
val jsonWriter = JsonWriter.string().`object`()
50+
jsonWriter.value("name", instance.name)
51+
jsonWriter.value("url", instance.url)
52+
val jsonToSave = jsonWriter.end().done()
53+
sharedPreferences.edit { putString(selectedInstanceKey, jsonToSave) }
54+
ServiceList.PeerTube.instance = instance
55+
return instance
56+
}
57+
58+
@JvmStatic
59+
val currentInstance: PeertubeInstance
60+
get() = ServiceList.PeerTube.instance
61+
}

0 commit comments

Comments
 (0)