Categories
discuss

When to use an AppCompatView vs a normal Android view

What is the difference between using them, and when should they be used?

An example of the documentation for an AppCompatView is:

A tint aware EditText. This will automatically be used when you use
EditText in your layouts. You should only need to manually use this
class when writing custom views

Why should the AppCompatView only be used for custom views?

There is a similar question, but I am looking for a good explanation for why the AppCompatView should only be used for custom views.

Answer

Some material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. However, you can design your apps to make use of these features when running on devices that support material design and still be compatible with devices running previous releases of Android.

Que-> Why the AppCompatView should only be used for custom views.

Answer -> In simple terms AppCompatView is used for maintaining compatibility. If your app uses the Material theme as with Theme.Material but does not provide an alternative theme, your app will not run on versions of Android earlier than 5.0.

If the layouts that you design according to the material design guidelines do not use any of the new XML attributes introduced in Android 5.0 (API level 21), they will work on previous versions of Android. Otherwise, you can provide alternative layouts. You can also provide alternative layouts to customize how your app looks on earlier versions of Android.

Making backwards compatible material design Android applications is much easier with AppCompat, especially when you understand how its styles and themes are working together to dynamically tint the user interface.

With AppCompat, you should spend less time fiddling with assets and backwards compatibility, and more time focusing on actually building your application.

Currently, new projects created through Android Studio incorporate this library by default.

Note: This library depends on the v4 Support Library.

Below are few links for references

  1. Android Material Themes Made Easy With AppCompat
  2. Migrating to the AppCompat Library
  3. Getting Material Design for Pre-Lollipop Devices with AppCompat v21
Categories
discuss

Maven Plugin Mojo Configure Default Parameter Values

I have a maven plugin and a simple Mojo that looks somewhat close to

public abstract class AbstractSetupMojo extends AbstractMojo {
    @Parameter(property="targetHost", defaultValue="localhost") 
    private String targetHost;
    @Parameter(property="targetPort", defaultValue="27017")
    private Integer targetPort;
    @Parameter(property="targetDbName", required=true) 
    private String targetDbName;
    @Parameter(property="sourceHost", defaultValue="${mojo.configuration.targetHost}") 
    private String sourceHost;
    @Parameter(property="sourcePort", defaultValue="${mojo.configuration.targetPort}")
    private Integer sourcePort;
    @Parameter (property="sourceDbName", defaultValue="${mojo.configuration.targetDbName}")
    private String sourceDbName;
    @Parameter(property="scriptsPath")
    private File scriptsPath;
}     

Other Mojos are extending this one. So, the idea is to set the source* parameters to the values of the corresponding target* parameters. Also some default values are set.

In the test I have something like

public class GenerateMojoTest extends AbstractMojoTestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testConfiguration() throws Exception {
        File pom = getTestFile("src/test/resources/test-project-1/pom.xml");
        assertNotNull(pom);
        assertTrue(pom.exists());
        GenerateMojo generateMojo = (GenerateMojo)lookupMojo("generate", pom);
        assertThat(generateMojo.getSourceDbName()).isEqualTo(generateMojo.getTargetDbName());
        assertThat(generateMojo.getSourcePort()).isEqualTo(generateMojo.getTargetPort()).isEqualTo(27017);
        assertThat(generateMojo.getSourceHost()).isEqualTo(generateMojo.getTargetHost()).isEqualTo("localhost");
    }

}

The part of interest in the POM file in the test looks like

        <plugin>
            <groupId>com.ffy</groupId>
            <artifactId>setup-maven-plugin</artifactId>
    <!--                
            <configuration>
                <scriptsPath>src/test/resources/temp</scriptsPath>
            </configuration>
    -->                
            <executions>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The test fails because all the Mojo parameters are null if I keep <configuration> commented out, if I uncomment it then only scriptsPath is set. Other parameters are null.

Do I have to do something else in my tests in order to have a fully configured Mojo ?

I tried longer approach with

protected GenerateMojo setupMojo(final File pom) throws ComponentConfigurationException, Exception {
    final MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    final ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class);
    final MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
    final MavenSession session = newMavenSession(project);
    final MojoExecution execution = newMojoExecution("generate");
    final GenerateMojo mojo = (GenerateMojo) this.lookupConfiguredMojo(session, execution);
    return mojo;
}

instead of lookupMojo but that didn’t change a bit.

Answer

You will need to have the <configuration/> part and define the values you’re interested in. You would expect it to be a little smarter, but in fact what the testing harness does is, it reads the values from the <configuration/> and ignores the ones from your annotations. The testing harness leaves a lot to be desired, it doesn’t actually load the values for you like a proper Maven execution/interpolation… Hence, I would advise using the maven-invoker-plugin, if it better suits your needs.

Categories
discuss

npm – EPERM: operation not permitted on Windows

I ran

npm config set prefix /usr/local

After running that command, When trying to run any npm commands on Windows OS I keep getting the below.

Error: EPERM: operation not permitted, mkdir 'C:Program Files (x86)Gitlocal'
at Error (native)

Have deleted all files from

C:Users<your username>.configconfigstore

It did not work.

Any suggestion ?

Answer

Running this command was my mistake.

npm config set prefix /usr/local

Path /usr/local is not for windows. This command changed the prefix variable at 'C:Program Files (x86)Gitlocal'

To access and make a change to this directory I need to run my cmd as administrator.

So I did:

  1. Run cmd as administrator
  2. Run npm config edit (You will get notepad editor)
  3. Change prefix variable to C:Users<User Name>AppDataRoamingnpm

Then npm start works in a normal console.

Categories
discuss

Pattern for shareReplay(1) in RxJS5

I’ve started playing with RxJS5, and now see that there is no longer a shareReplay method.

It’s quite possible that I often misused shareReplay in RxJS4, but now I’m struggling to get the behaviour that I want, i.e:

  • Create an observable
  • Subscribe to the observable, and the observable produces a value
  • Subscribe to the observable a second time, and I get the same first value
  • Observable produces a second value, and both subscriptions get the second value

How do I implement this with RxJS5?

In general I think I understand the RxJS operators quite well, but the whole cold, hot, publish, connect is rather unclear to me. Is there a good reference that shows how to find what kind of observable I have, so that I can find out in a logical manner why a subscribe is not getting values, or why an observable is being executed multiples times?

EDIT

Happy news, shareReplay() is back in RxJS 5.4.0:

Changelog: https://github.com/ReactiveX/rxjs/blob/892700dd4f5d5e5f9ae9276ede32208f4390c5e9/CHANGELOG.md#540-2017-05-09

Barebones documentation: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-shareReplay

Answer

That question is best answered by members who participate in Rxjs5, but here is my take:

  • shareReplay is the multicast operator with a ReplaySubject, followed by a refCount. So I would bet that publishReplay(x).refCount() should be quite close to the shareReplay behaviour. In any case, publishReplay already gives you all the points you mentioned. The refCount adds the unsubscription when there is no more observers (refCount decreased to 0).
  • you can have a look at the specs here http://reactivex.io/rxjs/test-file/spec-js/operators/publishReplay-spec.js.html. See line 127 onwards var replayed = source.publishReplay(1).refCount();, that should be equivalent to your shareReplay(1).

About the rest of your question:

  • I think we all want that good reference that shows how to find what kind of observable I have.... There are many places, including Rxjs4 documentation where you find explanations about hot and cold observables.
  • Here, and here are some examples of resources.

Follows my own present understanding of the matter:

  • subjects are hot (mostly anyways, as you could argue that a replay subject has a behaviour closer to than of a cold observable)
  • all observables are cold, unless made explicitly otherwise.
  • among the explicit ways to make a cold observable hot, you have the multicast operator and its derivatives share, publish, shareReplay etc. Those operators internally all involve subjects.
  • Note that it does not have to be visible to you that those operators were used. But in that case, the API or documentation should explicitly tell you. For instance, Rx.Observable.fromEvent('input','click') is hot. You can see in its implementation that there is a share somewhere.
  • to the hot/cold dichotomy you have to add the connectable kind which till it is connected, is neither hot nor cold.
  • defer always give rise to a cold observable.
  • lastly some operators do not change the nature of the observable, but do create hot observables internally and pass them on in their stream. That is the case for instance of groupBy. op1.op2.groupBy is cold, but it will emit hot observables as values in the resulting stream. In those cases, only the documentation (if any) can help you find out. Otherwise, the source code, and the test specs. Or asking on SO.
Categories
discuss

javascript sort list of lists by sublist second entry

How can I sort a list of lists by order of the last element?

Here is my best attempt so far:

var mylist = [ [1, 'c'], [3, 'a'],  [5, 'b']];
mylist.sort(function(a,b){return a[1]-b[1];});

My call to sort has not effect, and mylist remains in the same order.

Answer

var mylist = [ [1, 'c'], [3, 'a'],  [5, 'b']];
mylist.sort(function(a,b){return a[1].localeCompare(b[1]);});
console.log(mylist)

Use a[1].localeCompare(b[1]) to sort the list in the ascending order or b[1].localeCompare(a[1]) for other way round. This way you will be comparing two strings. You were trying to subtract one string from the other which return NaN.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..