-
Notifications
You must be signed in to change notification settings - Fork 17
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
@guliash & Artem Gilmudinov #5
Open
guliash
wants to merge
12
commits into
yamblz-native:master
Choose a base branch
from
guliash:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8a64bd
wip
guliash 1f91cfb
fix paddings; remove logs;
guliash 4ff41b8
last fixes - dps; comments;
guliash 18c93f5
forgot cancelling queries;
guliash bd992fa
make singers view instead of select from select;
guliash d8adf95
butterknife unbind;
guliash cc0471f
exception message classname fix;
guliash 8167a94
renamed portrait to phone;
guliash 728d3ea
fix lag when going back to tabs from desc;
guliash 0ae9c6d
enable wal;
guliash 094f099
singers_genres singer_id index;
guliash 03dfb52
end transaction at finally block;
guliash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/src/main/java/ru/yandex/yamblz/provider/CPDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ru.yandex.yamblz.provider; | ||
|
||
import android.content.ContentUris; | ||
import android.content.Context; | ||
import android.support.annotation.MainThread; | ||
import android.support.annotation.NonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.Executor; | ||
|
||
import ru.yandex.yamblz.performance.AnyThread; | ||
import ru.yandex.yamblz.singerscontracts.Singer; | ||
|
||
import static ru.yandex.yamblz.singerscontracts.SingersContract.Singers; | ||
|
||
/** | ||
* Content provider based {@link DataProvider}. | ||
* Executes long methods on a background thread, posts result back to the main thread | ||
*/ | ||
public class CPDataProvider implements DataProvider { | ||
|
||
private Context mContext; | ||
private DataTransformer mDataTransformer; | ||
private Executor mWorker, mPoster; | ||
private Set<Callback> mCallbacks = new HashSet<>(0); | ||
|
||
@MainThread | ||
public CPDataProvider(Context context, DataTransformer dataTransformer, Executor worker, | ||
Executor poster) { | ||
mContext = context; | ||
mDataTransformer = dataTransformer; | ||
mWorker = worker; | ||
mPoster = poster; | ||
} | ||
|
||
@MainThread | ||
private void persistCallback(Callback callback) { | ||
mCallbacks.add(callback); | ||
} | ||
|
||
@MainThread | ||
private boolean checkIfSubscribed(Callback callback) { | ||
return mCallbacks.contains(callback); | ||
} | ||
|
||
@MainThread | ||
@Override | ||
public void getSingers(final Callback<List<Singer>> callback) { | ||
persistCallback(callback); | ||
mWorker.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
List<Singer> singers = mDataTransformer.toSingers(mContext.getContentResolver().query( | ||
Singers.CONTENT_URI, null, null, null, null)); | ||
postResult(callback, singers); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void getSinger(final int singerId, @NonNull final Callback<Singer> callback) { | ||
persistCallback(callback); | ||
mWorker.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
Singer singer = mDataTransformer.toSinger(mContext.getContentResolver().query( | ||
ContentUris.withAppendedId(Singers.CONTENT_URI, singerId), | ||
null, null, null, null)); | ||
postResult(callback, singer); | ||
} | ||
}); | ||
} | ||
|
||
@MainThread | ||
@Override | ||
public void cancel(Callback callback) { | ||
mCallbacks.remove(callback); | ||
} | ||
|
||
@AnyThread | ||
private <T> void postResult(final Callback<T> callback, final T result) { | ||
mPoster.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
if(checkIfSubscribed(callback)) { | ||
callback.postResult(result); | ||
cancel(callback); | ||
} | ||
} | ||
}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/ru/yandex/yamblz/provider/CPDataTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ru.yandex.yamblz.provider; | ||
|
||
import android.database.Cursor; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import ru.yandex.yamblz.singerscontracts.Singer; | ||
|
||
/** | ||
* Transforms data from Cursor formed by {@link ru.yandex.yamblz.singerscontracts.SingersContract} | ||
*/ | ||
public class CPDataTransformer implements DataTransformer { | ||
|
||
@Override | ||
@NonNull | ||
public List<Singer> toSingers(@Nullable Cursor cursor) { | ||
List<Singer> singers = new ArrayList<>(); | ||
if(cursor == null) { | ||
return singers; | ||
} | ||
cursor.moveToFirst(); | ||
do { | ||
singers.add(Singer.readSinger(cursor)); | ||
} while(cursor.moveToNext()); | ||
cursor.close(); | ||
return singers; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Singer toSinger(@Nullable Cursor cursor) { | ||
if(cursor == null) { | ||
return null; | ||
} | ||
cursor.moveToFirst(); | ||
|
||
Singer singer = Singer.readSinger(cursor); | ||
cursor.close(); | ||
return singer; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/ru/yandex/yamblz/provider/DataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.yandex.yamblz.provider; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.util.List; | ||
|
||
import ru.yandex.yamblz.singerscontracts.Singer; | ||
|
||
public interface DataProvider { | ||
|
||
interface Callback<T>{ | ||
void postResult(@Nullable T result); | ||
} | ||
|
||
void getSingers(@NonNull Callback<List<Singer>> callback); | ||
|
||
void getSinger(int singerId, @NonNull Callback<Singer> callback); | ||
|
||
void cancel(@NonNull Callback callback); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/ru/yandex/yamblz/provider/DataTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.yandex.yamblz.provider; | ||
|
||
import android.database.Cursor; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.util.List; | ||
|
||
import ru.yandex.yamblz.singerscontracts.Singer; | ||
|
||
/** | ||
* Transforms data from Cursor formed by some contract | ||
*/ | ||
public interface DataTransformer { | ||
|
||
@NonNull | ||
List<Singer> toSingers(@Nullable Cursor cursor); | ||
|
||
@Nullable | ||
Singer toSinger(@Nullable Cursor cursor); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Понял, о каком провайдере ты говорил)
Это немного другой провайдер, о котором я говорил.
Их функционалы практически одинаковы, разница лишь в месте, в котором они находятся.
Здесь он тоже уместен и выполняет задачи, мне нравится.