Soundfonts can be very useful in some cases but seem to have some limitations. Surprisingly there seems to be no method exposed in AudioKit that allows to just change the preset of an already loaded soundfont which would of course be the most common scenario. In my case there is only one soundfont ever used, but instead of being able to switch presets on it, which is the main reason to have a soundfont in the first place, I have to use one of the 3 load soundfont methods like loadMelodicSoundFont and each time reload the entire file. That seems inefficient as these files tend to be rather large. Is there another way to change the preset?
Currently changing soundfont presets like this:
do {
try loadMelodicSoundFont(soundFontTitle, preset: gmPreset)
} catch { print("load soundfont failed with error: \(error)") }
Related
I have a design system project where multiple Apps import the design system. This presents a few problems:
For SwiftUI previewing, it makes the most sense to provide a default set of colors. This keeps things light weight and flexible.
Optionally, each app importing the Design System should be able to override one of these colors. The goal is to provide a turn key starter kit for tons of apps and prototypes and only override things when needed.
I'd like the design system to be as automated as possible, were the Swift Package does all the work to set up the rules and definitions. The presence of a named color in the Assets file of the App target creates an override. Why? Using Swift Package plugins to generate code from the assets folder is ideal.
I've had mixed results trying to localize assets. I've also tried to inspect the Bundle names but this felt fragile and less elegant. I came up with the solution below where if a color is present in the main bundle (the App) then use it otherwise fall back to the Swift Package .module. This uses UIKit to check if the image is present but that feels like a hack.
Sample code below shows the logic for checking if an image is present in the main bundle.
import SwiftUI
import UIKit
extension Color {
// Auto generate this in the future using a SPM plugin.
public enum Palette: String, CaseIterable {
case brand, error, warning, success
}
public static func designSystem(color: Palette) -> Color {
let hasOverride = UIColor(named: color.rawValue) != nil
let bundle: Bundle = hasOverride ? .main : .module
return self.init(color.rawValue, bundle: bundle)
}
}
The screenshot shows this behavior in action.
Is there is a better or "more official" way to accomplish this kind if override across a hierarchy of App targets and Swift Package resources? It seems heavy handed to import all of UIKit to do this, but it works. I have been unable to get localized colors to accomplish the same thing.
If I am in the main activity, if I want to open a file that there is in the assets, I use this code:
using (Stream miFicheroOrigen = Assets.Open("configuration.xml"))
{}
I can see that the namespace is Android.Context.Res.AssetManager.
However, I would like to have a class with the methods to manage the configuration, so I have create this class:
internal class ConfigurationManager
{
}
But it this case I have to use Android.Content.Resoruces.System.Assets, i can't use the Assets in the same namespace than in the main activity.
It works, but I would like to know if really it is the same Assets or different.
Perhaps it is because the first one is an Activity and in the second one it is a normal class, but I don't understand well why in the second case I can't access to the same namespace.
Thanks.
I can see that the namespace is Android.Context.Res.AssetManager.
But it this case I have to use Android.Content.Resoruces.System.Assets
It may seem that you have mistyped the wrong letter in your question. Maybe you want to know the difference between them as bellow:
Android.Content.Res.AssetManager Class VS Android.Content.Res.Resources Class
Different:
Android.Content.Res.AssetManager : Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.
Android.Content.Res.Resources : Class for accessing an application's resources. This sits on top of the asset manager of the application (accessible through Resources.Assets) and provides a high-level API for getting typed data from the assets.Using application resources makes it easy to update various characteristics of your application without modifying code, and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as for different languages and screen sizes). This is an important aspect of developing Android applications that are compatible on different types of devices.
Android.Content.Res.Resources.System Property : Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).
Same:
They all can get the Android.Content.Res.AssetManager.
Android.Content.Res.AssetManager assetManager = Android.Content.Res.Resources.System.Assets;
Result :So you can see Android.Content.Res.Resources.System is a global shared resources object,so you can get from a normal class.However , it does not mean that Android.Content.Res.AssetManager can not be getted from normal class.According to the documentation, they are only the high and low points of the api, and the document recommends using such methods(Android.Content.Res.Resources.System) to obtain resources.
My code looks like the following:
let myAPI = Service(baseURL: "...")
myAPI.resource("/...").addObserver(owner: self) { resource, event in
}
if the URL always returns 200 (NOT 301), is there a way to check resource or event if the data itself, the json-content, changed or if it's the same?
I would like to achieve a behavior where the app does the loading in the background and only triggers a UI-action if the JSON itself changed.
Siesta currently does not support this. It can’t automatically do so in the general case, since there is no universally valid definition of “changed” for every type of resource content. (Even if the resource content is Equatable, and that happens to match the definition of “changed” you want, should some headers should be included in the notion of equality? Which ones?)
The 304 is a nice bright line: the server, arbiter of truth for the resource, declares it semantically unchanged — and all the API-specific rules about what “unchanged” means live on the server.
This does, however, seem like a nice thing to be able to customize. I recommend filing an issue if you’d like to see it implemented.
In the meantime, you can do it by hand: make your resource payload conform to Equatable and check it in your resource observers. If you have performance-sensitive updates, an approach with this shape sometimes makes more sense anyway, since you can check for partial changes (e.g. only some elems in a collection changed).
I'd like to add and remove items on top of each on the press of a button to avoid load times for network images.
My idea is to add ~5 images to a Stack on top of each other and be able to remove the top one and add another on to the back so that it can pre-load.
I did not find any documentation or answers if this is possible with the Stack class
I also had the idea to use a Dismissible, but there seems to be no way to change child and background on it either.
You shouldn't have to create a stack to preload images; what you're looking for is the NetworkImage class. You could maintain a set of NetworkImage classes that you've manually loaded and pass one of those into the Image constructor, but I'm fairly sure that flutter actually caches the last few images you've loaded in memory automatically so that isn't even really needed (I think I heard the last 100 images or so but I could be wrong).
Unfortunately, the API isn't all that simple for it (there's a bug open about this in the flutter repo), but it's not all that difficult either.
All you need is a method somewhere like this (or just do it in a build function):
static void preload(BuildContext, String path) {
final configuration = createLocalImageConfiguration(context);
NetworkImage(path).resolve(configuration);
}
Call that from anywhere you have a valid build context. And TBH, you could construct the ImageConfiguration yourself and pass that in instead (just look at the source for createLocalImageConfiguration to get an idea about that).
If you want to get a bit fancier about it, instead of just making an NetworkImage and resolving it, you could have a class that maintains a map of preloaded NetworkImages and returns them when you want.
Another thing you can do which might be enough to resolve your problem without doing anything else is setting gaplessPlayback = true where you construct your Image.network - that will make sure that the image doesn't switch until the next image is fully loaded.
Hope that helps =). If it doesn't actually work let me know though and I'll dig a bit deeper - I haven't done it in code myself yet.
I'm trying to find an interface which allows me to create a stream which allows seeking (just a Reader is fine, too) from either a file or []byte, but can't seem to find anything in the godoc. Some of the types in the bufio package would work quite well, but they don't appear to support seeking.
Is there something I overlooked which would fit what I'm looking for?
Both *os.File (for files) and *bytes.Reader (for having an io.Reader from a []byte) implement the io.Seeker interface and thus have a Seek method.
io.Seeker is implemented by...
*bytes.Reader
*io.SectionReader
io.ReadSeeker
io.WriteSeeker
io.ReadWriteSeeker
mime/multipart.File
net/http.File
*os.File
*strings.Reader
So if you're working with a file, thus very likely *os.File, you don't need to do anything additional to be able to seek it. Just make sure that if you're using interfaces instead of concrete types that you do not want an io.Reader but an io.ReadSeeker.