Android library which doing more easy development application for android. The library contains part for working with db, some widgets, loaders, requests for REST API and other tasty features.
Gradle dependency RELEASE:
compile 'com.rightutils:app:1.4.0@aar'
Gradle dependency SNAPSHOT:
compile 'com.rightutils:app:1.4.0-SNAPSHOT@aar'
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
Maven dependency:
<dependency>
<groupId>com.rightutils</groupId>
<artifactId>app</artifactId>
<version>1.4.0</version>
</dependency>
#####1) create class:
public class DBUtils extends RightDBUtils {
private static final String TAG = DBUtils.class.getSimpleName();
public static DBUtils newInstance(Context context, String name, int version) {
DBUtils dbUtils = new DBUtils();
dbUtils.setDBContext(context, name, version);
return dbUtils;
}
}
#####2) create and init static variable to your Application class:
public class ExampleApplication extends Application {
public static DBUtils dbUtils;
@Override
public void onCreate() {
super.onCreate();
//example_db.sqlite - name of existing database file in assets folder
dbUtils = DBUtils.newInstance(this, "example_db.sqlite", 1);
}
}
####How to use:
//Supports fields type: all primitive types, String, Long, Integer, Boolean, Float, Double, Date
//Use @TableName("table_name") annotation if name are different
public class Company {
//Use @ColumnName("_id") annotation if column name are different.
//Use @ColumnIgnore annotation if this field not saving in database
private long id;
private String name;
}
Add company:
add(company);
Add list of company:
add(companies);
Retrieve all companies from db:
RightList<Company> companies = getAll(Company.class);
Delete all companies from db:
deleteAll(Company.class);
Retrieve companies by ids:
RightList<Company> companies = getAllWhere(String.format("id IN (%s)", TextUtils.join(",", ids)), Company.class);
Delete companies by ids:
deleteWhere(Company.class, String.format("id IN (%s)", TextUtils.join(",", ids)));
//or
deleteWhere(Company.class, "id", ids)));
####TypeFaceWidgets
#####1) Specify the same typeface for all widgets in application theme.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="customTypefaceStyle">@style/TypefaceStyle</item>
</style>
<style name="TypefaceStyle">
<item name="typeface">fonts/Roboto-Light.ttf</item>
</style>
#####2) Specify typefaces for each type of widgets in application theme.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">@style/TextViewStyle</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
<item name="android:editTextStyle">@style/EditStyle</item>
<item name="android:radioButtonStyle">@style/RadioButton</item>
<item name="android:checkboxStyle">@style/CheckBoxStyle</item>
</style>
Each style has to inherited extend default parent element style to keep all attributes, e.g.
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
<style name="EditTextStyle" parent="android:Widget.EditText">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
<style name="RadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
<style name="ButtonStyle" parent="android:Widget.Button">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
<style name="CheckBoxStyle" parent="android:Widget.CompoundButton.CheckBox">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
Then you can add new styles extended already defined.
<style name="EditStyle.Login">
<item name="android:textColor">@android:color/white</item>
<item name="android:textColorHint">@android:color/white</item>
<item name="typeface">fonts/OpenSans-Regular.ttf</item>
</style>
<style name="TextViewStyle.Bold">
<item name="typeface">fonts/OpenSans-Bold.ttf</item>
</style>
#####3) Specify font in style.
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="typeface">fonts/Roboto-Regular.ttf</item>
</style>
Set style for widget directly in layout xml file.
<com.rightutils.rightutils.widgets.TypefaceTextView
android:layout_width="match_parent"
android:text="Lorem Ipsum style"
android:layout_height="wrap_content"
style="@style/TypefaceStyle"/>
#####4) Specify font directly in widget's element in layout xml file. Declare font tag in root element of layout xml file.
xmlns:font="http://schemas.android.com/apk/res-auto"
<com.rightutils.rightutils.widgets.TypefaceTextView
android:layout_width="match_parent"
android:text="Lorem Ipsum style"
android:layout_height="wrap_content"
font:typeface="fonts/Roboto-Regular.ttf"/>
First of all you need create the class which extends from BaseLoader class
``` java public class CustomLoader extends BaseLoader {
private static final String TAG = CustomLoader.class.getSimpleName();
private static final int loaderId = 1; // unique identificator in loader manager
public CustomLoader(FragmentActivity fragmentActivity) {
super(fragmentActivity, loaderId);
}
@Override
public SomeType loadInBackground() {
//TODO something
return someType;
}
}
After that you can use your loader in activities or fragments<br>
``` java
new CustomLoader(this)
//optional param, 'false' - loader can't be canceled. 'true' - loader can be canceled; by default 'true'
.setCancelable(false)
//optional param, if need access to fragment after execution
.setContainer(LoaderFragment.class)
//optional param, you can use standart dialog themes or customs;
.setTheme(android.R.style.Theme_Holo_Dialog)
.setLoaderListener(new BaseLoaderListener<SomeType>() {
@Override
public void onLoadFinished(FragmentActivity activity, Fragment fragmentContainer, SomeType data, BaseLoader<SomeType> loader) {
//activity - always valid activity, even after rotate screen
//fragmentContainer - can be null if setContainer not invoke or fragment was changed
//do something with result
}
})
.execute();
By default you can use basic implementation RightRequest interface - BasicRightRequest which contains several type of requests: ``` java HttpResponse getHttpResponse(String url) throws Exception;
HttpResponse getHttpResponse(String url, RequestConfig config) throws Exception;
HttpResponse getHttpResponse(String url, Header[] headers) throws Exception;
HttpResponse getHttpResponse(String url, Header header) throws Exception;
HttpResponse postHttpResponse(String url, List nameValuePairs) throws Exception;
HttpResponse postHttpResponse(String url, Header header, List nameValuePairs) throws Exception;
HttpResponse postHttpResponse(String url, Header[] headers, List nameValuePairs) throws Exception;
HttpResponse postHttpResponse(String url, HttpEntity entity) throws Exception;
HttpResponse postHttpResponse(String url, Header header, HttpEntity entity) throws Exception;
HttpResponse postHttpResponse(String url, StringEntity entity) throws Exception;
HttpResponse postHttpResponse(String url, String json) throws Exception;
HttpResponse postHttpResponse(String url, Header header, String json) throws Exception;
HttpResponse deleteHttpResponse(String url) throws Exception;
HttpResponse deleteHttpResponse(String url, Header header) throws Exception;
HttpResponse putHttpResponse(String url, List nameValuePairs) throws Exception;
HttpResponse putHttpResponse(String url, String json) throws Exception;
HttpResponse putHttpResponse(String url, HttpEntity entity) throws Exception;
HttpResponse putHttpResponse(String url, StringEntity entity) throws Exception;
HttpResponse putHttpResponse(String url, Header header, String json) throws Exception;
HttpResponse putHttpResponse(String url, Header[] headers, List nameValuePairs) throws Exception;
HttpResponse putHttpResponse(String url, Header header, List nameValuePairs) throws Exception;
HttpResponse putHttpResponse(String url, Header header) throws Exception;
HttpResponse putHttpResponse(String url, Header header, HttpEntity entity) throws Exception;
But if you need some another type of request you can create your class which extends from BasicRightRequest class and add you methods there.
Usage example:
``` java
@Override
protected String doInBackground(Void... params) {
try {
HttpResponse response = new BasicRightRequest().getHttpResponse(PROFILE_URL, new BasicHeader("Authorization", cache.getAuthToken()));
int status = response.getStatusLine().getStatusCode();
Log.i(TAG, "status code: " + String.valueOf(status));
if (status == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
Log.e(TAG, "run", e);
}
return null;
}
public class RightLoader extends RightBaseLazyLoader{
public RightLoader(FragmentActivity fragmentActivity, int loaderId) {
super(fragmentActivity, loaderId,SystemUtils.MAPPER);
// default setup
setResponseListener(HttpStatus.SC_FORBIDDEN, new CallbackResponse<String>() {
@Override
public void response(int pageCode, String response,FragmentActivity fragmentActivity, Fragment fragment) throws Exception {
}
});
setResponseListener(HttpStatus.SC_BAD_REQUEST, new CallbackResponse<String>() {
@Override
public void response(int pageCode, String response,FragmentActivity fragmentActivity, Fragment fragment) throws Exception {
}
});
}
}
new RightLoader(this,SystemUtils.LOADER_FRIENDS).setRequest(new GetFriendsRequest(this))
.setResponseListener(HttpStatus.SC_OK, new RightBaseLazyLoader.CallbackResponse<String>() {
@Override
public void response(int pageCode, String response,FragmentActivity fragmentActivity, Fragment fragment) throws Exception {
// success
}
}).execute();
public class GetFriendsRequest extends LazyRequest {
private Context context;
private Cache cache;
public GetFriendsRequest(Context context) {
this.context = context;
}
@Override
public Header getHeader() {
SystemUtils.getCache(context, new CacheUtils.CallBack<Cache>() {
@Override
public boolean run(Cache cache) {
GetFriendsRequest.this.cache = cache;
return false;
}
});
return new BasicHeader(SystemUtils.APIKEY_PARAM, cache.getAuthToken());
}
@Override
public String getUrl() {
return SystemUtils.API_URL_FRIENDS;
}
}
public class CreateReminderRequest extends LazyRequest {
private Cache cache;
private ImportantDate importantDate;
public CreateReminderRequest(Cache cache, ImportantDate importantDate) {
this.cache = cache;
this.importantDate = importantDate;
}
@Override
public String getPostJson() throws JSONException {
JSONObject jsonPOst = new JSONObject();
jsonPOst.put("reminder",new JSONObject().put("date", importantDate.getStart()));
return jsonPOst.toString();
}
@Override
public String getUrl() {
return String.format(SystemUtils.API_URL_POST_REMINDER,importantDate.getId());
}
@Override
public Header getHeader() {
return new BasicHeader(SystemUtils.APIKEY_PARAM, cache.getAuthToken());
}
}
public class DeleteReminderRrequest extends LazyRequest {
private Cache cache;
private ImportantDate importantDate;
public DeleteReminderRrequest(Cache cache, ImportantDate importantDate) {
this.cache = cache;
this.importantDate = importantDate;
}
@Override
public String getUrl() {
return String.format(SystemUtils.API_URL_DELETE_REMINDER,importantDate.getId());
}
@Override
public HttpResponse getCustomResponse() throws Exception {
BasicRightRequest brr = new BasicRightRequest();
return brr.deleteHttpResponse(getUrl(),new BasicHeader(SystemUtils.APIKEY_PARAM, cache.getAuthToken()));
}
}
public class SearchUserRequest extends LazyRequest {
private Context context;
private Cache cache;
private String identity;
public SearchUserRequest(Context context,String identity) {
this.identity = identity;
this.context = context;
}
@Override
public String getUrl() {
return String.format(SystemUtils.API_URL_SEARCH_BYEMAILID,identity);
}
@Override
public Header getHeader() {
SystemUtils.getCache(context, new CacheUtils.CallBack<Cache>() {
@Override
public boolean run(Cache cache) {
SearchUserRequest.this.cache = cache;
return false;
}
});
return new BasicHeader(SystemUtils.APIKEY_PARAM, cache.getAuthToken());
}
}
public class UpdateProfileRequest extends AddMultipartEntityBuilderToLazyRequest {
private Context context;
private Cache cache;
public UpdateProfileRequest(Context context,Cache cache) {
super(context);
this.context = context;
this.cache = cache;
}
@Override
public String getUrl() {
return SystemUtils.API_URL_UPDATE_PROFILE;
}
@Override
public Header getHeader() {
return new BasicHeader(SystemUtils.APIKEY_PARAM, cache.getAuthToken());
}
}
UpdateProfileRequest request = new UpdateProfileRequest(this);
request.addPart("user[firstName]",edtFirstName.getText().toString());
request.addPart("user[lastName]",edtSureName.getText().toString());
request.addPart("user[gender]",Integer.toString(gender));
request.addPart("user[birthday]",DateUtils.calendarToJson(birthday));
if(profiileImageURI != null) {
request.addPart("picture",profiileImageURI);
}
new RightLoader(this,SystemUtils.LOADER_UPDATE_PROFILE)
.setRequest(request)
.setResponseListener(HttpStatus.SC_OK, new RightBaseLazyLoader.CallbackResponse<String>() {
@Override
public void response(int pageCode, String response,FragmentActivity fragmentActivity, Fragment fragment) throws Exception {
// success
}
}).execute();
public class SingleUserResponse implements RightResponse {
public User user;
public SingleUserResponse() {
}
@Override
public boolean isValid() {
return user != null;
}
@Override
public String toString() {
return "SingleUserResponse{" +
"user=" + user +
'}';
}
}