Skip to content

Commit f9f8756

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

2 files changed

Lines changed: 56 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
return JsonParser.`object`().from(savedJson).getArray("instances")
31+
.filterIsInstance<JsonObject>()
32+
.map { PeertubeInstance(it.getString("url"), it.getString("name")) }
33+
} catch (_: JsonParserException) {
34+
return listOf(currentInstance)
35+
}
36+
}
37+
38+
@JvmStatic
39+
fun selectInstance(instance: PeertubeInstance, context: Context): PeertubeInstance {
40+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
41+
val selectedInstanceKey = context.getString(R.string.peertube_selected_instance_key)
42+
43+
val jsonWriter = JsonWriter.string().`object`()
44+
jsonWriter.value("name", instance.name)
45+
jsonWriter.value("url", instance.url)
46+
val jsonToSave = jsonWriter.end().done()
47+
48+
sharedPreferences.edit { putString(selectedInstanceKey, jsonToSave) }
49+
ServiceList.PeerTube.instance = instance
50+
return instance
51+
}
52+
53+
@JvmStatic
54+
val currentInstance: PeertubeInstance
55+
get() = ServiceList.PeerTube.instance
56+
}

0 commit comments

Comments
 (0)