Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget import export #352

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import android.content.Context
import android.net.Uri
import com.github.premnirmal.ticker.components.Injector
import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.widget.WidgetData
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.annotations.SerializedName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
Expand Down Expand Up @@ -84,4 +88,71 @@ internal object PortfolioExporter {
}
return@withContext uri.path
}
}
internal object WidgetExporter {
private val gson = Injector.appComponent().gson()
suspend fun exportWidget(file: File, vararg widgetData: WidgetData): String? =
withContext(Dispatchers.IO){
try {
val jsonString = getJSONFrom(widgetData[0])
val fileOutputStream = FileOutputStream(file)
fileOutputStream.use { it.write(jsonString.toByteArray()) }
} catch (e: IOException){
Timber.e(e)
return@withContext null
}
return@withContext file.path
}
suspend fun exportWidget(context: Context, uri: Uri, vararg widgetData: WidgetData): String? =
withContext(Dispatchers.IO) {
try {
val jsonString = getJSONFrom(widgetData[0])
val contentResolver = context.applicationContext.contentResolver

contentResolver.openFileDescriptor(uri, "rwt")
?.use {
FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
fileOutputStream.write((jsonString.toByteArray()))
}
}
} catch (e: IOException) {
Timber.e(e)
return@withContext null
}
return@withContext uri.path
}

private fun getJSONFrom(widgetData: WidgetData): String {
val json = gson.newBuilder().serializeNulls().setPrettyPrinting()
//json.setExclusionStrategies(FieldExcluder)
val exportWidget = ExportWidget(widgetData)
val mygson :String = json.create().toJson(exportWidget)
return mygson
}

object FieldExcluder : ExclusionStrategy {
override fun shouldSkipField(f: FieldAttributes?): Boolean {
val name = f?.getAnnotation(SerializedName::class.java)?.value
if (name ==null) return true
return false
}

override fun shouldSkipClass(clazz: Class<*>?): Boolean {
val qualifier = clazz?.name
when (qualifier) {
"com.github.premnirmal.ticker.widget.WidgetData" -> return false
"com.github.premnirmal.ticker.AppPreferences" -> return false
"java.lang.String" -> return false
"java.util.List" -> return false
"android.content.SharedPreferences" -> return false
"int" -> return false
"java.lang.Integer" -> return false
"android.app.SharedPreferencesImpl" -> return false

else
-> return true
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.premnirmal.ticker.settings

import com.github.premnirmal.ticker.AppPreferences.Companion.toCommaSeparatedString
import com.github.premnirmal.ticker.widget.WidgetData

class ExportWidget() {
lateinit var name: String
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the indentation is off in some of these files. Can you makes sure you follow the style guide listed in the contribution page https://github.com/premnirmal/StockTicker/blob/master/CONTRIBUTING.md#contributing-to-stockticker?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry I was just after some early input

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unable to use that project, but that is unrelated to this issue, I will start another.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using the latest version of Android Studio is that going to be an issue?
There seems to be no corelation between the project you linked to and code styles in android studio

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instructions for the codestyle are in https://github.com/square/java-code-styles

lateinit var tickerList: String // csv list of tickers

lateinit var autoSort: String
lateinit var hideHeader: String
lateinit var showCurrency: String
lateinit var showBold: String

lateinit var widgetSize: String //
lateinit var layout: String //
lateinit var textColor: String
lateinit var background: String

constructor(widgetData: WidgetData) : this() {
name = widgetData.widgetName()
tickerList = widgetData.getTickers().toCommaSeparatedString()
autoSort = widgetData.autoSortEnabled.value.toString()
showCurrency = widgetData.isCurrencyEnabled().toString()
showBold = widgetData.isBoldEnabled().toString()
hideHeader = widgetData.hideHeader().toString()
widgetSize = widgetData.widgetSizePref().toString() // either 0 (default) or 1 (1 ticker per row)
layout = widgetData.layoutPref().toString() // 0 Anim,1 Tabs,2 Fixed,3(?)Myportfolio
textColor = widgetData.textColorPref().toString() // 0 Sys, 1 Light,2 Dark
background = widgetData.bgPref().toString() // 0 Sys, 1, Transparent, 2 Translucent
}
}
Loading