Skip to content

Commit 4846766

Browse files
author
Yevhen Babiichuk (DustDFG)
committed
Convert newpipe/util/PeertubeHelper to kotlin
1 parent 780e6a4 commit 4846766

2 files changed

Lines changed: 53 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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.JsonWriter
14+
import org.schabi.newpipe.R
15+
import org.schabi.newpipe.extractor.ServiceList
16+
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance
17+
18+
object PeertubeHelper {
19+
@JvmStatic
20+
fun getInstanceList(context: Context): List<PeertubeInstance> {
21+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
22+
val savedInstanceListKey = context.getString(R.string.peertube_instance_list_key)
23+
val savedJson = sharedPreferences.getString(savedInstanceListKey, null)
24+
if (savedJson == null) {
25+
return listOf(currentInstance)
26+
}
27+
28+
return runCatching {
29+
JsonParser.`object`().from(savedJson).getArray("instances")
30+
.filterIsInstance<JsonObject>()
31+
.map { PeertubeInstance(it.getString("url"), it.getString("name")) }
32+
}.getOrDefault(listOf(currentInstance))
33+
}
34+
35+
@JvmStatic
36+
fun selectInstance(instance: PeertubeInstance, context: Context): PeertubeInstance {
37+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
38+
val selectedInstanceKey = context.getString(R.string.peertube_selected_instance_key)
39+
40+
val jsonWriter = JsonWriter.string().`object`()
41+
jsonWriter.value("name", instance.name)
42+
jsonWriter.value("url", instance.url)
43+
val jsonToSave = jsonWriter.end().done()
44+
45+
sharedPreferences.edit { putString(selectedInstanceKey, jsonToSave) }
46+
ServiceList.PeerTube.instance = instance
47+
return instance
48+
}
49+
50+
@JvmStatic
51+
val currentInstance: PeertubeInstance
52+
get() = ServiceList.PeerTube.instance
53+
}

0 commit comments

Comments
 (0)