This file contains hidden or 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
| dependencies { | |
| ... | |
| // Network | |
| implementation 'com.squareup.retrofit2:retrofit:2.7.1' | |
| implementation 'com.squareup.retrofit2:converter-gson:2.4.0' | |
| implementation 'com.squareup.okhttp3:okhttp:4.2.1' | |
| ... | |
| } |
This file contains hidden or 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
| object Constants { | |
| // Endpoints | |
| const val BASE_URL = "https://baseurl.com/" | |
| const val LOGIN_URL = "auth/login" | |
| const val POSTS_URL = "posts" | |
| } |
This file contains hidden or 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
| /** | |
| * Interface for defining REST request functions | |
| */ | |
| interface ApiService { | |
| @POST(Constants.LOGIN_URL) | |
| @FormUrlEncoded | |
| fun login(@Body request: LoginRequest): Call<LoginResponse> | |
| } |
This file contains hidden or 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
| /** | |
| * Retrofit instance class | |
| */ | |
| class ApiClient { | |
| private lateinit var apiService: ApiService | |
| fun getApiService(): ApiService { | |
| // Initialize ApiService if not initialized yet | |
| if (!::apiService.isInitialized) { |
This file contains hidden or 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
| data class User ( | |
| @SerializedName("id") | |
| var id: String, | |
| @SerializedName("first_name") | |
| var firstName: String, | |
| @SerializedName("last_name") | |
| var lastName: String, | |
This file contains hidden or 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
| data class LoginRequest ( | |
| @SerializedName("email") | |
| var email: String, | |
| @SerializedName("password") | |
| var password: String | |
| ) |
This file contains hidden or 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
| data class LoginResponse ( | |
| @SerializedName("status_code") | |
| var statusCode: Int, | |
| @SerializedName("auth_token") | |
| var authToken: String, | |
| @SerializedName("user") | |
| var user: User | |
| ) |
This file contains hidden or 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
| /** | |
| * Session manager to save and fetch data from SharedPreferences | |
| */ | |
| class SessionManager (context: Context) { | |
| private var prefs: SharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE) | |
| companion object { | |
| const val USER_TOKEN = "user_token" | |
| } |
This file contains hidden or 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
| class LoginActivity : AppCompatActivity() { | |
| private lateinit var sessionManager: SessionManager | |
| private lateinit var apiClient: ApiClient | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_login) | |
| apiClient = ApiClient() | |
| sessionManager = SessionManager(this) |
This file contains hidden or 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
| data class Post ( | |
| @SerializedName("id") | |
| var id: Int, | |
| @SerializedName("title") | |
| var title: String, | |
| @SerializedName("description") | |
| var description: String, | |
OlderNewer