├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_home.xml
│ │ │ │ ├── ic_school.xml
│ │ │ │ ├── ic_email.xml
│ │ │ │ ├── ic_name.xml
│ │ │ │ ├── ic_password.xml
│ │ │ │ ├── ic_users.xml
│ │ │ │ ├── ic_settings.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── layout
│ │ │ │ ├── users_fragment.xml
│ │ │ │ ├── activity_profile.xml
│ │ │ │ ├── recyclerview_users.xml
│ │ │ │ ├── home_fragment.xml
│ │ │ │ ├── activity_login.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── settings_fragment.xml
│ │ │ ├── menu
│ │ │ │ └── bottom_menu.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── net
│ │ │ │ └── simplifiedcoding
│ │ │ │ └── retrofitandroidtutorial
│ │ │ │ ├── models
│ │ │ │ ├── UsersResponse.java
│ │ │ │ ├── DefaultResponse.java
│ │ │ │ ├── LoginResponse.java
│ │ │ │ └── User.java
│ │ │ │ ├── fragments
│ │ │ │ ├── HomeFragment.java
│ │ │ │ ├── UsersFragment.java
│ │ │ │ └── SettingsFragment.java
│ │ │ │ ├── api
│ │ │ │ ├── Api.java
│ │ │ │ └── RetrofitClient.java
│ │ │ │ ├── adapters
│ │ │ │ └── UsersAdapter.java
│ │ │ │ ├── storage
│ │ │ │ └── SharedPrefManager.java
│ │ │ │ └── activities
│ │ │ │ ├── ProfileActivity.java
│ │ │ │ ├── LoginActivity.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── net
│ │ │ └── simplifiedcoding
│ │ │ └── retrofitandroidtutorial
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── net
│ │ └── simplifiedcoding
│ │ └── retrofitandroidtutorial
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── caches
│ └── build_file_checksums.ser
├── vcs.xml
├── runConfigurations.xml
├── gradle.xml
├── misc.xml
├── codeStyles
│ └── Project.xml
└── assetWizardSettings.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Retrofit Android Tutorial
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/probelalkhan/Retrofit-Android-Tutorial/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jul 18 09:51:41 IST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #21a7ed
4 | #0a5b85
5 | #0886c7
6 | #375290
7 | #e4e4e4
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_home.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_school.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_email.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/users_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_name.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/test/java/net/simplifiedcoding/retrofitandroidtutorial/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_password.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/models/UsersResponse.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.models;
2 |
3 | import java.util.List;
4 |
5 | public class UsersResponse {
6 |
7 | private boolean error;
8 | private List users;
9 |
10 | public UsersResponse(boolean error, List users) {
11 | this.error = error;
12 | this.users = users;
13 | }
14 |
15 | public boolean isError() {
16 | return error;
17 | }
18 |
19 | public List getUsers() {
20 | return users;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/bottom_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/models/DefaultResponse.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.models;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | public class DefaultResponse {
6 |
7 | @SerializedName("error")
8 | private boolean err;
9 |
10 | @SerializedName("message")
11 | private String msg;
12 |
13 | public DefaultResponse(boolean err, String msg) {
14 | this.err = err;
15 | this.msg = msg;
16 | }
17 |
18 | public boolean isErr() {
19 | return err;
20 | }
21 |
22 | public String getMsg() {
23 | return msg;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_users.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/models/LoginResponse.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.models;
2 |
3 | public class LoginResponse {
4 |
5 | private boolean error;
6 | private String message;
7 | private User user;
8 |
9 | public LoginResponse(boolean error, String message, User user) {
10 | this.error = error;
11 | this.message = message;
12 | this.user = user;
13 | }
14 |
15 | public boolean isError() {
16 | return error;
17 | }
18 |
19 | public String getMessage() {
20 | return message;
21 | }
22 |
23 | public User getUser() {
24 | return user;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/models/User.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.models;
2 |
3 | public class User {
4 |
5 | private int id;
6 | private String email, name, school;
7 |
8 | public User(int id, String email, String name, String school) {
9 | this.id = id;
10 | this.email = email;
11 | this.name = name;
12 | this.school = school;
13 | }
14 |
15 | public int getId() {
16 | return id;
17 | }
18 |
19 | public String getEmail() {
20 | return email;
21 | }
22 |
23 | public String getName() {
24 | return name;
25 | }
26 |
27 | public String getSchool() {
28 | return school;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/net/simplifiedcoding/retrofitandroidtutorial/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("net.simplifiedcoding.retrofitandroidtutorial", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_profile.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
17 |
18 |
19 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_settings.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "net.simplifiedcoding.retrofitandroidtutorial"
7 | minSdkVersion 15
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | implementation 'com.squareup.retrofit2:retrofit:2.4.0'
29 | implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
30 | implementation 'com.android.support:design:27.1.1'
31 | implementation 'com.android.support:cardview-v7:27.1.1'
32 | implementation 'com.android.support:recyclerview-v7:27.1.1'
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/fragments/HomeFragment.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.fragments;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.TextView;
11 |
12 | import net.simplifiedcoding.retrofitandroidtutorial.R;
13 | import net.simplifiedcoding.retrofitandroidtutorial.storage.SharedPrefManager;
14 |
15 | public class HomeFragment extends Fragment {
16 |
17 | private TextView textViewEmail, textViewName, textViewSchool;
18 |
19 | @Nullable
20 | @Override
21 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
22 | return inflater.inflate(R.layout.home_fragment, container, false);
23 | }
24 |
25 | @Override
26 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
27 | super.onViewCreated(view, savedInstanceState);
28 |
29 | textViewEmail = view.findViewById(R.id.textViewEmail);
30 | textViewName = view.findViewById(R.id.textViewName);
31 | textViewSchool = view.findViewById(R.id.textViewSchool);
32 |
33 | textViewEmail.setText(SharedPrefManager.getInstance(getActivity()).getUser().getEmail());
34 | textViewName.setText(SharedPrefManager.getInstance(getActivity()).getUser().getName());
35 | textViewSchool.setText(SharedPrefManager.getInstance(getActivity()).getUser().getSchool());
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recyclerview_users.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
16 |
17 |
23 |
24 |
30 |
31 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/api/Api.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.api;
2 |
3 | import net.simplifiedcoding.retrofitandroidtutorial.models.DefaultResponse;
4 | import net.simplifiedcoding.retrofitandroidtutorial.models.LoginResponse;
5 | import net.simplifiedcoding.retrofitandroidtutorial.models.UsersResponse;
6 |
7 | import retrofit2.Call;
8 | import retrofit2.http.DELETE;
9 | import retrofit2.http.Field;
10 | import retrofit2.http.FormUrlEncoded;
11 | import retrofit2.http.GET;
12 | import retrofit2.http.POST;
13 | import retrofit2.http.PUT;
14 | import retrofit2.http.Path;
15 |
16 | public interface Api {
17 |
18 |
19 | @FormUrlEncoded
20 | @POST("createuser")
21 | Call createUser(
22 | @Field("email") String email,
23 | @Field("password") String password,
24 | @Field("name") String name,
25 | @Field("school") String school
26 | );
27 |
28 | @FormUrlEncoded
29 | @POST("userlogin")
30 | Call userLogin(
31 | @Field("email") String email,
32 | @Field("password") String password
33 | );
34 |
35 | @GET("allusers")
36 | Call getUsers();
37 |
38 | @FormUrlEncoded
39 | @PUT("updateuser/{id}")
40 | Call updateUser(
41 | @Path("id") int id,
42 | @Field("email") String email,
43 | @Field("name") String name,
44 | @Field("school") String school
45 | );
46 |
47 | @FormUrlEncoded
48 | @PUT("updatepassword")
49 | Call updatePassword(
50 | @Field("currentpassword") String currentpassword,
51 | @Field("newpassword") String newpassword,
52 | @Field("email") String email
53 | );
54 |
55 | @DELETE("deleteuser/{id}")
56 | Call deleteUser(@Path("id") int id);
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/.idea/assetWizardSettings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/adapters/UsersAdapter.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.adapters;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 |
11 | import net.simplifiedcoding.retrofitandroidtutorial.R;
12 | import net.simplifiedcoding.retrofitandroidtutorial.models.User;
13 |
14 | import java.util.List;
15 |
16 | public class UsersAdapter extends RecyclerView.Adapter {
17 |
18 | private Context mCtx;
19 | private List userList;
20 |
21 | public UsersAdapter(Context mCtx, List userList) {
22 | this.mCtx = mCtx;
23 | this.userList = userList;
24 | }
25 |
26 | @NonNull
27 | @Override
28 | public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
29 | View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_users, parent, false);
30 | return new UsersViewHolder(view);
31 | }
32 |
33 | @Override
34 | public void onBindViewHolder(@NonNull UsersViewHolder holder, int position) {
35 | User user = userList.get(position);
36 |
37 | holder.textViewName.setText(user.getName());
38 | holder.textViewEmail.setText(user.getEmail());
39 | holder.textViewSchool.setText(user.getSchool());
40 | }
41 |
42 | @Override
43 | public int getItemCount() {
44 | return userList.size();
45 | }
46 |
47 | class UsersViewHolder extends RecyclerView.ViewHolder {
48 |
49 | TextView textViewName, textViewEmail, textViewSchool;
50 |
51 | public UsersViewHolder(View itemView) {
52 | super(itemView);
53 |
54 | textViewName = itemView.findViewById(R.id.textViewName);
55 | textViewEmail = itemView.findViewById(R.id.textViewEmail);
56 | textViewSchool = itemView.findViewById(R.id.textViewSchool);
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/api/RetrofitClient.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.api;
2 |
3 | import android.util.Base64;
4 |
5 | import java.io.IOException;
6 |
7 | import okhttp3.Interceptor;
8 | import okhttp3.OkHttpClient;
9 | import okhttp3.Request;
10 | import okhttp3.Response;
11 | import retrofit2.Retrofit;
12 | import retrofit2.converter.gson.GsonConverterFactory;
13 |
14 | public class RetrofitClient {
15 |
16 | private static final String AUTH = "Basic " + Base64.encodeToString(("belalkhan:123456").getBytes(), Base64.NO_WRAP);
17 |
18 | private static final String BASE_URL = "http://simplifiedlabs.xyz/MyApi/public/";
19 | private static RetrofitClient mInstance;
20 | private Retrofit retrofit;
21 |
22 |
23 | private RetrofitClient() {
24 | OkHttpClient okHttpClient = new OkHttpClient.Builder()
25 | .addInterceptor(
26 | new Interceptor() {
27 | @Override
28 | public Response intercept(Chain chain) throws IOException {
29 | Request original = chain.request();
30 |
31 | Request.Builder requestBuilder = original.newBuilder()
32 | .addHeader("Authorization", AUTH)
33 | .method(original.method(), original.body());
34 |
35 | Request request = requestBuilder.build();
36 | return chain.proceed(request);
37 | }
38 | }
39 | ).build();
40 |
41 | retrofit = new Retrofit.Builder()
42 | .baseUrl(BASE_URL)
43 | .addConverterFactory(GsonConverterFactory.create())
44 | .client(okHttpClient)
45 | .build();
46 | }
47 |
48 | public static synchronized RetrofitClient getInstance() {
49 | if (mInstance == null) {
50 | mInstance = new RetrofitClient();
51 | }
52 | return mInstance;
53 | }
54 |
55 | public Api getApi() {
56 | return retrofit.create(Api.class);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/storage/SharedPrefManager.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.storage;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import net.simplifiedcoding.retrofitandroidtutorial.models.User;
7 |
8 | public class SharedPrefManager {
9 |
10 | private static final String SHARED_PREF_NAME = "my_shared_preff";
11 |
12 | private static SharedPrefManager mInstance;
13 | private Context mCtx;
14 |
15 | private SharedPrefManager(Context mCtx) {
16 | this.mCtx = mCtx;
17 | }
18 |
19 |
20 | public static synchronized SharedPrefManager getInstance(Context mCtx) {
21 | if (mInstance == null) {
22 | mInstance = new SharedPrefManager(mCtx);
23 | }
24 | return mInstance;
25 | }
26 |
27 |
28 | public void saveUser(User user) {
29 |
30 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
31 | SharedPreferences.Editor editor = sharedPreferences.edit();
32 |
33 | editor.putInt("id", user.getId());
34 | editor.putString("email", user.getEmail());
35 | editor.putString("name", user.getName());
36 | editor.putString("school", user.getSchool());
37 |
38 | editor.apply();
39 |
40 | }
41 |
42 | public boolean isLoggedIn() {
43 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
44 | return sharedPreferences.getInt("id", -1) != -1;
45 | }
46 |
47 | public User getUser() {
48 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
49 | return new User(
50 | sharedPreferences.getInt("id", -1),
51 | sharedPreferences.getString("email", null),
52 | sharedPreferences.getString("name", null),
53 | sharedPreferences.getString("school", null)
54 | );
55 | }
56 |
57 | public void clear() {
58 | SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
59 | SharedPreferences.Editor editor = sharedPreferences.edit();
60 | editor.clear();
61 | editor.apply();
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/fragments/UsersFragment.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.fragments;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 |
13 | import net.simplifiedcoding.retrofitandroidtutorial.R;
14 | import net.simplifiedcoding.retrofitandroidtutorial.adapters.UsersAdapter;
15 | import net.simplifiedcoding.retrofitandroidtutorial.api.RetrofitClient;
16 | import net.simplifiedcoding.retrofitandroidtutorial.models.User;
17 | import net.simplifiedcoding.retrofitandroidtutorial.models.UsersResponse;
18 |
19 | import java.util.List;
20 |
21 | import retrofit2.Call;
22 | import retrofit2.Callback;
23 | import retrofit2.Response;
24 |
25 | public class UsersFragment extends Fragment {
26 |
27 | private RecyclerView recyclerView;
28 | private UsersAdapter adapter;
29 | private List userList;
30 |
31 | @Nullable
32 | @Override
33 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
34 | return inflater.inflate(R.layout.users_fragment, container, false);
35 | }
36 |
37 | @Override
38 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
39 | super.onViewCreated(view, savedInstanceState);
40 |
41 | recyclerView = view.findViewById(R.id.recyclerView);
42 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
43 |
44 |
45 | Call call = RetrofitClient.getInstance().getApi().getUsers();
46 |
47 | call.enqueue(new Callback() {
48 | @Override
49 | public void onResponse(Call call, Response response) {
50 |
51 | userList = response.body().getUsers();
52 | adapter = new UsersAdapter(getActivity(), userList);
53 | recyclerView.setAdapter(adapter);
54 | }
55 |
56 | @Override
57 | public void onFailure(Call call, Throwable t) {
58 |
59 | }
60 | });
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/home_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
13 |
14 |
15 |
21 |
22 |
27 |
28 |
29 |
30 |
31 |
32 |
38 |
39 |
44 |
45 |
46 |
47 |
48 |
49 |
55 |
56 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/activities/ProfileActivity.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.activities;
2 |
3 | import android.content.Intent;
4 | import android.support.annotation.NonNull;
5 | import android.support.design.widget.BottomNavigationView;
6 | import android.support.design.widget.NavigationView;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.os.Bundle;
10 | import android.view.MenuItem;
11 | import android.widget.TextView;
12 |
13 | import net.simplifiedcoding.retrofitandroidtutorial.R;
14 | import net.simplifiedcoding.retrofitandroidtutorial.fragments.HomeFragment;
15 | import net.simplifiedcoding.retrofitandroidtutorial.fragments.SettingsFragment;
16 | import net.simplifiedcoding.retrofitandroidtutorial.fragments.UsersFragment;
17 | import net.simplifiedcoding.retrofitandroidtutorial.models.User;
18 | import net.simplifiedcoding.retrofitandroidtutorial.storage.SharedPrefManager;
19 |
20 | public class ProfileActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
21 |
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_profile);
27 |
28 | BottomNavigationView navigationView = findViewById(R.id.bottom_nav);
29 | navigationView.setOnNavigationItemSelectedListener(this);
30 |
31 | displayFragment(new HomeFragment());
32 | }
33 |
34 | private void displayFragment(Fragment fragment) {
35 | getSupportFragmentManager()
36 | .beginTransaction()
37 | .replace(R.id.relativeLayout, fragment)
38 | .commit();
39 | }
40 |
41 |
42 | @Override
43 | protected void onStart() {
44 | super.onStart();
45 |
46 | if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
47 | Intent intent = new Intent(this, MainActivity.class);
48 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
49 | startActivity(intent);
50 | }
51 | }
52 |
53 | @Override
54 | public boolean onNavigationItemSelected(@NonNull MenuItem item) {
55 |
56 | Fragment fragment = null;
57 |
58 | switch(item.getItemId()){
59 | case R.id.menu_home:
60 | fragment = new HomeFragment();
61 | break;
62 | case R.id.menu_users:
63 | fragment = new UsersFragment();
64 | break;
65 | case R.id.menu_settings:
66 | fragment = new SettingsFragment();
67 | break;
68 | }
69 |
70 | if(fragment != null){
71 | displayFragment(fragment);
72 | }
73 |
74 | return false;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
30 |
31 |
39 |
40 |
48 |
49 |
50 |
51 |
52 |
64 |
65 |
76 |
77 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
30 |
31 |
39 |
40 |
48 |
49 |
57 |
58 |
66 |
67 |
68 |
69 |
81 |
82 |
93 |
94 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/activities/LoginActivity.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.activities;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.util.Patterns;
8 | import android.view.View;
9 | import android.widget.EditText;
10 | import android.widget.Toast;
11 |
12 | import net.simplifiedcoding.retrofitandroidtutorial.R;
13 | import net.simplifiedcoding.retrofitandroidtutorial.api.RetrofitClient;
14 | import net.simplifiedcoding.retrofitandroidtutorial.models.LoginResponse;
15 | import net.simplifiedcoding.retrofitandroidtutorial.storage.SharedPrefManager;
16 |
17 | import retrofit2.Call;
18 | import retrofit2.Callback;
19 | import retrofit2.Response;
20 |
21 | public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
22 |
23 |
24 | private EditText editTextEmail;
25 | private EditText editTextPassword;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.activity_login);
31 |
32 | editTextEmail = findViewById(R.id.editTextEmail);
33 | editTextPassword = findViewById(R.id.editTextPassword);
34 |
35 | findViewById(R.id.buttonLogin).setOnClickListener(this);
36 | findViewById(R.id.textViewRegister).setOnClickListener(this);
37 | }
38 |
39 | @Override
40 | protected void onStart() {
41 | super.onStart();
42 |
43 | if (SharedPrefManager.getInstance(this).isLoggedIn()) {
44 | Intent intent = new Intent(this, ProfileActivity.class);
45 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
46 | startActivity(intent);
47 | }
48 | }
49 |
50 | private void userLogin() {
51 |
52 | String email = editTextEmail.getText().toString().trim();
53 | String password = editTextPassword.getText().toString().trim();
54 |
55 | if (email.isEmpty()) {
56 | editTextEmail.setError("Email is required");
57 | editTextEmail.requestFocus();
58 | return;
59 | }
60 |
61 | if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
62 | editTextEmail.setError("Enter a valid email");
63 | editTextEmail.requestFocus();
64 | return;
65 | }
66 |
67 | if (password.isEmpty()) {
68 | editTextPassword.setError("Password required");
69 | editTextPassword.requestFocus();
70 | return;
71 | }
72 |
73 | if (password.length() < 6) {
74 | editTextPassword.setError("Password should be atleast 6 character long");
75 | editTextPassword.requestFocus();
76 | return;
77 | }
78 |
79 | Call call = RetrofitClient
80 | .getInstance().getApi().userLogin(email, password);
81 |
82 | call.enqueue(new Callback() {
83 | @Override
84 | public void onResponse(Call call, Response response) {
85 | LoginResponse loginResponse = response.body();
86 |
87 | if (!loginResponse.isError()) {
88 |
89 | SharedPrefManager.getInstance(LoginActivity.this)
90 | .saveUser(loginResponse.getUser());
91 |
92 | Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
93 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
94 | startActivity(intent);
95 |
96 |
97 | } else {
98 | Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
99 | }
100 | }
101 |
102 | @Override
103 | public void onFailure(Call call, Throwable t) {
104 |
105 | }
106 | });
107 |
108 | }
109 |
110 | @Override
111 | public void onClick(View v) {
112 | switch (v.getId()) {
113 | case R.id.buttonLogin:
114 | userLogin();
115 | break;
116 | case R.id.textViewRegister:
117 | startActivity(new Intent(this, MainActivity.class));
118 | break;
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/activities/MainActivity.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.activities;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.util.Patterns;
8 | import android.view.View;
9 | import android.widget.EditText;
10 | import android.widget.Toast;
11 |
12 | import net.simplifiedcoding.retrofitandroidtutorial.models.DefaultResponse;
13 | import net.simplifiedcoding.retrofitandroidtutorial.R;
14 | import net.simplifiedcoding.retrofitandroidtutorial.api.RetrofitClient;
15 | import net.simplifiedcoding.retrofitandroidtutorial.storage.SharedPrefManager;
16 |
17 | import java.io.IOException;
18 |
19 | import retrofit2.Call;
20 | import retrofit2.Callback;
21 | import retrofit2.Response;
22 |
23 | /*
24 | * Sign Up Activity
25 | * */
26 |
27 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
28 |
29 | private EditText editTextEmail, editTextPassword, editTextName, editTextSchool;
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_main);
35 |
36 | editTextEmail = findViewById(R.id.editTextEmail);
37 | editTextPassword = findViewById(R.id.editTextPassword);
38 | editTextName = findViewById(R.id.editTextName);
39 | editTextSchool = findViewById(R.id.editTextSchool);
40 |
41 | findViewById(R.id.buttonSignUp).setOnClickListener(this);
42 | findViewById(R.id.textViewLogin).setOnClickListener(this);
43 | }
44 |
45 |
46 | @Override
47 | protected void onStart() {
48 | super.onStart();
49 | if(SharedPrefManager.getInstance(this).isLoggedIn()){
50 | Intent intent = new Intent(this, ProfileActivity.class);
51 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
52 | startActivity(intent);
53 | }
54 | }
55 |
56 | private void userSignUp() {
57 | String email = editTextEmail.getText().toString().trim();
58 | String password = editTextPassword.getText().toString().trim();
59 | String name = editTextName.getText().toString().trim();
60 | String school = editTextSchool.getText().toString().trim();
61 |
62 | if (email.isEmpty()) {
63 | editTextEmail.setError("Email is required");
64 | editTextEmail.requestFocus();
65 | return;
66 | }
67 |
68 | if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
69 | editTextEmail.setError("Enter a valid email");
70 | editTextEmail.requestFocus();
71 | return;
72 | }
73 |
74 | if (password.isEmpty()) {
75 | editTextPassword.setError("Password required");
76 | editTextPassword.requestFocus();
77 | return;
78 | }
79 |
80 | if (password.length() < 6) {
81 | editTextPassword.setError("Password should be atleast 6 character long");
82 | editTextPassword.requestFocus();
83 | return;
84 | }
85 |
86 | if (name.isEmpty()) {
87 | editTextName.setError("Name required");
88 | editTextName.requestFocus();
89 | return;
90 | }
91 |
92 | if (school.isEmpty()) {
93 | editTextSchool.setError("School required");
94 | editTextSchool.requestFocus();
95 | return;
96 | }
97 |
98 | Call call = RetrofitClient
99 | .getInstance()
100 | .getApi()
101 | .createUser(email, password, name, school);
102 |
103 |
104 | call.enqueue(new Callback() {
105 | @Override
106 | public void onResponse(Call call, Response response) {
107 | if (response.code() == 201) {
108 |
109 | DefaultResponse dr = response.body();
110 | Toast.makeText(MainActivity.this, dr.getMsg(), Toast.LENGTH_LONG).show();
111 |
112 | } else if (response.code() == 422) {
113 | Toast.makeText(MainActivity.this, "User already exist", Toast.LENGTH_LONG).show();
114 | }
115 | }
116 |
117 | @Override
118 | public void onFailure(Call call, Throwable t) {
119 |
120 | Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
121 |
122 | }
123 | });
124 |
125 |
126 | }
127 |
128 | @Override
129 | public void onClick(View v) {
130 | switch (v.getId()) {
131 | case R.id.buttonSignUp:
132 | userSignUp();
133 | break;
134 | case R.id.textViewLogin:
135 |
136 | startActivity(new Intent(this, LoginActivity.class));
137 |
138 | break;
139 | }
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/settings_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
18 |
19 |
27 |
28 |
36 |
37 |
45 |
46 |
55 |
56 |
62 |
63 |
69 |
70 |
78 |
79 |
87 |
88 |
89 |
98 |
99 |
105 |
106 |
107 |
116 |
117 |
118 |
128 |
129 |
130 |
131 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/net/simplifiedcoding/retrofitandroidtutorial/fragments/SettingsFragment.java:
--------------------------------------------------------------------------------
1 | package net.simplifiedcoding.retrofitandroidtutorial.fragments;
2 |
3 | import android.app.AlertDialog;
4 | import android.content.DialogInterface;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.support.annotation.NonNull;
8 | import android.support.annotation.Nullable;
9 | import android.support.v4.app.Fragment;
10 | import android.util.Patterns;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.EditText;
15 | import android.widget.Toast;
16 |
17 | import net.simplifiedcoding.retrofitandroidtutorial.R;
18 | import net.simplifiedcoding.retrofitandroidtutorial.activities.LoginActivity;
19 | import net.simplifiedcoding.retrofitandroidtutorial.activities.MainActivity;
20 | import net.simplifiedcoding.retrofitandroidtutorial.activities.ProfileActivity;
21 | import net.simplifiedcoding.retrofitandroidtutorial.api.RetrofitClient;
22 | import net.simplifiedcoding.retrofitandroidtutorial.models.DefaultResponse;
23 | import net.simplifiedcoding.retrofitandroidtutorial.models.LoginResponse;
24 | import net.simplifiedcoding.retrofitandroidtutorial.models.User;
25 | import net.simplifiedcoding.retrofitandroidtutorial.storage.SharedPrefManager;
26 |
27 | import retrofit2.Call;
28 | import retrofit2.Callback;
29 | import retrofit2.Response;
30 |
31 | public class SettingsFragment extends Fragment implements View.OnClickListener {
32 |
33 | private EditText editTextEmail, editTextName, editTextSchool;
34 | private EditText editTextCurrentPassword, editTextNewPassword;
35 |
36 |
37 | @Nullable
38 | @Override
39 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40 | return inflater.inflate(R.layout.settings_fragment, container, false);
41 | }
42 |
43 | @Override
44 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
45 | super.onViewCreated(view, savedInstanceState);
46 |
47 | editTextEmail = view.findViewById(R.id.editTextEmail);
48 | editTextName = view.findViewById(R.id.editTextName);
49 | editTextSchool = view.findViewById(R.id.editTextSchool);
50 | editTextCurrentPassword = view.findViewById(R.id.editTextCurrentPassword);
51 | editTextNewPassword = view.findViewById(R.id.editTextNewPassword);
52 |
53 | view.findViewById(R.id.buttonSave).setOnClickListener(this);
54 | view.findViewById(R.id.buttonChangePassword).setOnClickListener(this);
55 | view.findViewById(R.id.buttonLogout).setOnClickListener(this);
56 | view.findViewById(R.id.buttonDelete).setOnClickListener(this);
57 | }
58 |
59 | private void updateProfile() {
60 | String email = editTextEmail.getText().toString().trim();
61 | String name = editTextName.getText().toString().trim();
62 | String school = editTextSchool.getText().toString().trim();
63 |
64 | if (email.isEmpty()) {
65 | editTextEmail.setError("Email is required");
66 | editTextEmail.requestFocus();
67 | return;
68 | }
69 |
70 | if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
71 | editTextEmail.setError("Enter a valid email");
72 | editTextEmail.requestFocus();
73 | return;
74 | }
75 |
76 | if (name.isEmpty()) {
77 | editTextName.setError("Name required");
78 | editTextName.requestFocus();
79 | return;
80 | }
81 |
82 | if (school.isEmpty()) {
83 | editTextSchool.setError("School required");
84 | editTextSchool.requestFocus();
85 | return;
86 | }
87 |
88 | User user = SharedPrefManager.getInstance(getActivity()).getUser();
89 |
90 | Call call = RetrofitClient.getInstance()
91 | .getApi().updateUser(
92 | user.getId(),
93 | email,
94 | name,
95 | school
96 | );
97 |
98 | call.enqueue(new Callback() {
99 | @Override
100 | public void onResponse(Call call, Response response) {
101 |
102 | Toast.makeText(getActivity(), response.body().getMessage(), Toast.LENGTH_LONG).show();
103 |
104 | if (!response.body().isError()) {
105 | SharedPrefManager.getInstance(getActivity()).saveUser(response.body().getUser());
106 | }
107 |
108 | }
109 |
110 | @Override
111 | public void onFailure(Call call, Throwable t) {
112 |
113 | }
114 | });
115 | }
116 |
117 | private void updatePassword() {
118 | String currentpassword = editTextCurrentPassword.getText().toString().trim();
119 | String newpassword = editTextNewPassword.getText().toString().trim();
120 |
121 | if (currentpassword.isEmpty()) {
122 | editTextCurrentPassword.setError("Password required");
123 | editTextCurrentPassword.requestFocus();
124 | return;
125 | }
126 |
127 | if (newpassword.isEmpty()) {
128 | editTextNewPassword.setError("Enter new password");
129 | editTextNewPassword.requestFocus();
130 | return;
131 | }
132 |
133 |
134 | User user = SharedPrefManager.getInstance(getActivity()).getUser();
135 |
136 | Call call = RetrofitClient.getInstance().getApi()
137 | .updatePassword(currentpassword, newpassword, user.getEmail());
138 |
139 | call.enqueue(new Callback() {
140 | @Override
141 | public void onResponse(Call call, Response response) {
142 |
143 | Toast.makeText(getActivity(), response.body().getMsg(), Toast.LENGTH_LONG).show();
144 | }
145 |
146 | @Override
147 | public void onFailure(Call call, Throwable t) {
148 |
149 | }
150 | });
151 | }
152 |
153 | private void logout() {
154 | SharedPrefManager.getInstance(getActivity()).clear();
155 | Intent intent = new Intent(getActivity(), LoginActivity.class);
156 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
157 | startActivity(intent);
158 | }
159 |
160 | private void deleteUser() {
161 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
162 | builder.setTitle("Are you sure?");
163 | builder.setMessage("This action is irreversible...");
164 | builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
165 | @Override
166 | public void onClick(DialogInterface dialog, int which) {
167 | User user = SharedPrefManager.getInstance(getActivity()).getUser();
168 | Call call = RetrofitClient.getInstance().getApi().deleteUser(user.getId());
169 |
170 | call.enqueue(new Callback() {
171 | @Override
172 | public void onResponse(Call call, Response response) {
173 |
174 | if (!response.body().isErr()) {
175 | SharedPrefManager.getInstance(getActivity()).clear();
176 | SharedPrefManager.getInstance(getActivity()).clear();
177 | Intent intent = new Intent(getActivity(), MainActivity.class);
178 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
179 | startActivity(intent);
180 | }
181 |
182 | Toast.makeText(getActivity(), response.body().getMsg(), Toast.LENGTH_LONG).show();
183 | }
184 |
185 | @Override
186 | public void onFailure(Call call, Throwable t) {
187 |
188 | }
189 | });
190 |
191 | }
192 | });
193 | builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
194 | @Override
195 | public void onClick(DialogInterface dialog, int which) {
196 |
197 | }
198 | });
199 |
200 | AlertDialog ad = builder.create();
201 | ad.show();
202 | }
203 |
204 | @Override
205 | public void onClick(View v) {
206 | switch (v.getId()) {
207 | case R.id.buttonSave:
208 | updateProfile();
209 | break;
210 | case R.id.buttonChangePassword:
211 | updatePassword();
212 | break;
213 | case R.id.buttonLogout:
214 | logout();
215 | break;
216 | case R.id.buttonDelete:
217 | deleteUser();
218 | break;
219 | }
220 | }
221 | }
222 |
--------------------------------------------------------------------------------