Search in sources :

Example 1 with Indexes

use of org.moire.ultrasonic.domain.Indexes in project ultrasonic by ultrasonic.

the class OfflineMusicService method getIndexes.

@Override
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
    List<Artist> artists = new ArrayList<Artist>();
    File root = FileUtil.getMusicDirectory(context);
    for (File file : FileUtil.listFiles(root)) {
        if (file.isDirectory()) {
            Artist artist = new Artist();
            artist.setId(file.getPath());
            artist.setIndex(file.getName().substring(0, 1));
            artist.setName(file.getName());
            artists.add(artist);
        }
    }
    String ignoredArticlesString = "The El La Los Las Le Les";
    final String[] ignoredArticles = COMPILE.split(ignoredArticlesString);
    Collections.sort(artists, new Comparator<Artist>() {

        @Override
        public int compare(Artist lhsArtist, Artist rhsArtist) {
            String lhs = lhsArtist.getName().toLowerCase();
            String rhs = rhsArtist.getName().toLowerCase();
            char lhs1 = lhs.charAt(0);
            char rhs1 = rhs.charAt(0);
            if (Character.isDigit(lhs1) && !Character.isDigit(rhs1)) {
                return 1;
            }
            if (Character.isDigit(rhs1) && !Character.isDigit(lhs1)) {
                return -1;
            }
            for (String article : ignoredArticles) {
                int index = lhs.indexOf(String.format("%s ", article.toLowerCase()));
                if (index == 0) {
                    lhs = lhs.substring(article.length() + 1);
                }
                index = rhs.indexOf(String.format("%s ", article.toLowerCase()));
                if (index == 0) {
                    rhs = rhs.substring(article.length() + 1);
                }
            }
            return lhs.compareTo(rhs);
        }
    });
    return new Indexes(0L, ignoredArticlesString, Collections.<Artist>emptyList(), artists);
}
Also used : Artist(org.moire.ultrasonic.domain.Artist) ArrayList(java.util.ArrayList) Indexes(org.moire.ultrasonic.domain.Indexes) File(java.io.File)

Example 2 with Indexes

use of org.moire.ultrasonic.domain.Indexes in project ultrasonic by ultrasonic.

the class RESTMusicService method getArtists.

@Override
public Indexes getArtists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
    Indexes cachedArtists = fileStorage.load(ARTISTS_STORAGE_NAME, DomainSerializers.getIndexesSerializer());
    if (cachedArtists != null && !refresh) {
        return cachedArtists;
    }
    updateProgressListener(progressListener, R.string.parser_reading);
    Response<GetArtistsResponse> response = subsonicAPIClient.getApi().getArtists(null).execute();
    checkResponseSuccessful(response);
    Indexes indexes = APIIndexesConverter.toDomainEntity(response.body().getIndexes());
    fileStorage.store(ARTISTS_STORAGE_NAME, indexes, DomainSerializers.getIndexesSerializer());
    return indexes;
}
Also used : Indexes(org.moire.ultrasonic.domain.Indexes) GetArtistsResponse(org.moire.ultrasonic.api.subsonic.response.GetArtistsResponse)

Example 3 with Indexes

use of org.moire.ultrasonic.domain.Indexes in project ultrasonic by ultrasonic.

the class RESTMusicService method getIndexes.

@Override
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
    Indexes cachedIndexes = fileStorage.load(INDEXES_STORAGE_NAME, DomainSerializers.getIndexesSerializer());
    if (cachedIndexes != null && !refresh) {
        return cachedIndexes;
    }
    updateProgressListener(progressListener, R.string.parser_reading);
    Response<GetIndexesResponse> response = subsonicAPIClient.getApi().getIndexes(musicFolderId, null).execute();
    checkResponseSuccessful(response);
    Indexes indexes = APIIndexesConverter.toDomainEntity(response.body().getIndexes());
    fileStorage.store(INDEXES_STORAGE_NAME, indexes, DomainSerializers.getIndexesSerializer());
    return indexes;
}
Also used : GetIndexesResponse(org.moire.ultrasonic.api.subsonic.response.GetIndexesResponse) Indexes(org.moire.ultrasonic.domain.Indexes)

Example 4 with Indexes

use of org.moire.ultrasonic.domain.Indexes in project ultrasonic by ultrasonic.

the class SelectArtistActivity method load.

private void load() {
    BackgroundTask<Indexes> task = new TabActivityBackgroundTask<Indexes>(this, true) {

        @Override
        protected Indexes doInBackground() throws Throwable {
            boolean refresh = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_REFRESH, false);
            MusicService musicService = MusicServiceFactory.getMusicService(SelectArtistActivity.this);
            boolean isOffline = Util.isOffline(SelectArtistActivity.this);
            boolean useId3Tags = Util.getShouldUseId3Tags(SelectArtistActivity.this);
            if (!isOffline && !useId3Tags) {
                musicFolders = musicService.getMusicFolders(refresh, SelectArtistActivity.this, this);
            }
            String musicFolderId = Util.getSelectedMusicFolderId(SelectArtistActivity.this);
            return !isOffline && useId3Tags ? musicService.getArtists(refresh, SelectArtistActivity.this, this) : musicService.getIndexes(musicFolderId, refresh, SelectArtistActivity.this, this);
        }

        @Override
        protected void done(Indexes result) {
            if (result != null) {
                List<Artist> artists = new ArrayList<Artist>(result.getShortcuts().size() + result.getArtists().size());
                artists.addAll(result.getShortcuts());
                artists.addAll(result.getArtists());
                artistListView.setAdapter(new ArtistAdapter(SelectArtistActivity.this, artists));
            }
            // Display selected music folder
            if (musicFolders != null) {
                String musicFolderId = Util.getSelectedMusicFolderId(SelectArtistActivity.this);
                if (musicFolderId == null) {
                    if (folderName != null) {
                        folderName.setText(R.string.select_artist_all_folders);
                    }
                } else {
                    for (MusicFolder musicFolder : musicFolders) {
                        if (musicFolder.getId().equals(musicFolderId)) {
                            if (folderName != null) {
                                folderName.setText(musicFolder.getName());
                            }
                            break;
                        }
                    }
                }
            }
        }
    };
    task.execute();
}
Also used : Artist(org.moire.ultrasonic.domain.Artist) TabActivityBackgroundTask(org.moire.ultrasonic.util.TabActivityBackgroundTask) MusicService(org.moire.ultrasonic.service.MusicService) ArtistAdapter(org.moire.ultrasonic.view.ArtistAdapter) ArrayList(java.util.ArrayList) Indexes(org.moire.ultrasonic.domain.Indexes) MusicFolder(org.moire.ultrasonic.domain.MusicFolder)

Example 5 with Indexes

use of org.moire.ultrasonic.domain.Indexes in project ultrasonic by ultrasonic.

the class CachedMusicService method getArtists.

@Override
public Indexes getArtists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
    checkSettingsChanged(context);
    if (refresh) {
        cachedArtists.clear();
    }
    Indexes result = cachedArtists.get();
    if (result == null) {
        result = musicService.getArtists(refresh, context, progressListener);
        cachedArtists.set(result);
    }
    return result;
}
Also used : Indexes(org.moire.ultrasonic.domain.Indexes)

Aggregations

Indexes (org.moire.ultrasonic.domain.Indexes)6 ArrayList (java.util.ArrayList)2 Artist (org.moire.ultrasonic.domain.Artist)2 File (java.io.File)1 GetArtistsResponse (org.moire.ultrasonic.api.subsonic.response.GetArtistsResponse)1 GetIndexesResponse (org.moire.ultrasonic.api.subsonic.response.GetIndexesResponse)1 MusicFolder (org.moire.ultrasonic.domain.MusicFolder)1 MusicService (org.moire.ultrasonic.service.MusicService)1 TabActivityBackgroundTask (org.moire.ultrasonic.util.TabActivityBackgroundTask)1 ArtistAdapter (org.moire.ultrasonic.view.ArtistAdapter)1