jeudi 23 novembre 2017

Ideal Project Structure for API Interactions

I am new to Android and in progress of creating a app with MongoDB as Backend. App connects MongoDB API through Retrofit.

I thought of creating the project structure corresponds to MVC. But later reading some articles, I came up with MVP pattern.

I am not sure what pattern here it is, but as follows

com.examples.sample.activities
  --LoginActivity
  --HomeActivity

com.examples.sample.bal
  --UserBal

com.examples.sample.entity
  --User

com.examples.sample.service
  --UserService

com.examples.sample.helpers
  --Any other needed helper classes

I have few queries here,

  1. Whether this is the best practices of project structure which deals with API? Though not only Activities, I will be adding fewer fragments as well.
  2. Is it good to pass Context through Constructors to Non-Activity Classes? If not, how to get the Activity in those classes?
  3. And in UserBal, doLogin Method, I am navigating to HomeAcitivity in case of success response. Is it good approach to change activity there in non-activity class?

Here is my code:

package com.examples.sample.activities;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class LoginActivity extends Activity
{
    private Button _btnLogin;
    private UserBal _userBal;

    public LoginActivity()
    {
        _userBal = new UserBal(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.login);

        _btnLogin = (Button)findViewById(R.id.btn_login);

        _btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User user = new User();

                //populate its properties from UserName & Password EditText from Activity

                _userBal.doLogin(user);
            }
        });
    }
}

package com.examples.sample.bal;

import android.content.Context;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserBal
{
    private Context _context;
    private UserService _userService;
    private ProgressDialog _progressDialog;

    public UserBal(Context context) //Is it good pass context here in constructor from activity, memory leaks?
    {
        _context = context;

            Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        _userService = retrofit.create(UserService.class);
        _progressDialog = new ProgressDialog(_context);
    }

    public void doLogin(User user)
    {
        Call<List<User>> call = _userService.doLogin(user);
        call.enqueue(new Callback<List<User>>{

        //Show Progress Dialog in Activity
        _progressDialog.show();

            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                _progressDialog.dismiss();

                if(response.isSuccessful()) {
                    List<User> userList = response.body();

                    //if login succeed, move to another activity
                    Intent i = new Intent(_context, HomeActivity.class);
                    _context.startActivity(i);

                } else {
                    System.out.println(response.errorBody());

                    Toast.makeText(_context, "<<Custom Message>>", Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onFailure(Call<List<Change>> call, Throwable t) {
                t.printStackTrace();

                _progressDialog.dismiss();
            }   
        });
    }


}

package com.examples.sample.entity;

public class User
{
    private String _userName;
    private String _password;

    //Getter & Setter code
}

package com.examples.sample.service;

public class UserService
{
    @POST("login")
    Call<List<User>> doLogin(@Body User user);
}

Aucun commentaire:

Enregistrer un commentaire